Java | TicTacToe 右对角线不起作用
Java | TicTacToe Right Diagonal Not Working
目前正在 java 开发一款井字游戏,我有一个 checkWin() 方法可以正确处理 4 种可能的获胜条件中的 3 种。我有问题的是右对角线。
代码:
public boolean checkWin(String player){
int row = 0; // Holder to count number of player spots in row
int d1 = 0; // Holder to count number of player spots in right diag.
int d2 = 0; // Holder to count number of player spots in left diag.
int[] column = new int[squares[0].length]; /* Holder to count number
of player spots in column */
for(int i = 0; i < size; i++){
row = 0;
for(int j = 0; j < size; j++){
if(null == squares[i][j]){
continue;
}
if(squares[i][j].getText().equals(player)){
row++; /* If spot at [i][j] equals player, increase row */
column[j]++; /* If spot at [i][j] equals player, increase
col */
if(i == j){ /* If spot at i is equal to j, increase left
diag */
d1++;
} else if ((size - 1) == i + j){ /* If spot at i + j
equals board size - 1, increase right diag. */
d2++;
}
}
}
if(row == size){
/*
if spots in row is equal to size (otherwise, if it fills
the row, return win
*/
return true;
}
}
if(size == d1 || size == d2){
/*
if spots in either diag is equal to size, return win
*/
return true;
}
for(int i = 0; i < column.length; i++){
if(column[i] == size){
/*
if column is full of the same player character, return win
*/
return true;
}
}
/*
otherwise, return false
*/
return false;
}
问题部分是:
else if ((size - 1) == i + j){ /* If spot at i + j
equals board size - 1, increase right diag. */
d2++;
}
这样设置的原因是二维阵列的工作原理,所以对于 3x3 板:
[00][01][02]
[10][11][12]
[20][21][22]
对于 i + j = size - 1,它会评估 2 + 0、1 + 1、0 + 2 都等于 2,如果 size = 3,则为 size - 1,但是当我 运行 程序并执行右对角线移动,它没有 return 真正的胜利价值。
如有任何解决此问题的建议,我们将不胜感激。
else if ((size - 1) == i + j)
^ 仅当上面的 if
条件为假时才会计算。
if(i == j)
当i == 1
和j == 1
时,则i == j
为真,因此(size - 1) == i + j
不计算。
TLDR:摆脱你的 else
。
目前正在 java 开发一款井字游戏,我有一个 checkWin() 方法可以正确处理 4 种可能的获胜条件中的 3 种。我有问题的是右对角线。
代码:
public boolean checkWin(String player){
int row = 0; // Holder to count number of player spots in row
int d1 = 0; // Holder to count number of player spots in right diag.
int d2 = 0; // Holder to count number of player spots in left diag.
int[] column = new int[squares[0].length]; /* Holder to count number
of player spots in column */
for(int i = 0; i < size; i++){
row = 0;
for(int j = 0; j < size; j++){
if(null == squares[i][j]){
continue;
}
if(squares[i][j].getText().equals(player)){
row++; /* If spot at [i][j] equals player, increase row */
column[j]++; /* If spot at [i][j] equals player, increase
col */
if(i == j){ /* If spot at i is equal to j, increase left
diag */
d1++;
} else if ((size - 1) == i + j){ /* If spot at i + j
equals board size - 1, increase right diag. */
d2++;
}
}
}
if(row == size){
/*
if spots in row is equal to size (otherwise, if it fills
the row, return win
*/
return true;
}
}
if(size == d1 || size == d2){
/*
if spots in either diag is equal to size, return win
*/
return true;
}
for(int i = 0; i < column.length; i++){
if(column[i] == size){
/*
if column is full of the same player character, return win
*/
return true;
}
}
/*
otherwise, return false
*/
return false;
}
问题部分是:
else if ((size - 1) == i + j){ /* If spot at i + j
equals board size - 1, increase right diag. */
d2++;
}
这样设置的原因是二维阵列的工作原理,所以对于 3x3 板:
[00][01][02]
[10][11][12]
[20][21][22]
对于 i + j = size - 1,它会评估 2 + 0、1 + 1、0 + 2 都等于 2,如果 size = 3,则为 size - 1,但是当我 运行 程序并执行右对角线移动,它没有 return 真正的胜利价值。
如有任何解决此问题的建议,我们将不胜感激。
else if ((size - 1) == i + j)
^ 仅当上面的 if
条件为假时才会计算。
if(i == j)
当i == 1
和j == 1
时,则i == j
为真,因此(size - 1) == i + j
不计算。
TLDR:摆脱你的 else
。