使用java方法的tic-tac-toe

使用java方法的tic-tac-toe,java,tic-tac-toe,Java,Tic Tac Toe,代码应该是玩Tictatcoe的,但是在打印游戏板方法中有一个异常,越界异常,我不知道为什么。如果有人知道如何避免这种例外情况,我将不胜感激。这就是我到目前为止所做的: static char arrayList[][] = {{0,1,2},{0,1,2}}; static Scanner input; public static void main(String[] args) { // Only one scanner!!! input = new Scanner(System.

代码应该是玩Tictatcoe的,但是在打印游戏板方法中有一个异常,越界异常,我不知道为什么。如果有人知道如何避免这种例外情况,我将不胜感激。这就是我到目前为止所做的:

 static char arrayList[][] = {{0,1,2},{0,1,2}};
 static Scanner input;
 public static void main(String[] args) {
 // Only one scanner!!!
 input = new Scanner(System.in);
 while (true) { // TTT 4 forever!
 printGameBoard();
 userInput();
 if (checkForWin('X')) {
 System.out.println("Congrats, you won!");
 break;
 }
 if (checkForStale()) {
 printGameBoard();
 System.out.println("Tie!");
 break;
 }
 computerTurn();
 if (checkForWin('O')) {
 System.out.println("All hail the future overlords!");
 break;
 }
 if (checkForStale()) {
 System.out.println("Tie!");
 break;
 }
 }
 }
 static void printGameBoard() {
 System.out.println("+---+---+---+");
 System.out.println("| " + arrayList[0][0] + " | " + arrayList[0][1] + " | " + arrayList[0][2]
 + " |");
 System.out.println("+---+---+---+");
 System.out.println("| " + arrayList[1][0] + " | " + arrayList[1][1] + " | " + arrayList[1][2]
 + " |");
 System.out.println("+---+---+---+");
 System.out.println("| " + arrayList[2][0] + " | " + arrayList[2][1] + " | " + arrayList[2][2]
 + " |");
 System.out.println("+---+---+---+");
 }
 static void userInput() {
 System.out.print("Please enter the board number:");
 int place = input.nextInt();
// Note to self: check user input for sanity here!
 if (checkForSanity(place)) {
 placeOnBoard(place, 'X');
 System.out.println("Ok...");
 printGameBoard();
 } else {
 System.out.println("Wrong move!");
 }
 }
 static void computerTurn() {
 boolean placed = false;
 while (!placed) {
 // Randomly choose a place
 int place = (int) (1 + (Math.random() * 8));
 // Check if sane
 if (checkForSanity(place)) {
 placeOnBoard(place, 'O');
 placed = true;
 }
 }
 printGameBoard();
 }
 static boolean checkForWin(char piece) {
 if ((arrayList[0][0] == piece) && (arrayList[0][1] == piece) && (arrayList[0][2] == piece))
 return true;
 if ((arrayList[1][0] == piece) && (arrayList[1][1] == piece) && (arrayList[1][2] == piece))
 return true;
 if ((arrayList[2][0] == piece) && (arrayList[2][1] == piece) && (arrayList[2][2] == piece))
 return true;
 if ((arrayList[0][0] == piece) && (arrayList[1][0] == piece) && (arrayList[2][0] == piece))
 return true;
 if ((arrayList[0][1] == piece) && (arrayList[1][1] == piece) && (arrayList[2][1] == piece))
 return true;
 if ((arrayList[0][2] == piece) && (arrayList[1][2] == piece) && (arrayList[2][2] == piece))
 return true;
 if ((arrayList[0][0] == piece) && (arrayList[1][1] == piece) && (arrayList[2][2] == piece))
 return true;
 if ((arrayList[0][2] == piece) && (arrayList[1][1] == piece) && (arrayList[2][0] == piece))
 return true;
 return false;
 }
 static boolean checkForStale() {
 if ((arrayList[0][0] != ' ') && (arrayList[0][1] != ' ') && (arrayList[0][2] != ' ')
 && (arrayList[1][0] != ' ') && (arrayList[1][1] != ' ') && (arrayList[1][2] != ' ')
 && (arrayList[2][0] != ' ') && (arrayList[2][1] != ' ') && (arrayList[2][2] != ' '))
 return true;
 return false;
 }
 static void placeOnBoard(int place, char piece) {
 if (place == 1)
 arrayList[0][0] = piece;
 else if (place == 2)
 arrayList[0][1] = piece;
 else if (place == 3)
 arrayList[0][2] = piece;
 else if (place == 4)
 arrayList[1][0] = piece;
 else if (place == 5)
 arrayList[1][1] = piece;
 else if (place == 6)
 arrayList[1][2] = piece;
 else if (place == 7)
 arrayList[2][0] = piece;
 else if (place == 8)
 arrayList[2][1] = piece;
 else if (place == 9)
 arrayList[2][2] = piece;
 }
 static boolean checkForSanity(int place) {
 boolean sane = false;
 if ((place == 1) && (arrayList[0][0] == ' '))
 sane = true;
 else if ((place == 2) && (arrayList[0][1] == ' '))
 sane = true;
 else if ((place == 3) && (arrayList[0][2] == ' '))
 sane = true;
 else if ((place == 4) && (arrayList[1][0] == ' '))
 sane = true;
 else if ((place == 5) && (arrayList[1][1] == ' '))
 sane = true;
 else if ((place == 6) && (arrayList[1][2] == ' '))
 sane = true;
 else if ((place == 7) && (arrayList[2][0] == ' '))
 sane = true;
 else if ((place == 8) && (arrayList[2][1] == ' '))
 sane = true;
 else if ((place == 9) && (arrayList[2][2] == ' '))
 sane = true;
 return sane;
 }
 static boolean checkForSanity(int row, int col) {
 return true;
 }
}

`

我认为问题在于您对arrayList的初始化。您已将其定义为2行3列。此外,您正在将其初始化为整数0、1、2,而不是字符“0”、“1”、“2”,因此在打印时,它将打印控制字符

我建议创建如下内容:

char[][] arrayList = new char[3][3];

这将给您留下一个空板(所有单元格都将为空。

我认为问题在于您对arrayList的初始化。您已将其定义为2行3列。此外,您正在将其初始化为整数0、1、2,而不是字符“0”、“1”、“2”,因此在打印时,它将打印控制字符

我建议创建如下内容:

char[][] arrayList = new char[3][3];

这将给您留下一个空板(所有单元格都将为空。

这是因为您的数组维度超出了范围。特别是第一个维度只能是0或1,第二个维度可以是0、1或2

鉴于此:

static char arrayList[][] = {{0,1,2},{0,1,2}};
这将是无效的:

arrayList[2][0]

这是因为数组维度超出了范围。特别是第一个维度只能是0或1,第二个维度可以是0、1或2

鉴于此:

static char arrayList[][] = {{0,1,2},{0,1,2}};
这将是无效的:

arrayList[2][0]
检查线路

 static char arrayList[][] = {{0,1,2},{0,1,2}};
仅声明2x3数组。请尝试再添加一行,使其成为3x3

tatic char arrayList[][] = {{0,1,2},{0,1,2},{0,1,2}};
检查线路

 static char arrayList[][] = {{0,1,2},{0,1,2}};
仅声明2x3数组。请尝试再添加一行,使其成为3x3

tatic char arrayList[][] = {{0,1,2},{0,1,2},{0,1,2}};

那个例外是什么?那个例外是什么?