Catching ArrayIndexOutOfBoundsException in backward? - java

I creating pacman game. I have array of size 15x15, total 225fields. When I move from 255 to i.e.256, I got ArrayIndexOutOfBoundsException, this makes sense. So I can catch it and do some operation, lets say I set new starting point of pacman. But if I go from field 75 to 74 nothing happened.
So I asking, can I somehow catch this and do some operation, like I mention above.

You should not rely on ArrayIndexOutOfBoundsException for normal logic. This exception is an indication of a programming error.
Instead, you should check the index before incrementing it:
if (currentIndex == 255) {
// "special logic"
} else {
// "usual logic"
}
This way you can also handle any "special" indexes, e.g.
if ((currentIndex + 1) % 15 == 0) {
// "special logic"
} else {
// "usual logic"
}
Another point: consider using two indexes - x and y - if you are programming a 2-D game.
Every move modifies x and/or y, which can easily "wrap around" like in pacman (e.g. 13 -> 14 -> 15 -> 1 -> 2 -> ...).
And convert the (x,y)-Pair to an index only when you need to access the field element:
// Assuming that x and y are 1-based, not 0-based:
public FieldElement getFieldElementAtPosition(final int x, final int y) {
final int index = (y - 1) * FIELD_WIDTH + x - 1;
return fieldArray[index];
}

Related

Using ASCII for board co-ordinates in Java

I'm pretty new to Java and learning it as a hobby mainly after school to kill my free time productively. I find it really interesting and picking it up relatively pain free however I'm a bit stuck trying to implement a basic chess program that can be played via the command line. Ideally, I'd like to print out the board initially with just Kings and Queens on both sides and get them moving forwards, backwards and diagonally. (Once I get the hang of this I would try adding all other pieces, but at first I would like to just start as simply as possible). For simplicity I will just be using the standard 8x8 playing board.
I've already created a main game loop which uses a command line input of 1 to switch players and 2 to exit the game but I am stuck when it comes to printing out positions and having them change during the game. Firstly I would like to print out the starting positions of both kings and queens as a string (e.g ["D1","E1","D8","E8"]) for the player to see. I imagine the best way to do this is by using the ASCII index but I'm not really sure where to go from this, I tried the code below I know it's not correct and I don't know what to change...
int num[] = {65, 66, 67, 68, 69, 70, 71, 72};
String start_Pos =null;
for(int i = 4; i < 6; i++){
start_Pos[i] = (Character.toString((char)i) + "1");
}
int num[] = {65, 66, 67, 68, 69, 70, 71, 72};
String start_Pos =null;
for(int i = 4; i < 6; i++){
start_Pos1[i] = (Character.toString((char)i) + "8");
}
System.out.println(start_Pos + start_Pos1);
I have also tried coding the board set-up but this is literally just printing out the starting positions of the pieces and therefore will not change when a player makes a move - ideally it should. An example would be the starting positions are shown on the board like so:
Photo (PS I know QK should be swapped on one side so they're not opposite each other, my bad!
But after an input of "D1 D3" (first coordinates indicating what piece and the second indicating final position)from player 1 the board changes to reflect this. Is this possible without having to recompile the entire code after each turn? (Maybe a stupid question...).
Any help would be greatly appreciated. I find learning through making small games like these a lot more interesting and rewarding so if anyone is able to help me implement this I would be very thankful.
Java is an Object Oriented language, so it is best to use classes and object instances.
Of course, with a chessboard, you'll still want to perform calculations on the x, y coordinates. Computers are just better with numbers, they have problems interpreting things. The chess notation is mainly of use to us humans.
So here is a class that can be used to parse and interpret chess positions. Note that you should keep this class immutable and simply use a new object instance rather than changing the x or y field.
public final class ChessPosition {
// use constants so you understand what the 8 is in your code
private static final int BOARD_SIZE = 8;
// zero based indices, to be used in a 2D array
private final int x;
private final int y;
public ChessPosition(int x, int y) {
// guards checks, so that the object doesn't enter an invalid state
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
throw new IllegalArgumentException("Invalid position");
}
this.x = x;
this.y = y;
}
public ChessPosition(String chessNotation) {
// accept upper and lowercase, but output only uppercase
String chessNotationUpper = chessNotation.toUpperCase();
// use a regular expression for this guard
if (!chessNotationUpper.matches("[A-H][0-7]")) {
throw new IllegalArgumentException("Invalid position");
}
// can be done in one statement, but we're not in a hurry
char xc = chessNotationUpper.charAt(0);
// chars are also numbers, so we can just use subtraction with another char
x = xc - 'A';
char yc = chessNotation.charAt(1);
// note that before calculation, they are converted to int by Java
y = yc - '1';
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public String getChessNotation() {
// perform the reverse and create a string out of it
// using StringBuilder#append(char c) is another option, but this is easier
return new String(new char[] {
// Java converts 'A' to int first (value 0x00000041)
(char) (x + 'A'),
(char) (y + '1')
});
}
// this will be helpfully displayed in your debugger, so implement toString()!
#Override
public String toString() {
return getChessNotation();
}
}
Now probably you want to also create a Board class with a backing 2D array of ChessPiece[][] and perform board.set(WHITE_KING, new ChessPosition("E1")) or something similar.

Debugging Game of Life, Java

I am trying to reproduce the Game of Life but I've a bug. Cells are born according to design, but they don't die. This confuses me because my strategy for killing cells is the same as for giving birth to them. Here is a segment of the console output, 'x' represents living cells, '-' represents dead cells.
---------
---------
---------
---xx----
----x----
----x----
----xx---
---------
---------
---------
---------
---------
---xx----
----xx---
---xx----
----xx---
---------
---------
---------
---------
---------
---xxx---
----xx---
---xx----
---xxx---
---------
---------
And the relevant piece of code:
public class Life {
final static int WIDTH = 9, HEIGHT = 9;
void start(){
// scanning input file
char[][][] board = new char[WIDTH][HEIGHT][maxAllowedGenerations];
board = getInitialBoard(initialBoardString, maxAllowedGenerations, board);
for (int generation = 1; generation < maxAllowedGenerations; generation++){
for (int y = 0; y < HEIGHT; y++)
for (int x = 0; x < WIDTH; x++){
int numberOfNeighbours = getNumberOfNeighbours(x, y, generation - 1 , board);
if (board[x][y][generation - 1] == '-' && numberOfNeighbours == 3)
board[x][y][generation] = 'x';
else if (board[x][y][generation - 1] == 'x' && numberOfNeighbours < 2)
board[x][y][generation] = '-';
else board[x][y][generation] = board[x][y][generation - 1];
if (board[x][y][generation] == 'x')
ui.place(x, y, LifeUserInterface.ALIVE);
else
ui.place(x, y, LifeUserInterface.DEAD);
out.print(board[x][y][generation]);
}
out.println();
}
}
out.println("Max number of generations reached");
System.exit(0);
}
I agree with #elyashiv - if you change char[][][] board to SomeEnum[][][] board, with SomeEnum defined with values LIVE_CELL and DEAD_CELL that would make things much more readable.
Also, there is no such thing as an empty character ''. An empty String is simply a String with zero length (ie no characters), but '' makes no sense. You could use null, but then you'd have to move away from the primitive char declaration and use Character instead since primitives can't be null.
That said, much better to use enums to represent the data. If you want, you can even make your enum look like this so you can represent your X and empty characters like so:
public enum SomeEnum {
LIVE_CELL("X"),
DEAD_CELL("");
public final displayString;
SomeEnum(String displayString) {
this.displayString = displayString;
}
}
Then for your display you could reference SomeEnum.LIVE_CELL.displayString in your code
Found two bugs! One of them was impossible for you to spot because I didn't post the code in which it was contained: I am a cell at [x][y][g]. I was considering [x][y][g - 1] to be a neighbour, but that is of course me! I am not my own neighbour.
The other bug was a bit embarrasing actually. I had left out rule number 2... >.<
I also realize I should have posted the rules of the Game of Life instead of assuming that you all know them or that you would bother researching them. It's a bit late now of course, but I'll post them anyway in case you are interested. Also, I really reccomend the wiki article for anyone interested in self-organization.
Rules:
Live cells with < 2 live neighbours die, as if by loneliness.
Live cells with > 3 live neighbours die, as if by overpopulation.
Live cells with 2 || 3 live neighbours survive to the next generation.
Dead cells with 3 live neighbours are revived, as if by reproduction.
Thank you for all input!

If objects (x,y) are near other objects (x,y)

I have two separate objects in Java, Object1 and Object2, both are the same size and square. For each object I can get the x and y coordinates. What I need to do is check if Object1 is within a certain distance of Object2. That distance is within 32 points on both the X and Y axis.
once the condition has been met then I can run my code. e.g.
if ( check condition ) {
//my code here
}
Try the following:
if(Math.abs(Object1.x - Object2.x) <= 32 && Math.abs(Object1.y - Object2.y) <= 32)
{
// Do stuff
}
If these are rectangles and not points, you need to compensate for their width and height. Since they're squares, just use their width if you have it
if(Math.abs(Object1.X - Object2.X) <= 32 + Object1.Width && Math.abs(Object1.Y - Object2.Y) <= 32 + Object1.Width)
{
// my code here
}

Snake game, How to make a snake move?

I am writing a snake game, specifically, is a centipede game. It needs me to draw a snake and that snake will automatically move one line by one line.
I did draw a snake, and it can move from left side to right side. However, the problem is:
I can't make the snake changes line, if it finish the first line, I need it changes to the second line and which starts from the right side.
My code is like this:
private void move()
{
myCentipedes[0] =
new Centipede(Settings.centipedeStartSize, Settings.RIGHT,
Settings.DOWN);
myCentipedes[0].segments = new Point[Settings.centipedeStartSize];
myCentipedes[0].segments[0] = new Point(0, 0);
boolean dr = true;
if (dr == true) {
if (myCentipedes[0].segments[0].x < 30) {
System.out.println(myCentipedes[0].segments[0].x +
" " +
myCentipedes[0].segments[0].y);
myCentipedes[0].segments[0] = new Point(x, 0);
for (int i = 1; i < 10; i++) {
myCentipedes[0].segments[i] =
new Point(myCentipedes[0].segments[i - 1].x - 1,
myCentipedes[0].segments[i - 1].y);
}
x++;
}
}
if (myCentipedes[0].segments[0].x == 29) {
x = 29;
dr = false;
}
if (dr == false) {
if (myCentipedes[0].segments[0].x > 0) {
myCentipedes[0].segments[0] = new Point(x, 1);
for (int i = 1; i < 10; i++) {
myCentipedes[0].segments[i] =
new Point(myCentipedes[0].segments[i - 1].x + 1, 1);
}
x--;
}
}
}
It appears to me that you re-create your entire centipede on every single move:
private void move()
{
myCentipedes[0] =
new Centipede(Settings.centipedeStartSize, Settings.RIGHT,
Settings.DOWN);
Is re-creating the centipede every move() intentional? Or should move() run the centipede entirely down the board, from start to finish? (If so, you'll need to add some looping to this method.)
I assume the myCentipedes[0] is simply a placeholder for future extensions, involving two or more centipedes on the board simultaneously. This sort of over-generic programming can sometimes make the code more difficult to read and write while initially programming, and almost certainly doesn't help matters. You can always re-factor a move() method that works on one centipede to a move(int centipede) method that works on a specific centipede and a move() method that calls move(int) for every centipede on the board. Or maybe you'll find it easier to place the movement code into the Centipede class, and need to remove the array indexes then and use class member storage instead.
boolean dr = true;
if (dr == true) {
dr will always equal true at this point. You might as well remove the variable and the test.
for (int i = 1; i < 10; i++) {
myCentipedes[0].segments[i] =
new Point(myCentipedes[0].segments[i - 1].x - 1,
myCentipedes[0].segments[i - 1].y);
}
Since you're counting up, you'll actually copy the value from segment[0] through to all elements in the array, one element at a time. Can't you just assign the Point objects new array indexes? Starting from i=centipede.segments.length and counting down, it'll look more like this:
for (int i=myCentipede[0].segments.length; i > 0; i--) {
myCentipede[0].segments[i] = myCentipede[0].segments[i-1];
}
myCentipede[0].segments[0] = new Point(...,...);
Some of your tests can be simplified:
if (myCentipedes[0].segments[0].x == 29) {
x = 29;
dr = false;
}
if (dr == false) {
if (myCentipedes[0].segments[0].x > 0) {
If dr == false at this point, you might as well have written it like this instead:
if (myCentipedes[0].segments[0].x == 29) {
x = 29;
if (myCentipedes[0].segments[0].x > 0) {
But then the second if is obviously not needed -- after all, 29 > 0.
While you're here, clean up all those hard-coded 10 with either a constant (Settings.centipedeStartSize) or find the actual length of the centipede (myCentipedes[0].segments.length).
Now that I've critiqued your current approach, I'd like to suggest a different tack:
Take a step back and break your problem down into smaller methods.
You've embedded two for loops that move the centipede one segment at a time by assigning to segment[i] the values from segment[i-1]. Instead of duplicating the code, write a new method with the body of the for loop to move the centipede forward. Make it take a Point object for the new first element each trip through the function. (Don't forget to make it count down rather than up.)
Once you've broken apart the for loops, I think it will be easier to make whatever changes are necessary for traveling left-to-right and right-to-left. You will probably want to write it with nested for loops -- one to control the vertical dimension, and within it, perhaps one or two new for loops to control the horizontal dimension. Make these loops work with a simple Centipede c, rather than the complicated expression you've currently got.
Breaking apart the larger function into smaller function will give you a better opportunity to test your functions in isolation -- test movement manually, with simple test methods like this:
move_forward(Centipede c, Point p) {
/* code to move forward one space to occupy `p` */
}
test_right() {
Centipede c = new Centipede(/* ... */);
move_forward(c, new Point(0,0));
move_forward(c, new Point(1,0));
move_forward(c, new Point(2,0));
move_forward(c, new Point(3,0));
move_forward(c, new Point(4,0));
move_forward(c, new Point(5,0));
/* ... */
}
Take it slow, test every method as you write them, and I think you'll find this is an easier problem than it currently looks.

Efficiency problem

i am trying to make my algorithm more efficient but for some reason its not working correctly could someone tell me if my logic is correct. The general problem is that if u have a height of 'x', and you can jump 'u' distance but you fall 'd' distance if you havent cleared the height already. i have to calculate the number of jumps.
Initial code works correctly
while(x-u>0) {
x=x-u+d;
i++;
}
i++;
more efficient code (for some reason fails some cases, I don't know which cases though)
int k=u-d;
if(x-u<=0){
i++;
} else {
int z=x/k;
if (x-((z-1)*k)-u <= 0) {
i+=z;
} else {
i=i+z+1;
}
}
let me try and clarify the problem you have a wall of height X, you can jump up distance U but every time you jump you also slip down distance D.
so lets say if u have a wall of height x=4, u=4, d=1. Then you would only have to jump once because the first time you jump you have cleared the wall, so you dont slip down at all. now lets say x=6, u=4,d=1. Then you would have to jump twice because the first time you would jump up to 4 but fall 1 so you are at 3 then the next jump you clear the wall.
Okay, let's see. The last jump comes from the height of x - u or higher. The rest you have to cover in (u - d)-size steps, the number of such steps is of course (x - u)/(u - d).
After i-th step you are at height i * (u - d) + u (and falling down). So, in approx. (x - u)/(u - d) steps you are at height x - u + u = x. Recalling that the number of steps should be a whole number, we get the final result:
if (u >= x)
return 1;
if (u <= d)
throw "Impossible";
return ceil((x - u)/(u - d));
(ceil is a mathematical function returning the smallest integer not less than the given number.)

Categories

Resources