我正在尝试实现一个Minimax (带有alpha beta剪枝。我现在的问题是,如果我评估一个位置并回溯到迭代中的下一步(上一级),那么"currentBoard“不是初始板,而是来自评估叶的板,即使makeMove和removeFigure都返回了一个新板。
那么我如何才能“保存”旧的板子以进行正确的回溯呢?
附言:我想使用复制而不是撤消移动,因为板是一个简单的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...]
}
发布于 2017-05-10 19:33:29
我想我找到了这样做的方法。至少它看起来是有效的。它们的关键是在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 {
https://stackoverflow.com/questions/43889121
复制相似问题