我已经按照维基百科文章上的伪代码,我想我得到了它的工作。但是,它会返回分数,当我想知道我想采取什么行动时,这并没有帮助。
我尝试了一种我认为是获得最佳移动的方法,但我认为它不起作用,因为当我真正尝试与它(国际象棋)下棋时,人工智能会做出一些深度级别为 3 的迟缓移动。
这是我的功能:
public static function alphaBeta(node, depth, alph, beta, team, tellTheMove:Boolean = false):* {
var pointer:ChessMove;
if (depth == 0) {
return scoreOf(node);
}
var childrenOf:Vector.<ChessMove > = returnPossibleMoves(node,team);
if (childrenOf.length == 0) {
return scoreOf(node);
}
if (team == 0) {
for (var i in childrenOf) {
var that:Number = alphaBeta(childrenOf[i],depth - 1,alph,beta,1);
if(tellTheMove){
}
if (that > alph) {
alph = that;
if(tellTheMove){
pointer = childrenOf[i];
}
}
if (beta <= alph) {
break;
}
}
if(tellTheMove){
return pointer; //Returns the move that's score last exceeded alpha.
}
return alph;
} else {
for (var j in childrenOf) {
var that2:Number = alphaBeta(childrenOf[j],depth - 1,alph,beta,0);
if (that2 < beta) {
beta = that2;
}
if (beta <= alph) {
break;
}
}
return beta;
}
}