2013-07-17 134 views
0

我是新手Java學生,誰被分配了tic tac腳趾遊戲。當我嘗試編譯並運行程序,我得到以下錯誤:tic tac toe遊戲java

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) 

我也是在說"Local variable hides a field"

下面是我的項目代碼到目前爲止我createBoard方法得到一個警告。大多數代碼是由學校提供給我的,我想在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; 
    } 
} 
+3

您需要使用一個調試器。 – SLaks

+2

密切關注編譯器提供的警告。 Java很好,僅僅因爲很好的理由而警告。在這種情況下,它直接指出你的問題。 – NoUserException

回答

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]; 
} 

相反

+0

那麼解決了一個問題,但又帶來了另一個問題。現在我在這些行上發現錯誤,說';'預計不是一個聲明']'預計不是一個聲明。任何想法呢?謝謝你的幫助。 –

+0

對不起,我的壞,檢查更新 – MadProgrammer

+0

現在它的工作!萬分感謝! –