저는 Java를 배우면서 간단한 Tic-Tac-Toe 게임을하고 있습니다. 실행되지 않는 IF 문을 제외하고는 모든 것이 제대로 작동한다고 생각합니다. 따라서 플레이어가 3 개의 X를 연속적으로 또는 대각선으로 가져 오면 IF 문은 실행되지 않습니다. 그러나 ELSE IF 문은 플레이어가 매를 연결하거나 3 개의 O를 얻을 때마다 잘 작동합니다. 어떤 도움을 많이 주시면 감사하겠습니다!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