需要更好的TicTacToe逻辑

时间:2015-04-22 04:55:37

标签: java swing

我正在使用Java Swing实现一个TicTacToe 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;
}

这看起来有点笨拙。玩家的胜利是否有更好的逻辑?

1 个答案:

答案 0 :(得分:3)

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

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

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

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;

}

这基本上得到第一个单元格的值,如果它不是空的,它会根据delta值开始在矩阵中移动,并检查每个其他单元格以查看它是否为空或者是否与第一个细胞。

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

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

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();

}

是的,一个方法调用,但你仍然有权确定“如何”