Chess Program Depth Search
in
Programming Questions
•
1 year ago
I am making my second Chess AI and at the moment it is not really an AI, it just looks at all the possible moves for the current turn and sees if Checkmate is a possible option (if it is it plays it otherwise it plays a random possible move). My first one was too slow to be able to plan into the future so I didn't have to worry about depth search. This one is much faster and I would like it to be able to test for Checkmate for "n" depth where "n" is the number of moves into the future.
The Chess environment code is very long so I didn't post it but below is the logic for finding Checkmate for the current position if it is an option and the AI is playing. I realize that not posting the entire code probably makes understanding what is going on quite difficult so if anyone could just post a general idea of how to structure searching all possible moves in the future for Checkmate that would be great (jbum if you are reading this I'd really appreciate some help). There are a few functions that are being called within the for loop:
ai() is the code I posted below. It is called during the AI's turn. It contains the functions below:
aiDo() executes the move from the for loop
aiUndo() undoes the move from the for loop
attackedSquares() determines which squares on the board are attacked
possibleMoveList() builds a list of all the possible moves at some depth (and also removes illegal moves that leave the King in check)
Some other helpful things to point out would be that bTurn means Black's turn. If it is true it is Black's turn, otherwise it is White's turn. isAttacked is a boolean array that is 64 elements long (one element per board square). If an element is true that square is attacked otherwise it is not. kingSquare is a global integer that is set to the opponent's King's board position during attackedSquares(). I use it to determine if the King is attacked. If it is and there are no possible move responses for the opponent it is Checkmate.
void ai() {
boolean somethingWasSelected = false;
int bestVal = 0;
int selectedMove = 0;
// Run through all moves
for (int i = 0; i < numPossible[0]; i++) { // numPossible[0] means the current turn (The 0 means current turn)
int currVal = 0;
int undo = board[possibleTarget[0][i]]; // Again, the 0 means current turn. This stores a piece that might have been captured by the function below
aiDo(0, i); // The 0 is current turn, the i is the current move
bTurn = !bTurn; // Flip the current player's turn so attackedSquares sees what would be attacked
attackedSquares(); // Get my list of attacked squares
if (isAttacked[kingSquare] == true) { // If the King is attacked
possibleMoveList(1); // Generate a list of all possible responses 1 turn into the future
if (numPossible[1] == 0) currVal += 1000000; // If there are no possible moves for the opponent it is Checkmate!
}
bTurn = !bTurn; // Unflip the current player's turn
if (currVal > bestVal) {
somethingWasSelected = true;
selectedMove = i; // Remember the best move (only remembered if Checkmate was an option)
bestVal = currVal;
}
aiUndo(0, i); // Undo the move i at depth 0
board[possibleTarget[0][i]] = undo; // Put back any piece that was removed by doing the move at the beginning of the for loop
}
// Do the move that scored best
// If nothing was selected then randomly select a move
if (somethingWasSelected == false) selectedMove = int(random(numPossible[0]));
aiDo(0, selectedMove);
}
1