首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MiniMax reversi实现

MiniMax reversi实现
EN

Stack Overflow用户
提问于 2013-12-06 17:40:20
回答 1查看 978关注 0票数 0

我试图在Reversi/Othello游戏中实现一个MiniMax算法,而且我被困住了,因为我编写的函数看起来非常正常,但是我得到了一些奇怪的动作,几次之后就崩溃了。下面是找出最佳动作的函数:

代码语言:javascript
复制
public Field findBestMove(GameBoard gb, int depth, int player) 
{       
    if(depth >= max_depth) return null;
    ArrayList <Field> moves = findAllPossibleMoves(gb, player);
    Field best_move = null;
    /** iterating over all possible moves, to find the best one */

    for (int i=0; i<moves.size(); i++)
    {
        /* board to simulate moves */
        GameBoard temp_board = new GameBoard(gb);
        Field move = moves.get(i);
        game.move(move, temp_board, player);

        int opposite_player = player == GameBoard.WHITE ? GameBoard.BLACK : GameBoard.WHITE;
        Field best_deep_move = findBestMove (temp_board, depth + 1, opposite_player);
        /** if the maximum depth is reached, we have a null, so we evaluate */
        if (best_deep_move == null)
        {
            /** we rate it according to the evaluation table */
            move.setRating(evaluate (temp_board, player));
        }
        else
        {
            move.setRating(best_deep_move.getRating());
        }

        if(best_move == null)
        {
            best_move = move;
        }
        else
        {   
            if (depth%2==0)
            {
                /** for us, we look for the maximum */
                if (best_move.getRating() < move.getRating()) best_move = move;
            }
            else
            {
                /** for the opponent, we look for the minimum */
                if (best_move.getRating() > move.getRating()) best_move = move;
            }
        }
    }
    return best_move;
}

每次移动后,活动玩家将被更改。在onTouchEvent的GameView方法中,玩家先移动,然后将玩家变成白色的,即AI,然后移动。它应该在他的树中寻找最好的移动,然后执行一个移动,而不是他做了几个奇怪的动作。我不知道为什么,对于每个分支,我创建一个新的副本板,所以我不知道为什么主游戏板会被修改。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-06 18:15:42

如果更改对象的副本会影响原始对象。那就是“浅薄的复制品”。这意味着数据结构中的对象是共享的。你想要一份“深版”。

向我们展示new GameBoard(gb)的代码

一些optinos:您可以为您的游戏板和它包含的所有对象(以及它们包含的对象)实现克隆。或者,在你的棋盘中实现一个撤销()函数。只要你撤销你的棋盘中的每一个动作,你就可以在它上执行移动。这将节省计算期间进行测试移动时的内存和对象创建开销。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20430194

复制
相关文章

相似问题

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