I want to make a 2 player checkers game. I am starting in text but i want to add graphics later. I have 3 classes so far piece board and player. I represent the board with a 2d int array and i was thinking then that a red piece is a 3 and a black piece is a 4 and the empty spots that are movable are 1's and the other ones are 2. So then to add graphics i just go through the game board and draw whatever needs to be drawn based on if its a 1, 2, 3, or 4. I have the board set up and the pieces on the board. I am having trouble moving them around though. My player class gets an array of pieces. The color of these pieces depends on if the person is black or red. I need a move function that looks 1 diagnol from the current x and y and detects a collision or not. If no collision it would move the index in the piece array from the old position to the new one. I get an array index out of bounds. Basically i think i don't know which index of the pieces array i would pass it. Here is how i initially set the red pieces on the board.
// check for collision, if no collision set the piece
static void setRedPieces(int xPos, int yPos, Board b)
{
// loop through row
for (int row = 0; row < 3; row++)
{
// loop through col
for (int col = 0; col < 8; col++)
{
// loop through red pieces
for (int i = 0; i < 12; i++)
{
pieces[i].setXPos(col);
pieces[i].setYPos(row);
if (!checkCollision(pieces[i].getXPos(), pieces[i].getYPos(), b))
b.getBoard()[pieces[i].getYPos()][pieces[i].getXPos()] = 3;
} // end for
} // end for
} // end for
} // end setPiece
So can i assume that from 0,0 in the game board array is 0 in the pieces array and so on like that? If so i have my move method:
// move a red piece
void moveRed(int pieceToMove, Board b)
{
if (!checkCollision(pieces[pieceToMove].getXPos()+1, pieces[pieceToMove].getYPos()+1, b))
{
b.getBoard()[pieces[pieceToMove].getYPos()][pieces[pieceToMove].getXPos()] = 1;
// gameBoard[yPos][xPos] = 3;
b.getBoard()[pieces[pieceToMove].getYPos()+1][pieces[pieceToMove].getXPos()+1] = 3;
} // end if
} // end moveRed
I get an array index out of bounds exception around the check collision part. I pass it in 1 which should not be allowed to move because it should be the 2nd piece on the board if i am thinking through this correctly but i should not get the arrayIndexOutOfBounds exception. If i need to post more code i can. Thanks.