2014-12-04 8 views
-3

저는 Tic Tac Toe Java 프로그램의 코드에서 좌절감을 느낍니다. 게임 보드를 실행할 때마다 게임 보드가 3 번 인쇄되고 두 번째로 X가 모두 X라는 플레이어의 라인을 채 웁니다. 알아 내려고 노력했지만 문제는 계속 찾아 낼 수 없습니다. 필자가 묻는 것은, 필요한 코드를 인쇄하는 것보다 더 많은 것을 인쇄하고 줄을 채우지 않는 것이 가장 큰 문제점은 무엇입니까?Java Tic-Tac-Toe, 3 개의 보드를 인쇄합니다.

이 형식을 올바르게 지정하기 바랍니다.

import java.util.Scanner; 

public class TicTacToeWork { 

    //Variable declaration. 
    public static char[][] GameBoard = new char[3][3]; 
    public static Scanner keyboard = new Scanner(System.in); 
    public static int column, row; 
    public static char PlayerTurn = 'X'; 

    public static void main(String args[]) { 

     for (int i = 0; i > 3; i++) { 

      for (int j = 0; j < 3; j++) { 
       GameBoard[i][j] = '_'; 
      } 
     } 
     System.out.println("Enter coordinates of row then column to choose your space."); 
     Playing(); 
    } 

    public static void Playing() { 

     boolean PlayerPlaying = true; 

     while (PlayerPlaying) { 
      boolean playing = true; 

      while (playing) { 
       row = keyboard.nextInt() - 1; 
       column = keyboard.nextInt() - 1; 
       GameBoard[row][column] = PlayerTurn; 

       if (EndGame(row, column)) { 
        playing = false; 
        System.out.println("Game over player " + PlayerTurn + " wins!"); 
       } 
       BoardPrint(); 

       if (PlayerTurn == 'X') { 
        PlayerTurn = 'O'; 
       } else { 
        PlayerTurn = 'X'; 
       } 
      } 
     } 
    } 

    public static void BoardPrint() { 

     //Print out the game board. 
     for (int i = 0; i < GameBoard.length; i++) { 
      for (int n = 0; n < 3; n++) { 
       System.out.println(); 

       for (int j = 0; j < 3; j++) { 

        if (j == 0) { 
         System.out.print("| "); 
        } 
        System.out.print(GameBoard[i][j] + " | "); 
       } 
      } 
      System.out.println(); 
     } 
     System.out.println(); 
    } 

    public static boolean EndGame(int RowMove, int ColumnMove) { 

     //Deciding factors on who wins and ties. 
     if (GameBoard[0][ColumnMove] == GameBoard[1][ColumnMove] 
       && GameBoard[1][ColumnMove] == GameBoard[2][ColumnMove]) { 
      return true; 
     } 
     if (GameBoard[RowMove][0] == GameBoard[1][RowMove] 
       && GameBoard[RowMove][0] == GameBoard[RowMove][2]) { 
      return true; 
     } 
     if (GameBoard[0][0] == GameBoard[1][1] && GameBoard[0][0] == GameBoard[2][2] 
       && GameBoard[1][1] != '_') { 
      return true; 
     } 
     if (GameBoard[0][2] == GameBoard[1][1] && GameBoard[0][2] == GameBoard[2][0] 
       && GameBoard[1][1] != '_') { 
      return true; 
     } else { 
      return false; 
     } 
    } 
} 
+3

디버거를 사용하십시오. –

+1

당신의 질문은 무엇입니까? 우리는 당신의 코드를 디버깅하지 않습니다. 도움말을 읽어보십시오. http://stackoverflow.com/help/dont-ask –

답변

0

귀하의 BoardPrint 방법이 모두 잘못되었습니다. 이 같은 것을해야합니다.

public static void BoardPrint() { 
    System.out.println("-------------"); 

    for (int i = 0; i < 3; i++) { 
     System.out.print("| "); 

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

     System.out.println("-------------"); 
    } 

또한 수표를 다시 받으셔야합니다.

조금 힌트 .. 부울 메서드를 올바르게 사용하고 있지 않습니다. 이 라인 if(EndGame(row, column))은 EndGame이 참이라면 게임을 중지합니다. 귀하의 방법을 설정하는 방법은 정확하게 검사를 수행하지 않습니다. "사실이 아니라면"라고 말해야 할 것입니다. 이렇게 보일 것입니다. if(!EndGame(row, column)) 다른 오류가 몇 가지 있지만 프로젝트를 완료하는 데 좋은 시작이 될 것입니다.