Java TicTacToe ArrayOutOfBounds

时间:2016-04-01 05:02:09

标签: java arrays

import java.util.*;
public class TicTacToe {
public static char X = 'X';
public static char O = 'O';
public static char S = ' ';
public static char[][] board = new char[3][3];
public static boolean isFull = false, win = false;
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int c,r;
    for(r=0;r<board.length;r++){
        for(c=0;c<board[r].length;c++)
            board[r][c] = ' ';              
    }
    System.out.println("Player 1: X");
    System.out.println("Player 2: O");
    printBoard(board);
    for(int i = 0; i <9 && win==false;i++){

        if(i%2==0){
            do {
                System.out.print("Player 1: Enter your next move:(r,c)  ");
                r = in.nextInt();
                c = in.nextInt();
                if(r>board.length || c>board.length || r<0 || c<0)
                    System.out.println("Error, try again ");
                if(board[r][c]==X || board[r][c]==O){
                    isFull=true;
                    System.out.println("This square is already taken, Player 1, try again");
                }
                if(board[r][c]==S){
                    isFull=false;
                    board[r][c] = X;
                    checkWin(board);
                    printBoard(board);
                }
            }while(isFull);

        }
        else{
            do{ 
                System.out.print("Enter your next move:(r,c)  ");
                r = in.nextInt();
                c = in.nextInt();
                if(r>board.length || c>board.length || r<0 || c<0)
                    System.out.println("Error, try again ");
                if(board[r][c]==X || board[r][c]==O){
                    isFull=true;
                    System.out.println("This square is already taken, Player 2, try again");
                }
                if(board[r][c]==S){
                    isFull=false;
                    board[r][c] = O;
                    checkWin(board);
                    printBoard(board);
                }
            }while(isFull);
        }
        if(win)
            System.out.print("We have a winner");
    }
}
public static boolean checkWin(char[][] b){
    int r = 0,c = 0,countX = 0,countO = 0;
    for(char i = board[r][c];r<3 && c<3;r++)
        System.out.println(r);
        if(board[r][c]==X)
            countX++;
        if(board[r][c]==O)
            countO++;
        if(countX==3 || countO==3)
            return win = true;
        if(countX<3 && countO<3 && r<3){
            countX = 0;
            countO = 0;
            r = 0;
            c++;
        }
    return win = false;
}
public static void printBoard(char[][] b){
    int r = 0,c = 0;
    System.out.println();
    for(r=0;r<b.length;r++){
        for(c=0;c<b[r].length-1;c++)
            System.out.print(" " + b[r][c] + " |");
        System.out.println(" "+b[r][c]);
        if(r<b.length-1){
            for(c=0;c < b[r].length-1;c++)
                System.out.print("---+");
            System.out.println("---");
        }
    }
}
}

当我尝试检查连续三行时,它会在这些行上抛出一个ArrayOutOfBoundsException。

if(board[r][c]==X)
    countX++;
if(board[r][c]==O)
    countO++;

我不知道为什么它会在那里抛出异常,考虑到我只是在那里增加了三个。

2 个答案:

答案 0 :(得分:0)

问题出在这个for循环中:

for(char i = board[r][c] ; r<3 && c<3 ; r++)  

当您到达r = 3时,r<3 && c<3仍然是false并且您尝试访问br[r][c],这就是您获得越界异常的原因。

我建议您使用嵌套循环而不是使用if语句来重置。但如果你真的想走这条路,那么这是一个可能的结构:

 for(char i = board[r][c] ; c<3 ; r++) {
    ...
    if(r == 2)  {
        r=0;
        c++;
    }
 }  

答案 1 :(得分:0)

int r = 0, c = 0, countX = 0, countO = 0;
for (char i = board[r][c]; r < 3 && c < 3; r++)
  System.out.println(r);

if (board[r][c] == X)
  countX++;

在方法中,checkWin。有一个for循环。您在其中迭代0到3的行。

当循环完成时,行值变为3。 r ++工作3次,因此值为2,第4次值为3,条件失败。 在下一个声明中,您正在检查是否为

 if (board[r][c] == X)

在这里你使用的是相同的r值,当前为3,因此在3,0的位置没有元素。因此它给出了ArrayIndexOutOfBoundException。