我是 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)
我还在我的 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;
}
}