【问题标题】:Minimax in Java not workingJava中的Minimax不起作用
【发布时间】:2016-02-27 19:05:19
【问题描述】:

我正在尝试使我的 minimax 算法适用于 8x8 Gomoku 棋盘,我必须连续匹配 5 个/col/diagonal 才能获胜!我的算法似乎没有正常工作,我无法确定哪里出了问题!

我的 generateMoves() 方法工作正常,它生成所有合法的移动,所以我知道不是这样。 minimax 返回 -1,-1 作为最佳移动,但随后当然会引发非法移动错误,因为您不能在从 0,0 到 7,7 的棋盘上使用 -1,-1。

感谢任何帮助。谢谢。

 private int[] minimax(int depth, Color [][] board) {
          List<int[]> nextMoves = generateMoves(board);

          int bestScore = (me == Color.WHITE) ? Integer.MIN_VALUE : Integer.MAX_VALUE;
          int currentScore;
          int bestRow = -1;
          int bestCol = -1;

          if (nextMoves.isEmpty() || depth == 0) {
             bestScore = evaluateBoard(board);
          } else {
             for (int[] move : nextMoves) {
                 System.out.println(move[0]+","+move[1]+" THIS WAS ATTEMPTED");
                 board[move[0]][move[1]] = Color.WHITE;
                if (me == Color.BLACK) {  
                   currentScore = minimax(depth - 1, board)[0];
                   if (currentScore > bestScore) {
                      bestScore = currentScore;
                      bestRow = move[0];
                      bestCol = move[1];
                   }
                } else {  // oppSeed is minimizing player
                   currentScore = minimax(depth - 1, board)[0];
                   if (currentScore < bestScore) {
                      bestScore = currentScore;
                      bestRow = move[0];
                      bestCol = move[1];
                   }
                }
                board[move[0]][move[1]] = null;
             }
          }
          return new int[] {bestRow, bestCol};
       }

public int evaluateBoard(Color[][] board) {
    int score = 0;
    // Check all the rows
    for (int i = 0; i < 8; i++) {
        int blank = 0;
        int black = 0;
        int white = 0;
        for (int j = 0; j < 8 - 5; ++j) {
            if (board[i][j] == Color.black) {
                black++;
                if (board[i][j + 1] == Color.BLACK) {
                    black++;
                    if (board[i][j + 2] == Color.BLACK) {
                        if (board[i][j + 3] == Color.BLACK) {
                            black++;
                            if (board[i][j + 4] == Color.BLACK) {
                                black++;
                            }
                        }
                    }
                }
            } else if (board[i][j] == Color.WHITE) {
                white++;
                if (board[i][j + 1] == Color.WHITE) {
                    white++;
                    if (board[i][j + 2] == Color.WHITE) {
                        white++;
                        if (board[i][j + 3] == Color.WHITE) {
                            white++;
                            if (board[i][j + 4] == Color.WHITE) {
                                white++;
                            }
                        }
                    }
                }
            }
        }
        System.out.println(black);
        score += scoreChange(black, white);
    }

    // Check all the columns
    for (int j = 0; j < 8; ++j) {
        int blank = 0;
        int black = 0;
        int white = 0;
        for (int i = 0; i < 8 - 5; ++i) {
            if (board[i][j] == Color.black) {
                black++;
                if (board[i + 1][j] == Color.BLACK) {
                    black++;
                    if (board[i + 2][j] == Color.BLACK) {
                        black++;
                        if (board[i + 3][j] == Color.BLACK) {
                            black++;
                            if (board[i + 4][j] == Color.BLACK) {
                                black++;
                            }
                        }
                    }
                }
            } else if (board[i][j] == Color.WHITE) {
                white++;
                if (board[i + 1][j] == Color.WHITE) {
                    white++;
                    if (board[i + 2][j] == Color.WHITE) {
                        white++;
                        if (board[i + 3][j] == Color.WHITE) {
                            white++;
                            if (board[i + 4][j] == Color.WHITE) {
                                white++;
                            }
                        }
                    }
                }
            }
        }
        score += scoreChange(black, white);
    }

    int black = 0;
    int white = 0;
    // Check diagonal (Second)
    for (int i = 7, j = 0; i > 3; --i, ++j) {
        if (board[i][j] == Color.black) {
            black++;
            if (board[i - 1][j + 1] == Color.black) {
                black++;
                if (board[i - 2][j + 2] == Color.black) {
                    black++;
                    if (board[i - 3][j + 3] == Color.black) {
                        black++;
                        if (board[i - 4][j + 4] == Color.black) {
                            black++;
                        }
                    }
                }
            }
        } else if (board[i][j] == Color.white) {
            white++;
            if (board[i - 1][j + 1] == Color.white) {
                white++;
                if (board[i - 2][j + 2] == Color.white) {
                    white++;
                    if (board[i - 3][j + 3] == Color.white) {
                        white++;
                        if (board[i - 4][j + 4] == Color.white) {
                            white++;
                        }
                    }
                }
            }
        }
    }
    score += scoreChange(black, white);

    black = 0;
    white = 0;
    // Check Diagonal (First)
    for (int i = 0, j = 0; i < 4; ++i, ++j) {
        if (board[i][j] == Color.black) {
            black++;
            if (board[i + 1][j + 1] == Color.black) {
                black++;
                if (board[i + 2][j + 2] == Color.black) {
                    black++;
                    if (board[i + 3][j + 3] == Color.black) {
                        black++;
                        if (board[i + 4][j + 4] == Color.black) {
                            black++;
                        }
                    }
                }
            }
        } else if (board[i][j] == Color.white) {
            white++;
            if (board[i + 1][j + 1] == Color.white) {
                white++;
                if (board[i + 2][j + 2] == Color.white) {
                    white++;
                    if (board[i + 3][j + 3] == Color.white) {
                        white++;
                        if (board[i + 4][j + 4] == Color.white) {
                            white++;
                        }
                    }
                }
            }
        }
    }
    score += scoreChange(black, white);
    return score;
}

private int scoreChange(int black, int white) {
    int change;

    if (black == 5) {
        change = -10000;
    } else if (black == 4 && white == 0) {
        change = -1000;
    } else if (black == 3 && white == 0) {
        change = -100;
    } else if (black == 2 && white == 0) {
        change = -10;
    } else if (black == 1 && white == 0) {
        change = -1;
    } else if (white == 5) {
        change = 10000;
    } else if (white == 4 && black == 0) {
        change = 1000;
    } else if (white == 3 && black == 0) {
        change = 100;
    } else if (white == 2 && black == 0) {
        change = 10;
    } else if (white == 1 && black == 0) {
        change = 1;
    } else {
        change = 0;
    }
    return change;
}

【问题讨论】:

  • 你的 错了......

标签: java algorithm minimax


【解决方案1】:

如果玩家是黑棋,最好的分数是 Integer.Max。当前得分永远不会大于此,因此永远不会设置最佳移动。对于 White,问题是一致的:最好的分数是 Integer.Min,当前的分数永远不会更小。

使用调试器更容易找到像这样的错误。设置一个断点,将移动设置为 -1,然后向前走。当您看到那些 if 语句时,您会因为您的代码没有介入而感到惊讶。您将仔细查看正在比较的值,然后意识到您的错误。

【讨论】:

  • 所以我基本上设置了 currentScore 错误的方式,我没有修复,但它似乎并没有捍卫也没有思考更聪明
  • 我不玩游戏,也没有你所有的代码,所以我无法评估你的代码为什么不起作用。 (我也不应该。)但是我怀疑您希望 bestScore 代表的不是“可能的最佳分数”,而是“我到目前为止找到的最佳分数”。改变比较的方向只接受 每一个 移动。您还可以使用调试器来解决。
猜你喜欢
  • 2021-05-13
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多