A Chess Game Engine:
Minimax Algorithm
Moksh Jain, 16IT221
Mahim Agrawal, 16IT152
Nishanth Hebbar, 16IT234
Manas Kinkar, 16IT226
Chess
Chess is a two-player strategy board game played on a chessboard.
The objective is to checkmate the opponent's king by placing it under an inescapable threat of capture. To this end, a player's pieces are used to attack and capture the opponent's pieces, while supporting each other. In addition to checkmate, the game can be won by voluntary resignation of the opponent, which typically occurs when too much material is lost or checkmate appears inevitable. There are also several ways a game can end in a draw.

Minimax
Minimax is a decision rule used in game theory for minimizing the possible loss for a worst case (maximum loss) scenario.
The minimax algorithm performs a complete depth-first exploration of the game tree.
If the maximum depth of the tree is m and there are b legal moves at each point, then the time complexity of the minimax algorithm is O(b^m).
Pseudocode
function minimax(node, depth, maximizingPlayer)
if depth = 0 or node is a terminal node
return the heuristic value of node
if maximizingPlayer
bestValue := −∞
for each child of node
v := minimax(child, depth − 1, FALSE)
bestValue := max(bestValue, v)
return bestValue
else (* minimizing player *)
bestValue := +∞
for each child of node
v := minimax(child, depth − 1, TRUE)
bestValue := min(bestValue, v)
return bestValue
Example
Alpha Beta Pruning
Alpha–beta pruning is a search algorithm that seeks to decrease the number of nodes that are evaluated by the minimax algorithm in its search tree.
It returns the same move as minimax would, but prunes away branches that cannot possibly influence the final decision.
Alpha–beta pruning can be applied to trees of any depth, and it is often possible to prune entire subtrees rather than just leaves.
Pseudocode
function alphabeta(node, depth, α, β, maximizingPlayer)
if depth = 0 or node is a terminal node
return the heuristic value of node
if maximizingPlayer
v := -∞
for each child of node
v := max(v, alphabeta(child, depth – 1, α, β, FALSE))
α := max(α, v)
if β ≤ α
break (* β cut-off *)
return v
else
v := +∞
for each child of node
v := min(v, alphabeta(child, depth – 1, α, β, TRUE))
β := min(β, v)
if β ≤ α
break (* α cut-off *)
return v
Example
Chess Engine Using Minimax
function (depth, game, alpha, beta, isMaximisingPlayer) {
positionCount++;
if (depth === 0) {
return -evaluateBoard(game.board());
}
var newGameMoves = game.moves();
if (isMaximisingPlayer) {
var bestMove = -9999;
for (var i = 0; i < newGameMoves.length; i++) {
game.move(newGameMoves[i]);
bestMove = Math.max(bestMove, minimax(depth - 1, game, alpha, beta, !isMaximisingPlayer));
game.undo();
alpha = Math.max(alpha, bestMove);
if (beta <= alpha) {
return bestMove;
}
}
return bestMove;
} else {
var bestMove = 9999;
for (var i = 0; i < newGameMoves.length; i++) {
game.move(newGameMoves[i]);
bestMove = Math.min(bestMove, minimax(depth - 1, game, alpha, beta, !isMaximisingPlayer));
game.undo();
beta = Math.min(beta, bestMove);
if (beta <= alpha) {
return bestMove;
}
}
return bestMove;
}
};
Observations
Alpha-Beta Pruning cuts down the search space by a factor of ~10
Despite this, the algorithms searches around 10,000 - 100,000 nodes for a move.
The algorithm does not make mistakes, but cannot plan strategically.
Future Work
Implement move-ordering (sort child states in increasing order of value)
Implement faster move generation
Implement other pruning strategies such as: AEL Pruning, Delta Pruning, Null Pruning
Implement a better evaluation function
References
https://chessprogramming.wikispaces.com/Minimax
https://chessprogramming.wikispaces.com/Alpha-Beta
https://github.com/jhlywa/chess.js/
http://chessboardjs.com/
Artificial Intelligence: A Modern Approach: Stuart Russell, Peter Norvig
The code for our project is available on Github: https://github.com/MJ10/Minimax-Chess-AI
deck
By Moksh Jain
deck
- 181