tic tac toe game java

时间:2013-07-17 23:42:07

标签: java tic-tac-toe

我是Java学生的新手,他被分配了一个tic tac toe游戏。当我尝试编译并运行该程序时,我收到以下错误:

run:
Exception in thread "main" java.lang.NullPointerException

at tictactoegame.TicTacToeGame.displayBoard(TicTacToeGame.java:55)

at tictactoegame.TicTacToeGame.main(TicTacToeGame.java:23)

Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

我的createBoard方法中也会收到"Local variable hides a field"

的警告

以下是我目前为止的项目代码。大多数代码都是由学校提供给我的,我想在createBoard方法中初始化数组并确定winOrTie方法中的获胜者。谁会有任何想法如何运行?我在启动数组时做错了什么?感谢能帮助我解决这个问题的人。

package tictactoegame;

import static tictactoegame.TicTacToeGame.gameboard;

public class TicTacToeGame {

   static int[][] gameboard;
   static final int EMPTY = 0;
   static final int NOUGHT = -1;       //this is an 'O'
   static final int CROSS = 1;         //this is an 'X'

   public static void main(String[] args) {
      createBoard(3,3);
      int turn =0;
      int playerVal;
      int outcome;

      java.util.Scanner scan = new java.util.Scanner(System.in);

      do {
         displayBoard();
         playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
         if (playerVal == NOUGHT) System.out.println("\n--O's turn--");
         else System.out.println("\n--X's turn--");
         System.out.print("Enter row and column");

         try {
            set(playerVal, scan.nextInt(), scan.nextInt());   
         } catch (Exception ex) {System.err.println(ex);}
         turn++;
         outcome = winOrTie();
      } while (outcome == -2);
      displayBoard();
      switch (outcome){
         case NOUGHT:
            System.out.println("O wins!");
            break;
         case CROSS:
            System.out.println("X wins!");
            break;
         case 0:
            System.out.println("Tie.");
            break;
      }
   }

   static void set(int val, int row, int col) throws
      IllegalArgumentException {
      if (gameboard[row][col] == EMPTY) gameboard[row][col] = val;
      else throw new IllegalArgumentException("Player already there!");
   }



   static void displayBoard() {
      for(int r=0; r < gameboard.length; r++) {
         System.out.print("|");
         for(int c=0; c < gameboard[r].length; c++) {
            switch(gameboard [r][c]) {
               case NOUGHT:
                  System.out.print("O");
                  break;
               case CROSS:
                  System.out.print("X");
                  break;
               default:            //Empty
                  System.out.print(" ");
            }

            System.out.print("|");

         }

         System.out.println("\n-----\n");            

      }

   }



   static void createBoard(int rows, int cols) {
      int gameboard[][]=  new int[3][3];
   }



   static int winOrTie() {           

      //NOUGHT wins horizontal

      if (gameboard[0][0] == NOUGHT && gameboard[0][1] == NOUGHT && gameboard[0][2] == NOUGHT)
         return NOUGHT;
      else if (gameboard[1][0] == NOUGHT && gameboard[1][1] == NOUGHT && gameboard[1][2] == NOUGHT)
         return NOUGHT;
      else if (gameboard[2][0] == NOUGHT && gameboard[2][1] == NOUGHT && gameboard[2][2] == NOUGHT)
         return NOUGHT;

      //NOUGHT wins veritcal

      else if (gameboard[0][0] == NOUGHT && gameboard[1][0] == NOUGHT && gameboard[2][0] == NOUGHT)
         return NOUGHT;
      else if (gameboard[0][1] == NOUGHT && gameboard[1][1] == NOUGHT && gameboard[2][1] == NOUGHT)
         return NOUGHT;
      else if (gameboard[0][2] == NOUGHT && gameboard[1][2] == NOUGHT && gameboard[2][2] == NOUGHT)
         return NOUGHT;

      //NOUGHT wins diagonal

      else if (gameboard[0][0] == NOUGHT && gameboard[1][1] == NOUGHT && gameboard[2][2] == NOUGHT)
         return NOUGHT;
      else if (gameboard[0][2] == NOUGHT && gameboard[1][1] == NOUGHT && gameboard[2][0] == NOUGHT)
         return NOUGHT;

      //CROSS wins horizontal

      else if (gameboard[0][0] == CROSS && gameboard[0][1] == CROSS && gameboard[0][2] == CROSS)
         return CROSS;
      else if (gameboard[1][0] == CROSS && gameboard[1][1] == CROSS && gameboard[1][2] == CROSS)
         return CROSS;
      else if (gameboard[2][0] == CROSS && gameboard[2][1] == CROSS && gameboard[2][2] == CROSS)
         return CROSS;

      //CROSS wins veritcal

      else if (gameboard[0][0] == CROSS && gameboard[1][0] == CROSS && gameboard[2][0] == CROSS)
         return CROSS;
      else if (gameboard[0][1] == CROSS && gameboard[1][1] == CROSS && gameboard[2][1] == CROSS)
         return CROSS;
      else if (gameboard[0][2] == CROSS && gameboard[1][2] == CROSS && gameboard[2][2] == CROSS)
         return CROSS;

      //CROSS wins diagonal

      else if (gameboard [0][0] == CROSS && gameboard[1][1] == CROSS && gameboard[2][2] == CROSS)
         return CROSS;
      else if (gameboard [0][2] == CROSS && gameboard[1][1] == CROSS && gameboard[2][0] == CROSS)
         return CROSS;               
      else
         return -2;
   }
}

1 个答案:

答案 0 :(得分:2)

您正在隐藏gameboard变量。当您致电createBoard时,您正在创建一个新的本地变量并对其进行初始化,这会使您的其他createBoard变量保持未初始化状态。

 static void createBoard(int rows, int cols) {
     int gameboard[][]=  new int[3][3];
 }

...试

 static void createBoard(int rows, int cols) {
     gameboard =  new int[3][3];
 }

相反