使用minimax算法的Java tic tac toe问题

问题描述 投票:2回答:1

我想为tic tac toe实现MiniMax算法。我有两个方法min()和max()和一个评估方法,但它不起作用。例如,当我打电话

max(9);
Field[bestCol][bestRow]='O';
min(8);
Field[bestCol][bestRow]='X';     

在主函数中结果是

OX-                                  
---
---

但是玩家'X'的最佳动作是将'X'放在中间位置。

这是我的代码没有评估方法:

static char[][] Field = { { '-', '-', '-' },
                          { '-', '-', '-' },
                          { '-', '-', '-' } };

static char Player = 'O';
static char Computer = 'X';

static int Depth =9; // searchdepth
static int bestRow=0, bestCol=0; // best Move

public static int max(int depth) {
    if (depth == 0) {
        return evaluateMove();
    }

    int maxValue = Integer.MIN_VALUE;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (Field[i][j] == '-') {
                Field[i][j] = Computer;

                int value = min(depth - 1);
                Field[i][j] ='-';

                if (value > maxValue) {
                    maxValue = value;
                    if (depth == Depth) {
                        bestCol=i;
                        bestRow=j;
                    }
                }
            }
        }
    }
    return maxValue;
}

public static int min(int depth) {
    int minValue = Integer.MAX_VALUE;

    if (depth == 0) {
        return evaluateMove();
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (Field[i][j] == '-') {
                Field[i][j] = Player;
                int value = max(depth - 1);
                Field[i][j] = '-';

                if (value < minValue) {
                    minValue = value;
                    bestCol=i;
                    bestRow=j;
                }
            }
        }
    }
    return minValue;
}

最好的祝福

编辑:

感谢您的回答。第一点我忘记将'*'更改为' - '这是我的评估方法:

public static int evaluateMove() {
    for(int i=0; i<3; i++) {
        int countX=0; int countY=0;
        for(int j=0; j<3; j++) {
            if(Feld[i][j]==Computer) countX++;
            if(Feld[i][j]==Player) countY++;
        }
        if(countX==3) return 10;
        if(countY==3) return -10;
    }

    for(int j=0; j<3; j++) { // Spalten
        int countX=0; int countY=0;
        for(int i=0; i<3; i++) {
            if(Feld[i][j]==Computer) countX++;
            if(Feld[i][j]==Player) countY++;
            if(countX==3) return 10;
            if(countY==3) return -10;
        }
    }
    return 0; // Unentschieden
}
java algorithm minmax
1个回答
0
投票

有些事让我印象深刻:

  1. 你用' - '初始化比赛场的空方格,但在min / max-functions中你假设'*'是一个空方格
  2. 在min-function中,与max-function相反,你在每个级别而不是顶级设置最佳移动,所以更深层次将覆盖顶层的最佳结果(所以我认为你还应该检查深度==深度)
  3. 我不知道你在main中做了什么,但也应该在每次移动后减少“深度”,因为这定义了顶级(因此应该分配最佳移动的级别),并且根据你的问题,你减少了每次通话期间的“深度”参数
  4. 我想你知道你只会在游戏结束前一直得到最佳结果(这是在每次移动后递减深度和深度的另一个参数)
  5. 您没有检查evaluateMove函数中的对角线
  6. 在evaluateMove的第二个双循环中,你检查最里面循环内的countX,countY的条件(有效,但令人恼火的是,它与第一个双循环不同=>不太适合查找错误)

编辑:最后(鼓滚......):

  1. 在第一步中,您最大化增益(对于计算机移动('X')),但您实际上执行了移动('O')。对于第二次移动反之亦然。但是,您必须最小化第一步中的增益(这意味着玩家获胜)并在第二步中最大化。

那就是你实际做的事情:

    public static void ComputeAndExecuteBestMove()
    {
        // since Player begins, we minimize the gain value for the first move
        if ((MaxDepth-Depth) % 2 == 0) 
        {
            max(Depth);
            Field[bestCol,bestRow] = Player;
        }
        else 
        {
            min(Depth);
            Field[bestCol,bestRow] = Computer;
        }

        // next move
        Depth--;
    }

但你应该做什么:

    public static void ComputeAndExecuteBestMove()
    {
        // since Player begins, we minimize the gain value for the first move
        if ((MaxDepth-Depth) % 2 == 0) 
        {
            min(Depth);
            Field[bestCol,bestRow] = Player;
        }
        else 
        {
            max(Depth);
            Field[bestCol,bestRow] = Computer;
        }

        // next move
        Depth--;
    }
© www.soinside.com 2019 - 2024. All rights reserved.