【问题标题】:Need better logic for TicTacToe井字游戏需要更好的逻辑
【发布时间】:2015-04-22 04:55:37
【问题描述】:

我正在使用 Java Swing 实现一个井字游戏 GUI 应用程序。当前获胜的逻辑是:

JButton[] button = new JButton[9];
boolean win = false;
//logic for player's win
if (button[0].getText() == button[1].getText() && button[1].getText() == button[2].getText() && button[0].getText() != "") {
    Won = true;
} else if (button[3].getText() == button[4].getText() && button[4].getText() == button[5].getText() && button[3].getText() != "") {
    Won = true;
} else if (button[6].getText() == button[7].getText() && button[7].getText() == button[8].getText() && button[6].getText() != "") {
    Won = true;
} else if (button[0].getText() == button[3].getText() && button[3].getText() == button[6].getText() && button[0].getText() != "") {
    Won = true;
} else if (button[1].getText() == button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText() != "") {
    Won = true;
} else if (button[2].getText() == button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText() != "") {
    Won = true;
} else if (button[0].getText() == button[4].getText() && button[4].getText() == button[8].getText() && button[0].getText() != "") {
    Won = true;
} else if (button[2].getText() == button[4].getText() && button[4].getText() == button[6].getText() && button[2].getText() != "") {
    Won = true;
}

这看起来有点笨拙。球员获胜有更好的逻辑吗?

【问题讨论】:

标签: java swing


【解决方案1】:

所以,你拥有的(基本上)是一个 2D 矩阵(好吧,它是一个虚拟矩阵,但这样更容易思考)...

您需要的是一种方法,您可以通过该方法搜索此矩阵的三个单元格,从给定的行和列开始。

因此,给定一个起点,您还需要提供有关您要搜索的方向的信息(有时,您想向后搜索),可能类似于...

public boolean matrixWin(int row, int col, int rowDelta, int colDelta) {

    boolean win = false;
    String value = button[(row * 3) + col].getText();
    if (!value.isEmpty()) {
        win = true;
        for (int count = 1; count < 3; count++) {
            row += rowDelta;
            col += colDelta;
            String test = button[(row * 3) + col].getText();
            if (test.isEmpty() || !test.equals(value)) {
                win = false;
                break;
            }
        }
    }
    return win;

}

这基本上是获取第一个单元格的值,如果它不为空,它开始在矩阵中移动,基于增量值,并检查其他单元格以查看它是否不为空或是否匹配第一个单元格。

好的,这很酷,但我懒得尝试设置我想检查的每个排列,所以我会做一些辅助方法...

基本上,您要检查三行是否为水平获胜,三列是否为垂直获胜以及左右对角线...

public boolean horizontalWin(int row) {
    return matrixWin(row, 0, 0, 1);
}

public boolean verticalWin(int col) {
    return matrixWin(0, col, 1, 0);
}

public boolean leftDiagonalWin() {
    return matrixWin(0, 0, 1, 1);
}

public boolean rightDiagonalWin() {
    return matrixWin(0, 2, 1, -1);
}

但即便如此,除非我想知道哪一行/列/对角线实际上赢了,否则我会让它变得更容易...

public boolean horizontalWins() {

    int row = 0;
    boolean win = false;
    do {
        win = horizontalWin(row);
        row++;
    } while (row < 3 && !win);

    return win;

}

public boolean verticalWins() {

    int col = 0;
    boolean win = false;
    do {
        win = verticalWin(col);
        col++;
    } while (col < 3 && !win);

    return win;

}

那么你就可以开始...

public boolean didWin() {

    return horizontalWins() || verticalWins() || leftDiagonalWin() || rightDiagonalWin();

}

是的,一个方法调用,但您仍然可以准确地确定“如何”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多