I wrote a little C++ program for playing Chess:
Now I check the valid moves by filling an 8x8 board with different flags called cBoard
.
fNONE
= none typecanPASS
= the piece can go therecannotPASS
= the piece is blockedcanEAT
= the piece can go there and will eat a piece
The Piece has 2 properties:
id
= ROOK,PAWN,...,NONEcolor
= BLACK or WHITE
void ChessMoves::fillcBoardPawn(int row, int col){
//reset cBoard
resetcBoard();
//WHITE PAWN
//get pieceMaster
Piece const& pieceMaster = refBoard.getPieceAt(row,col);
if(pieceMaster.getColor() == WHITE){
//check if it can go fw
// |r-1c-1|r-1|r-1c+1|
Piece actualPiece = refBoard.getPieceAt(row-1,col);
if(actualPiece.getId() != NONE){
cBoard[row-1][col] = cannotPASS;
}
else{
cBoard[row-1][col] = canPASS;
}
//check if can eat
actualPiece = refBoard.getPieceAt(row-1,col-1);
if(actualPiece.getId() != NONE){
if(actualPiece.getColor() != pieceMaster.getColor()){
//different color can eat
cBoard[row-1][col-1] = canEAT;
}
}
actualPiece = refBoard.getPieceAt(row-1,col+1);
if(actualPiece.getId() != NONE){
if(actualPiece.getColor() != pieceMaster.getColor()){
//different color can eat
cBoard[row-1][col+1] = canEAT;
}
}
if(row == 2){
//check if in initial position, then can make two jump
actualPiece = refBoard.getPieceAt(row-2, col);
if (actualPiece.getId() != NONE){
cBoard[row-2][col] = cannotPASS;
}
else{
cBoard[row-2][col] = canPASS;
}
}
}
//BLACK PAWN
//the black pawn has everything the same but is going down instead of up
}
This is the function that checks the moves of the pawn. As you can see, this is a really big function and I wrote only half of it. How could I write this function in a more concise way?