2013-04-01 3 views
0

현재 java로 작성된 간단한 Tic Tac Toe 프로그램의 코드를 작성했습니다. 아래에서 볼 수있는 유일한 문제는 내 보드가 표시 될 때 열린 문자 대신에 널 문자 (\ u0000)가 인쇄된다는 것입니다.Java TicTacToe 프로그램에서 null 문자를 빈 문자로 바꾸려면 어떻게해야합니까?

교수님은 null 공간이 감지되는 방식으로이 프로그램을 작성하고 X 또는 O로 채우라고 말씀하셨습니다.

이제 널 (null) 문자가 00이 아닌 공백으로 변경 될 수 있습니다. 그렇지 않으면 형식이 올바르지 않습니다.

나는 '\ u0000'문자를 지우고 ''문자로 바꾸려고했지만 이미 내 보드가 나타나지 않습니다. 어떤 도움을 주셔서 감사합니다!

import java.util.Scanner; 

    public class TicTacToe 
    { 
    public static void main(String[] args) 
    { 

    char[][] board = new char[3][3]; 

    while (true) { 

     makeCompMove(board, 'X'); 

     displayBoard(board); 
     if(isWon('X', board)) { 
      System.out.println("\n\nComputer won!"); 
      System.exit(1); 
     } 

     else if (isDraw(board)) { 
      System.out.println("\n\nDraw Game! No winner"); 
      System.exit(2); 
     } 

     makeAMove(board, 'O'); 
     displayBoard(board); 

     if (isWon('O', board)) { 
      System.out.println("\n\nPlayer won!"); 
      System.exit(3); 
     } 
     else if (isDraw(board)) { 
      System.out.println("\n\nDraw Game! No winner"); 
      System.exit(4); 
     } 
    } 
    } 

    public static void displayBoard(char[][] board) 
    { 

    for(int k = 0; k < 3; k++) 
    { 
     for(int i = 0; i < 28; i++) 
     { 
      System.out.print("-"); 
     } 
     System.out.println(); 

     for(int j = 0; j < 3; j++) 
     { 
      System.out.print("|" + " " + board[k][j] + " "); 
     } 
     System.out.println("|"); 

    } 
     for(int i = 0; i < 28; i++) 
    { 
     System.out.print("-"); 
    } 

    } 

    public static void makeAMove(char[][] board, char o) 
    { 
    Scanner input = new Scanner(System.in); 
    while(true) 
    { 
     System.out.print("\n\nYour turn. Enter a row and col(0,1 or 2): "); 
     int row = input.nextInt(); 
     int col = input.nextInt(); 
     if(row > 2 || row < 0 || col > 2 || col < 0) 
     { 
      System.out.println("Incorrect Input. Try Again!"); 
      continue; 
     } 
     if(board[row][col] == '\u0000') 
     { 
       System.out.print("\n You (O) have made your move...\n\n"); 
      board[row][col] = 'O'; 
       break; 
     } 
     else 
      System.out.println("Incorrect Input. Try Again!"); 
    } 
    } 

    public static void makeCompMove(char[][] board, char x) 
    { 
     System.out.println(); 
     System.out.println(); 
    System.out.print("Computer (X) has made his move...\n"); 
    while(true) 
    { 
     int row = (int)(Math.random()*3); 
     int col = (int)(Math.random()*3); 

      if(board[row][col] == '\u0000') 
     { 
      board[row][col] = x; 
      break; 
     } 
    } 
     System.out.println(); 
    } 

public static boolean isDraw(char[][] board) 
    { 
    for(int row = 0; row < 3; row++) 
    { 
     for(int col = 0; col < 3; col++) 
     { 
      if(board[row][col] == '\u0000') 
      { 
       return false; 
      } 
     } 
    } 
    return true; 
    } 

    public static boolean isWon(char x, char[][] board) 
    { 
    // Check Rows 
     for (int i = 0; i < 3; i++) 
      if (x == board[i][0] && x == board[i][1] && x == board[i][2]) 
       return true; 

    // Check Columns 
     for (int j = 0; j < 3; j++) 
      if (x == board[0][j] && x == board[1][j] && x == board[2][j]) 
       return true; 

    // Check first diagonal 
     if (x == board[0][0] && x == board[1][1] && x == board[2][2]) 
      return true; 

    // Check second diagonal 
     if (x == board[0][2] && x == board[1][1] && x == board[2][0]) 
      return true; 

    return false; 
    } 
} 

답변

1

for(int j = 0; j < 3; j++) 
    { 
     if(board[k][j]=='\u0000') 
    System.out.print("|" + "  "); 
    else 
    System.out.print("|" + " " + board[k][j] + " "); 

    } 
+0

오오오! 고마워요, 매력처럼 작동합니다! – dmetal23

0

2 차원 배열의 요소는 배열 초기화시 null 문자로 설정됩니다. 그것들을 모두 공백으로 변환하고 싶은 경우 모두 반복하여 공백으로 바꿉니다.

for (int i = 0; i < board.length; i++) { 
    for (int j = 0; j < board[i].length; j++) { 
     board[i][j] = ' '; 
    } 
} 

장소를 사용하지 않으면 그 안에는 null 문자 대신 공백이 있습니다. 어레이를 사용하기 전에 전에 사용하십시오.

+0

처럼 사용 당신은 그냥 displayBoard에 표시

전에 확인 코드를 변경 할 필요가 와우 너무 쉬웠다. 대단히 감사합니다! – dmetal23

관련 문제