这是给出tic tac toe board的获胜者名字(X或Y)的方法。如果没有赢家,则返回空。
玩家X先行:
但在某种特定情况下,如果不起作用: x次:2,0 o播放:1,0 x次:2,1 o戏剧:1,1 x播放2,2 (此时'X'应该获胜但没有任何反应)
o play:1,2 (此时“O”应该获胜但没有任何反应)
x播放任何动作
O被宣布为胜利者。
我无法弄清楚这个漏洞。
public static char checkWinner(char [] [] array){
// check for row winners
for (int row = 0; row < array.length; row++) {
// if all three columns are the same then that player wins!
if (array[row][0] == array[row][1] && array[row][1] == array[row][2]) {
// return the winner!
return array[row][0];
}
}
// check for column winners
for (int col = 0; col < array.length; col++) {
// if all three rows are the same then that player wins!
if (array[0][col] == array[1][col] && array[1][col] == array[2][col]) {
// return our winner!
return array[0][col];
}
}
// check for diag winners
if (array[0][0] == array[1][1] && array[1][1] == array[2][2]) {
return array[0][0];
}
if (array[0][2] == array[1][1] && array[1][1] == array[2][0]) {
return array[0][2];
}
// otherwise just return an empty character
return ' ';
}
如果有帮助,这是整个代码:
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
// create an empty board
char[][] board = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };
Scanner input = new Scanner(System.in);
// start off with player 'X'
char player = 'X';
while (true) {
printBoard(board);
// ask the player for a location
System.out.println("Welcome, player " + player);
System.out.print("Enter a row: ");
int row = input.nextInt();
System.out.print("Enter a col: ");
int col = input.nextInt();
// check to see if this row is already taken
if (board[row][col] != ' ') {
System.out.println("\nThat spot is already taken!");
} else {
// otherwise assign the player's token to this spot
board[row][col] = player;
// check to see if someone won
char testWinner = checkWinner(board);
// do we have a winner?
if (testWinner != ' ') {
printBoard(board);
System.out.println("We have a winner! " + testWinner);
break;
} else {
// switch to the next player
if (player == 'X') {
player = 'O';
} else {
player = 'X';
}
}
}
}
}
public static char checkWinner(char[][] array) {
// check for row winners
for (int row = 0; row < array.length; row++) {
// if all three columns are the same then that player wins!
if (array[row][0] == array[row][1] && array[row][1] == array[row][2]) {
// return the winner!
return array[row][0];
}
}
// check for column winners
for (int col = 0; col < array.length; col++) {
// if all three rows are the same then that player wins!
if (array[0][col] == array[1][col] && array[1][col] == array[2][col]) {
// return our winner!
return array[0][col];
}
}
// check for diag winners
if (array[0][0] == array[1][1] && array[1][1] == array[2][2]) {
return array[0][0];
}
if (array[0][2] == array[1][1] && array[1][1] == array[2][0]) {
return array[0][2];
}
// otherwise just return an empty character
return ' ';
}
public static void printBoard(char[][] array) {
System.out.println("\n----------");
for (int row = 0; row < array.length; row++) {
System.out.print("|");
for (int col = 0; col < array.length; col++) {
System.out.print(array[row][col] + " |");
}
System.out.println("\n----------");
}
System.out.println();
}
}
答案 0 :(得分:0)
问题在于,当您看到当前组合的所有三个字符都相同时,您不会检查它是否为三个空字符。现在,在您的示例中,它将提供' '
,因为x = 0的列填充了' '
s。这种检查所有组合的方式也有点长,我会这样做:
public static char checkWinner(char[][] array) {
int[][] combinations = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6}
};
for (int[] co : combinations) {
char[] cs = new char[] {
array[co[0] % 3][co[0] / 3],
array[co[1] % 3][co[1] / 3],
array[co[2] % 3][co[2] / 3]
};
if (cs[0] != ' ' && cs[0] == cs[1] && cs[1] == cs[2]) {
return cs[0];
}
}
return ' ';
}
答案 1 :(得分:0)
checkWinner是否条件忽略空格字符。并且行块在第一次尝试时返回,因为顶行是所有空格。