java tictactoe_java – 需要更好的TicTacToe逻辑

我正在使用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;

}

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

解决方法:

所以,你所拥有的(基本上)是一个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;

}

这基本上得到第一个单元格的值,如果它不是空的,它会根据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();

}

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

标签:java,swing

来源: https://codeday.me/bug/20190925/1816634.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值