2014-12-04 76 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))还有其他一些错误,但这应该给你一个完成项目的良好开端。