hello everyone,
I am working on an assignment for my cs class. It is basically a board game with 20 levels.
i have a question about why this code in my program is not working...hopefully what I am posting makes any sense because what I am writing is already long...and psoting anymore might piss you all off...
i declared a 2d array:
public static int theBoardSize = 4;
Piece [][] tile = new Piece [theBoardSize][theBoardSize]
The Piece creates and displays a square of a particular size, color, and at an x & y position. I use 2 for loops to create the squares and each are stored in a single array element. the colors are randomly chosen by a random number generator.
theBoardSize is needed to increase the size of the board. So a 4X4 board is displayed for level 1 when the game begins, and when the user wins level 1, a 5x5 board is displayed an so on....until a 24x24 board is displayed.
If you are confused, here is an image if what the user would see when level 1 begins:
The game begins with tile[0][0] (in the image, it would be the blue square in the upper-left corner). The user types in a color and the computers job is to check if any surrounding squares (up, left, down and right. No diagonals...) are of that color. So if the user typed in the color black or green, no changes would be made to the board because there is no black square or green square surrounding tile[0][0]. If the user typed in red, the computer would notice that tile[0][1] square is red. tile[0][0] would also change to red. Here is an image showing the result.
The point of the game is to make the all the squares on the board the same color.
So far I have implemented this in my program.
In my Piece class, I have a portion of code:
The point of this method is for each square, determine the surrounding squares and store them.
private Piece theLeftSquare; // Reference to the left neighbor on the board.
private Piece theRightSquare; // Reference to the right neighbor on the board.
private Piece theTopSquare; // Reference to the top neighbor on the board.
private Piece theBottomSquare; // Reference to the bottom neighbore of the board.
/**
* Sets the left, upper, bottom, and right neighbor Pieces.
*/
void setTheNeighbors ( Piece left, Piece top, Piece right, Piece bottom )
{
theLeftSquare = left;
theTopSquare = top;
theRightSquare = right;
theBottomSquare = bottom;
}
I also have a PlayGame class with the following code:
public void setNeighbors()
{
for ( int row = 0; row < boardSize; row++ )
{
for ( int column = 0; column < boardSize; column++ )
{
tile[row][column].setTheNeighbors( tile[row][column - 1], tile[row - 1][column],
tile[row][column+1], tile[row + 1][column] );
}
}
}
However, when I run my code, I get an array out of bound exception. I understand that when row = 0 and column = 0, that tile[0][-1] does not exist. This is also the case for any square on the edge of the board.
I want the computer to notice that if it is checking for a square that is outside of the board range, then the value of the neighbor should be null.
So for example, using the above method and tile[0][0]:
tile[0][0].setTheNeighbors( tile[0][-1], tile[-1][0], tile[0][1], tile[1][0] );
would translate to:
tile[0][0].setTheNeighbors( null, null, tile[0][1], tile[1][0] );
I don't know if that makes any sense at all...or if I should make a seperate method that checks to see if the neighbor is outside the board range?
I'm not sure how to get around this problem...