Class BOARD

The Core of the Board

What is the board? To some it may be a delicately crafted wooden surface covered with checkered squares, some of which contain carefully weighted wooden pieces. To others, it may be a red and black plastic board that you can fold in half and take with you to the beach for family picnics. And to others, it may be a world full of strategies, each square having distinct meaning, filled with an elegant history of opening moves and games played. But in all cases the board ends up being marked with 8 numbers along the ranks and eight letters along the files which reminds us that the board is 8x8, a real life array.

But what is it really, if you take away the elegance and the abstract meanings that have been given to it by the great imaginative minds that have taken interest in its maze of complexities and look at its core. If you imagine that the pieces are not seperate from the board but a part of it, because without the board they have no home in which to be played.

If you broke it down, I mean really broke it down to its core, wouldn't the board just be sixty-four squares, each of which could be either occupiecd by a piece or empty? And isn't that what the piece class represents, pieces and empty pieces? So the board in my chess program is an 8x8 array of piece objects. It's as simple as that. Go back and look at the piece class. Get the idea glued in your head, because those simple objects that seemed to do nothing but waste space now mean something. Sixty-four of them make the board in this game, and that's all there is to it.

    Piece board[8][8];

Functionality of the Board

It is one thing to know what the board is and another thing to know what the board does or should do. We know it's an 8x8 array of pieces, but the concept of the game goes further than that, and so does the concept of the board. The board should manage the pieces. Controlling who gets to move is a part of this. If it's black's turn to move then he should move a black piece. If he moves a white piece then the board is not managing the pieces correctly. So, before trying to write the codes for the board I decided to figure out what I wanted the board to do. Here is what I came up with:

  • Make moves
  • Takeback moves
  • Check Move Validity
  • Keep Track of Whose Moving
  • Make a Castling Move
  • Undo a Castling Move
  • Be Accessible to the Enitre Program

Notice, I've sepearated the move from the castling move. This is because the two are different since a castling move requires the checking of several conditions and the moving of two pieces at once in an abnormal way. The board should therefore know when a castling move is being made so that it may handle it properly.

Checking validity requires two things. First, to make sure the piece is moving within the specified rules of the game and second to check paths, that is, to make sure that a piece is not illegally moving over another piece. The path algorithm is the most complicated of the board. Making moves and undoing them simply requires an understanding of what a piece is and what it represents in the game. "A piece is a square". "The board is an array of pieces".

Accessibility means this. Everything in the game in some way relies on the board because it contains all information relating to positions, whose moving and so forth. Therefore, its array should be accessiblle everywhere. This is why I declare the array static, meaning it can be used anywhere in the program. This idea becomes very helpful when searching the board for checks comes into play.

The board has a subtle complexity about it, but after making and understanding it, it seems so simple. I even wondered, "what was so hard about that?".

Putting the Pieces on the Board

Setting up a new board for play is a surprisingly simple task. We first clear the board, just in case it's not clear already, and then put the pieces in their respective places. Keep in mind, on our board the first dimension represents ranks and the second files. Here we use extensively the setPiece() function of the piece class to make the board for the first time be full of its pieces.

public void setUpNewBoard(){

    clearBoard(); //clear the board
    playerToMove = false; //reset the player to move property

    //place the black pieces on the board
    board[0][0].setPiece(BLACK, ROOK, false);
    board[0][7].setPiece(BLACK, ROOK, false);
    board[0][6].setPiece(BLACK, KNIGHT, false);
    board[0][1].setPiece(BLACK, KNIGHT, false);
    board[0][5].setPiece(BLACK, BISHOP, false);
    board[0][2].setPiece(BLACK, BISHOP, false);
    board[0][3].setPiece(BLACK, KING, false);
    board[0][4].setPiece(BLACK, QUEEN, false);

    //place the white pieces on the board
    board[7][0].setPiece(WHITE, ROOK, false);
    board[7][7].setPiece(WHITE, ROOK, false);
    board[7][6].setPiece(WHITE, KNIGHT, false);
    board[7][1].setPiece(WHITE, KNIGHT, false);
    board[7][5].setPiece(WHITE, BISHOP, false);
    board[7][2].setPiece(WHITE, BISHOP, false);
    board[7][3].setPiece(WHITE, KING, false);
    board[7][4].setPiece(WHITE, QUEEN, false);
    //pawns
    for(int i=0; i<8; i++){
        board[1][i].setPiece(PAWN, BLACK, false);
        board[6][i].setPiece(PAWN, WHITE, false);
    }
}