我一直在學習Java,並且正在開發一個簡單的Tic-Tac-Toe遊戲。我相信我已經得到了一切正常工作,除了IF語句沒有執行。所以當玩家連續或對角地獲得三個X時,IF語句不會執行。然而,無論何時玩家配對或得到三個O,ELSE IF語句都可以正常工作。任何幫助將非常感激!IF語句不執行,但ELSE IF語句正常工作
public static void check_status()
{
if ( (board[0][0]=='X' && board[0][1]=='X' && board[0][2]=='x') ||
(board[1][0]=='X' && board[1][1]=='X' && board[1][2]=='x') ||
(board[2][0]=='X' && board[2][1]=='X' && board[2][2]=='x') ||
(board[0][0]=='X' && board[1][0]=='X' && board[2][0]=='x') ||
(board[0][1]=='X' && board[1][1]=='X' && board[2][1]=='x') ||
(board[0][2]=='X' && board[1][2]=='X' && board[2][2]=='x') ||
(board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='x') ||
(board[0][2]=='X' && board[1][1]=='X' && board[2][0]=='x'))
{
System.out.println();
System.out.println("X wins the game!");
end_game = 1;
}
else if ( (board[0][0]=='O' && board[0][1]=='O' && board[0][2]=='O') ||
(board[1][0]=='O' && board[1][1]=='O' && board[1][2]=='O') ||
(board[2][0]=='O' && board[2][1]=='O' && board[2][2]=='O') ||
(board[0][0]=='O' && board[1][0]=='O' && board[2][0]=='O') ||
(board[0][1]=='O' && board[1][1]=='O' && board[2][1]=='O') ||
(board[0][2]=='O' && board[1][2]=='O' && board[2][2]=='O') ||
(board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O') ||
(board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O'))
{
System.out.println();
System.out.println("O wins the game!");
end_game = 1;
}
else if ( (board[0][0]!=' ' && board[0][1]!=' ' && board[0][2]!=' ') &&
(board[1][0]!=' ' && board[1][1]!=' ' && board[1][2]!=' ') &&
(board[2][0]!=' ' && board[2][1]!=' ' && board[2][2]!=' '))
{
System.out.println();
System.out.println("The game is a tie.");
end_game = 1;
}
}
此外,這是我第一次發佈堆棧溢出所以道歉,如果問題或格式不合適。
全碼:
import java.util.Scanner;
public class TicTacToe
{
private static char[][] board = new char[3][3];
private static int end_game = 0;
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
initBoard();
displayBoard();
do
{
System.out.println();
System.out.print("\'O\', choose your location (row, column): ");
int O_row = keyboard.nextInt();
int O_col = keyboard.nextInt();
board[O_row][O_col] = 'O';
System.out.println();
displayBoard();
check_status();
if (end_game==1){
break;
}
System.out.println();
System.out.print("\'X\', choose your location (row, column): ");
int X_row = keyboard.nextInt();
int X_col = keyboard.nextInt();
board[X_row][X_col] = 'X';
System.out.println();
displayBoard();
check_status();
} while (end_game==0);
}
public static void check_status()
{
if ( (board[0][0]=='X' && board[0][1]=='X' && board[0][2]=='x') ||
(board[1][0]=='X' && board[1][1]=='X' && board[1][2]=='x') ||
(board[2][0]=='X' && board[2][1]=='X' && board[2][2]=='x') ||
(board[0][0]=='X' && board[1][0]=='X' && board[2][0]=='x') ||
(board[0][1]=='X' && board[1][1]=='X' && board[2][1]=='x') ||
(board[0][2]=='X' && board[1][2]=='X' && board[2][2]=='x') ||
(board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='x') ||
(board[0][2]=='X' && board[1][1]=='X' && board[2][0]=='x'))
{
System.out.println();
System.out.println("X wins the game!");
end_game = 1;
}
else if ( (board[0][0]=='O' && board[0][1]=='O' && board[0][2]=='O') ||
(board[1][0]=='O' && board[1][1]=='O' && board[1][2]=='O') ||
(board[2][0]=='O' && board[2][1]=='O' && board[2][2]=='O') ||
(board[0][0]=='O' && board[1][0]=='O' && board[2][0]=='O') ||
(board[0][1]=='O' && board[1][1]=='O' && board[2][1]=='O') ||
(board[0][2]=='O' && board[1][2]=='O' && board[2][2]=='O') ||
(board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O') ||
(board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O'))
{
System.out.println();
System.out.println("O wins the game!");
end_game = 1;
}
else if ( (board[0][0]!=' ' && board[0][1]!=' ' && board[0][2]!=' ') &&
(board[1][0]!=' ' && board[1][1]!=' ' && board[1][2]!=' ') &&
(board[2][0]!=' ' && board[2][1]!=' ' && board[2][2]!=' '))
{
System.out.println();
System.out.println("The game is a tie.");
end_game = 1;
}
}
public static void initBoard()
{
// fills up the board with blanks
for (int r=0; r<3; r++)
for (int c=0; c<3; c++)
board[r][c] = ' ';
}
public static void displayBoard()
{
System.out.println(" 0 " + board[0][0] + "|" + board[0][1] + "|" + board[0][2]);
System.out.println(" --+-+--");
System.out.println(" 1 " + board[1][0] + "|" + board[1][1] + "|" + board[1][2]);
System.out.println(" --+-+--");
System.out.println(" 2 " + board[2][0] + "|" + board[2][1] + "|" + board[2][2]);
System.out.println(" 0 1 2 ");
}
}
您在AND的最後部分中是否有意使用小型「x」字符? – padde 2014-10-10 07:53:29