Moving Pieces Through the Board Array

The process of moving pieces throught the abstract board array has a pleasant simplicity to it that makes things seem to make sense in the midst of so many tiny words constrewed together to make a massive program such as this one. The board contains pieces each which have locations in its array that correspond to actual positions on the board itself. Therefore to move a piece we first move the piece to it's new square specified by an end rank and end file. Then, we delete the square it came from.

     board[eRank][eFile].setPieceProperties(board[sRank][sFile]);
     board[sRank][sFile].emptySqare();

Capturing and MovedYet

This simple code will work to move a piece. But now consider that you want to know if one piece is capturing another piece. This brings on the first instance of searching the board for a condition by using one of its constants. If there is a piece on the moving pieces destination square then it must be capturing. Now consider that some pieces, like pawns and kings, have restrictions after they have moved once. We must know when they have moved to make them play within the rules of the game. A pawn can only move two squares if it hasn't moved yet and a king can only castle if it hasn't moved yet. This means that every time a piece has moved we need to update it movedYet property if it is false, meaning that it hasn't moved yet. Now the code looks like this...

     if(board[eRank][eFile].getColor != Board.COLORLESS)
        capturing = true;
     board[eRank][eFile].setPieceProperties(board[iStartRow][iStartCol]);
    if(board[eRank][eFile].getMovedYet() == false)
         board[eRank][eFile].setMovedYet(true);
     board[sRank][sFile].emptySquare();

The Case of the Castling Move

The castling move presents a small problem. It is not compatable with the move function because two pieces are moving at once, and there is a condition on this move. That being that the king and rook can't have moved yet, while the others pieces blocking their path must have. One solution is to trap that special case in the move function by watching for horizontal king moves that are greater than two squares and analyzing them. This is the solution I have used in my program

    if(board[startRow][endRow].getKind == Board.KING){
        if(dX==2)
            DoCastlingMove(sFile, eFile, true)
        else if(dX==3)
            DoCastlingMove(sFile, eFile, false)

dX is the change in columns which must be calculated. The DoCastlingMove function needs to know if the player is castling kingside or queenside and therefore excepts true or false values. I challenge you to make a function for castling. Use the fact that you know where the king and rook must go, and that their must be nothing blocking the path between king and rook, nothing. But here is the DoMove function that can handle any move the board might encounter.

    public void DoMove(Move m){
        sRank = m.getsRank();
        sFile = m.getsFile();
        eRank = m.geteRank();
        eFile = m.geteFile();
        //check for capture
        if(board[sRank][sFile].getColor not equal Board.COLORLESS)
            capturing = true;
        int dX = Board.findTheDiff(sFile, eFile);
        if(board[sRank][sFile].getKind == Board.KING{
            if(dX==2)
                DoCastlingMove(startRow, endRow, true)
            else if(dX==3)
                DoCastlingMove(startRow, endRow, false)
        //set the movedYet
        if(not board[sRank][sFile].getMovedYet())
            board[sRank][sFile].setMovedYet(true);
    }

start