Sdickie Sdickie - 5 months ago 15
Java Question

Checkers Game - Laying Pieces in 2D array (Java)

Hi I'm trying to build a checkers game and am having issues figuring out how to fill the 2D array game board with the individual checker pieces. Any help would be appreciated.

I have two separate classes RedPiece and BlackPiece that implement the interface Piece. In the additional class Board, I'm trying to setBoard() with the pieces on their sides but I'm running into an issue where the variable Piece can't be found when constructing a new Board:

private Board[][] b = new Board[][]{Piece};


This is what I have for SetBoard() so far:

public void setBoard(int row, int col, Piece a)
{
for (int x = 0; x < board.length; x++)
{
for (int y = 0; y < board[0].length; y++)
{
board[x][y] = Piece;
}
}
}


Any ideas? Thanks.

Answer Source

I think your constructer doesnt make sense. Why not try:

private Piece board[][];

This would be a two dimensional Array of Pieces.


The Piece-Variable ist called 'a' not 'Piece'

public void setBoard(int row, int col, Piece a)
{
    for (int x = 0; x < board.length; x++)
    {
        for (int y = 0; y < board[0].length; y++)
        {
            board[x][y] = a;
        }
    }
}