2014-12-04 75 views
-3

我在井字遊戲Java程序的代碼中遇到挫折。每當我運行它時,遊戲板第二次打印3次,X全部填寫表示該玩家X獲勝的行。我一直在努力弄清楚,但我仍然無法找到與之相關的問題。我問的是,代碼打印出來的主要問題是什麼,並且停止填充行數?Java井字遊戲,打印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

使用您的調試器。 – 2014-12-04 23:51:06

+1

你的問題是什麼?我們不是在這裏調試你的代碼。請閱讀幫助:http://stackoverflow.com/help/dont-ask – 2014-12-04 23:56:12

回答

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))還有其他一些錯誤,但這應該給你一個完成項目的良好開端。