I successfully implemented the minimax algorithm, and am looking for an improvement with Alpha-Beta.
Currently both algorithm's take an outrageously long time at a search depth of 3+, however minimax works, and Alpa-Beta is making really horrible moves. The algorithm below is based off the pseudocode found at the chess programming wiki.
Strength of current position
calculatePositionStrength() //currently very simple
return (My piece material - opponent's piece material)
Algorithm: chessjs.move(m) makes a move. chessjs.undo() undo's it.
var score = 0, bestMove;
function alphaBetaMax(alpha, beta, depthleft) {
if(depthleft==0) {
return calculatePositionStrength();
}
chessjs.moves().forEach(function(move) {
chessjs.move(move);
score = alphaBetaMin(alpha, beta, depthleft-1);
chessjs.undo();
if(score>=beta) {
return beta; // fail hard beta-cutoff
}
if(score>alpha) {
bestMove = move;
alpha = score; // alpha acts like max in MiniMax
}
});
return alpha;
}
function alphaBetaMin(alpha, beta, depthleft) {
if(depthleft==0) {
return -calculatePositionStrength();
}
chessjs.moves().forEach(function(move) {
chessjs.move(move);
score = alphaBetaMax(alpha, beta, depthleft-1);
chessjs.undo();
if(score<=alpha ) {
return alpha; // fail hard alpha-cutoff
}
if(score<beta ) {
beta = score; // beta acts like min in MiniMax
}
});
return beta;
}
alphaBetaMax(-Infinity, Infinity, 3);
What's wrong with my logic? Am I placing move making/undoing in the correct spots? Also, why is it so slow?