I'm working on a game of checkers, if you want to read more about you can view it here; http://minnie.tuhs.org/I2P/Assessment/assig2.html
When I am doing my test to see if the player is able to get to a certain square on the grid (i.e. +1 +1, +1 -1 .etc) from it's current location, I get an java.lang.ArrayIndexOutOfBoundsException error.
This is the code I am using to make the move;
public static String makeMove(String move, int playerNumber)
{
// variables to contain the starting and destination coordinates, subtracting 1 to match array size
int colStart = move.charAt(1) - FIRSTCOLREF - 1;
int rowStart = move.charAt(0) - FIRSTROWREF - 1;
int colEnd = move.charAt(4) - FIRSTCOLREF - 1;
int rowEnd = move.charAt(3) - FIRSTROWREF - 1;
// variable to contain which player is which
char player, enemy;
if (playerNumber==1)
{
player= WHITEPIECE;
enemy= BLACKPIECE;
}
else
{
player= BLACKPIECE;
enemy= WHITEPIECE;
}
// check that the starting square contains a player piece
if (grid [ colStart ] [ rowStart ] == player)
{
// check that the player is making a diagonal move
if (grid [ colEnd ] [ rowEnd ] == grid [ (colStart++) ] [ (rowEnd++) ] &&
grid [ colEnd ] [ rowEnd ] == grid [ (colStart--) ] [ (rowEnd++) ] &&
grid [ colEnd ] [ rowEnd ] == grid [ (colStart++) ] [ (rowEnd--) ] &&
grid [ colEnd ] [ rowEnd ] == grid [ (colStart--) ] [ (rowEnd--) ])
{
// check that the destination square is free
if (grid [ colEnd ] [ rowEnd ] == BLANK)
{
grid [ colStart ] [ rowStart ] = BLANK;
grid [ colEnd ] [ rowEnd ] = player;
}
}
// check if player is jumping over a piece
else if (grid [ colEnd ] [ rowEnd ] == grid [ (colStart+2) ] [ (rowEnd+2) ] &&
grid [ colEnd ] [ rowEnd ] == grid [ (colStart-2) ] [ (rowEnd+2) ] &&
grid [ colEnd ] [ rowEnd ] == grid [ (colStart+2) ] [ (rowEnd-2) ] &&
grid [ colEnd ] [ rowEnd ] == grid [ (colStart-2) ] [ (rowEnd-2) ])
{
// check that the piece in between contains an enemy
if ((grid [ (colStart++) ] [ (rowEnd++) ] == enemy ) &&
(grid [ (colStart--) ] [ (rowEnd++) ] == enemy ) &&
(grid [ (colStart++) ] [ (rowEnd--) ] == enemy ) &&
(grid [ (colStart--) ] [ (rowEnd--) ] == enemy ))
{
// check that the destination is free
if (grid [ colEnd ] [ rowEnd ] == BLANK)
{
grid [ colStart ] [ rowStart ] = BLANK;
grid [ colEnd ] [ rowEnd ] = player;
}
}
}
}
I'm not sure how I can prevent the error from happening, what do you recommend?