我正在尝试实现Minimax(使用alpha beta修剪。我现在的问题是,如果我评估一个位置并回溯到迭代中的下一步(一级),“currentBoard”不是初始板但是即使makeMove和removeFigure都返回一个新的板,也是来自评估叶的那个。
那么如何“保存”旧电路板以进行正确的回溯?
P.s:我想使用复制而不是撤消移动,因为该板是一个简单的hashmap,所以我想这样更容易。
这是我到目前为止的代码:
public int alphaBeta(Board currentBoard, int depth, int alpha, int beta, boolean maximisingPlayer) {
int score;
if (depth == 0) {
return Evaluator.evaluateLeaf(whichColorAmI, currentBoard);
}
else if (maximisingPlayer) {
ArrayList<Move> possibleMoves= new ArrayList<Move>();
possibleMoves=getPossibleMoves(whichColorAmI, currentBoard);
for (Move iterMoveForMe : possibleMoves) {
if(currentBoard.figureAt(iterMoveForMe.to)!=null){
currentBoard = currentBoard.removeFigure(iterMoveForMe.to);
}
currentBoard= currentBoard.moveFigure(iterMoveForMe.from, iterMoveForMe.to);
score = alphaBeta(currentBoard, depth-1, alpha, beta, false);
if(score>=alpha){
alpha=score;
if(depth==initialDepth){
moveToMake=iterMoveForMe;
}
}
if (alpha>=beta) {
break;
}
}
return alpha;
}
else {[Minimizer...]
}
答案 0 :(得分:0)
我想我找到了一种方法来做到这一点。至少它似乎工作。它们的关键是在for循环之后立即复制并稍后使用此副本而不是currentBoard,因此循环的currentBoard永远不会被修改。
public int alphaBeta(Board currentBoard, int depth, int alpha, int beta, boolean maximisingPlayer) {
Display dis = new ConsoleDisplay();
int score;
if (depth == 0) {
int evaluatedScore = Evaluator.evaluateLeaf(whichColorAmI, currentBoard);
return evaluatedScore;
}
else if (maximisingPlayer) {
ArrayList<Move> possibleMoves= new ArrayList<Move>();
possibleMoves=getPossibleMoves(whichColorAmI, currentBoard);
for (Move iterMoveForMe : possibleMoves) {
Board copy = new Board(currentBoard.height, currentBoard.width,currentBoard.figures());
if(copy.figureAt(iterMoveForMe.to)!=null){
copy = currentBoard.removeFigure(iterMoveForMe.to);
}
copy= copy.moveFigure(iterMoveForMe.from, iterMoveForMe.to);
score = alphaBeta(copy, depth-1, alpha, beta, false);
if(score>=alpha){
alpha=score;
if(depth==maxDepth){
moveToMake=iterMoveForMe;
}
}
if (alpha>=beta) {
break;
}
}
return alpha;
}
else {