首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Minimax:保存单板的副本以进行回溯

Minimax:保存单板的副本以进行回溯
EN

Stack Overflow用户
提问于 2017-05-10 17:56:48
回答 1查看 231关注 0票数 0

我正在尝试实现一个Minimax (带有alpha beta剪枝。我现在的问题是,如果我评估一个位置并回溯到迭代中的下一步(上一级),那么"currentBoard“不是初始板,而是来自评估叶的板,即使makeMove和removeFigure都返回了一个新板。

那么我如何才能“保存”旧的板子以进行正确的回溯呢?

附言:我想使用复制而不是撤消移动,因为板是一个简单的hashmap,所以我想这样更容易。

以下是我到目前为止拥有的代码:

代码语言:javascript
运行
复制
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...]

}

EN

回答 1

Stack Overflow用户

发布于 2017-05-10 19:33:29

我想我找到了这样做的方法。至少它看起来是有效的。它们的关键是在for循环之后立即创建一个副本,并在以后使用该副本而不是currentBoard,这样循环的currentBoard就永远不会被修改。

代码语言:javascript
运行
复制
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 {
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43889121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档