【问题标题】:how to select the correct buttons in tic tac toe game如何在井字游戏中选择正确的按钮
【发布时间】:2018-06-07 22:34:18
【问题描述】:

我有一种方法可以尝试确定 AI 在井字游戏中的移动。目前我有一种方法,计算机随机选择一个按钮,但我希​​望它更聪明一点。我对如何做到这一点有一个想法,但在实施中需要一些指导。

基本上我希望计算机知道,如果有可能或赢或输,那么它会将他们的计数器放在方格上,这将帮助他们赢得或阻止玩家获胜。下面是一个使用 3x3 tic tac toe board 坐标网格的示例:

00 01 02
10 11 12  -- coordinates of all of the buttons in a tic tac toe board
20 21 22

if 00 is not empty and 01 is same as 00, then place 'O' on 02 -- helps win or prevent a win for 3 in a row
if 01 is not empty and 02 is same as 01, then place 'O' on 00 -- helps win or prevent a win for 3 in a row
if 01 is not empty and 21 is same as 01, then place 'O' on 11 -- helps win or prevent a win for 3 in a column
if 20 is not empty and 02 is same as 20, then place 'O' on 11 -- helps win or prevent a win for 3 in a diaganol

正如您所知,还有更多场景,但主要目的是如果有机会获胜或有机会阻止获胜,那么计算机将需要放置他们的“O”。如果条件不满足则基本上选择一个随机按钮。

所以我知道我需要一堆 else if 但是如何实现它来完成它需要做的事情?

下面是我试图做的一个框架,以防止在一行中连续 3 次获胜/获胜,并在上述任何条件不匹配时选择任何随机按钮:

private void computerMove() {
    String[][] field = new String[3][3];
    Random random = new Random(); //you may want to declare this as a class field
    List<Button> emptyButtons = new ArrayList<>();

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            field[i][j] = buttons[i][j].getText().toString();
            if (field[i][j].equals("")) {
                emptyButtons.add(buttons[i][j]);
            }
        }
    }

    Button selectButton;

    for (int i = 0; i < 3; i++) {
        if (field[i][0].equals(field[i][1])
                && field[i][2].equals("")){
            //prevent win/win on rows by selecting the correct button here
        }
        else if (field[i][0].equals(field[i][2])
                && field[i][1].equals("")){
             //prevent win/win on rows by selecting the correct button here
        }
        else if (field[i][1].equals(field[i][2])
                && field[i][0].equals("")){
            //prevent win/win on rows by selecting the correct button here
        }
        else {
            selectButton = emptyButtons.get(random.nextInt(emptyButtons.size()));
        }

    selectButton.setText("O");
    selectButton.setTextColor(playerO);
    turnsCount++;
    isGameOver();

}

【问题讨论】:

    标签: java


    【解决方案1】:

    为避免冗长的 if-else 语句,您可以使用整数表示“O”和“X”,这样任何组合的总和都是唯一的。

    例如,如果“0”为 1,“X”为 10,则给出:

             | 11
             |----
    X  X  .  | 20
    O  X  O  | 12
    O  O  .  | 2
    ---------|----
    12 21  1 | 20
    

    也就是说,这些列的总和为:12, 21, 1

    这些行的总和为:20, 12, 2

    对角线总和为:20, 11

    使用这种求和方法,您可以计算出任一玩家是否赢了 1 步。

    • 如果sum == 20,“X”可以通过在该行/列/对角线的零值索引上放置一块来获胜。

    • 如果sum == 2O 可以通过在该行/列/对角线的零值索引上放置一块来获胜。

    要获得对角线和,只需将i==j 所在的索引相加即可。

    编辑: i==j 给你一个对角线。在一般情况下,另一个以相反的顺序遍历两个循环(行与列)的索引。或者对于 3x3 游戏,就是 (1,3), (2,2), (3,1)。

    【讨论】:

    • 这似乎是一种非常聪明的做法。我会尝试实现它
    • 干杯。告诉我进展如何。
    【解决方案2】:

    井字游戏有 8 个可能的获胜位置(3 行、3 列和 2 个对角线)。

    计算机参考行、列或对角线的存储点

    100 个,连续 3 个

    10 个连续 2 个(有一个空单元格)

    1对1连续(有两个空单元格)

    现在您有足够的数据来确保下一步的最佳行动。

    注意:您也可以使用 Minimax 和 Alpha-beta 修剪技术。

    【讨论】:

      猜你喜欢
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多