有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我应该如何避免java。lang.ArrayIndexOutOfBoundsException尝试调用二维数组中可能的空元素时发生异常

我正在尝试做一个井字游戏,我是java新手。我在方法中的第一个if语句isHorizontalWin中得到了突破,我假设它也会在isVerticalWin中发生。如果我错了,请纠正我,但我相信它正在发生,因为gameboard的一部分没有价值,因此if语句超出边界,但我不确定如何修复此问题。附带说明:粘贴代码时,格式有点混乱,因此我不得不将一些JoptionPane放在多行上以适应。谢谢

import javax.swing.JOptionPane;
    public class TicTacToe
    {
    public static void main(String[] args)
    {

    char gameboard[][] = new char[3][3];
    printCurrentState(gameboard);
    int Turn = 0;
    int gameon=0;

    while(gameon<9)
    {
        if(Turn==0)
        {
            JOptionPane.showMessageDialog(null, "Player 1's turn aka X");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Player 2's turn aka O");

        }
        boolean proceed=true;   
        String yo =JOptionPane.showInputDialog("Enter the row and column"+ "one"+ 
"after another. Example-for row 1 and comlumn 2 type \"12\"");
        if(yo.length()!=2)
        {
            JOptionPane.showMessageDialog(null, "You did not put the row"+ "and column one after another- Try again");
        proceed=false;  
        }
        if(proceed==true) {
        String aa= yo.charAt(0)+"";
        int x = Integer.parseInt(aa)-1;
        String ba= yo.charAt(1)+"";
        int y = Integer.parseInt(ba)-1;
        if((x < 0 || x > 2)||(y<0||y>2))
        {
            JOptionPane.showMessageDialog(null, "Please enter a row and"+ 
"column that both at least 1 and no bigger than 3. Try again!");
            yo =JOptionPane.showInputDialog("Enter the row and column one"+ 
"after another. Example-for row 1 and comlumn 2 type \"12\"");
             aa= yo.charAt(0)+"";
             x = Integer.parseInt(aa)-1;
             ba= yo.charAt(1)+"";
             y = Integer.parseInt(ba)-1;
        }
            if(gameboard[x][y] == 'X' || gameboard[x][y] == 'O')
            {
                JOptionPane.showMessageDialog(null, "That is already taken"+ 
"by an "+ gameboard[x][y]+". Go again!");
                yo =JOptionPane.showInputDialog("Enter the row and column"+ 
"one after another. Example-for row 1 and comlumn 2 type \"12\"");
                 aa= yo.charAt(0)+"";
                 x = Integer.parseInt(aa)-1;
                 ba= yo.charAt(1)+"";
                 y = Integer.parseInt(ba)-1;

            }
        if(Turn== 0)
        {
            gameboard[x][y] = 'X';
            printCurrentState(gameboard);
            isHorizontalWin(gameboard);
            isVerticalWin(gameboard);
            isDiagnolWin(gameboard);
            Turn++;
        }
        else if(Turn == 1)
        {
            gameboard[x][y] = 'O';

            printCurrentState(gameboard);
            isHorizontalWin(gameboard);
            isVerticalWin(gameboard);
            isDiagnolWin(gameboard);
            Turn--;
        }
        gameon++;
        }
    }
    if(isHorizontalWin(gameboard)==false&&isVerticalWin(gameboard)==false&&isDiagnolWin(gameboard)==false)
    {
        JOptionPane.showMessageDialog(null, "There was no winner this game ");
    }
}
public static void printCurrentState(char gameboard[][])
{
    System.out.println(" COLUMN");
    System.out.println(" 1  2  3");

    System.out.print(gameboard[0][0] + " | " + gameboard[0][1] + " | " + gameboard[0][2] +" 1 R"+ "\n" 
                    + "--|---|--\n" +
                     gameboard[1][0] + " | " + gameboard[1][1] + " | " + gameboard[1][2] +" 2 O"+ "\n"
                    + "--|---|--\n" +
                      gameboard[2][0] + " | " + gameboard[2][1] + " | " + gameboard[2][2] +" 3 W "+"\n");
    System.out.println();
}
public static boolean isHorizontalWin(char[][] gameboard)
{
    int tally[][]= new int[3][3];
    for(int r = 0; r < 3; r++)
    {
        for(int c = 0; c < 3; r++)
        {
            if(gameboard[r][c]=='O')
            {
                tally[r][c]=10;
            }
            else if(gameboard[r][c]=='X')
            {
                tally[r][c]=1;
            }
            else
            {
                tally[r][c]=0;
            }
        }
    }
    int c=0;
    for(int m = 0; m < 3; m++)
    {
        c++;
        int cool = 0;
        for(int n = 0; n < 3; n++)
        {
            cool += tally[m][n];
            if(cool == 30)
            {
                JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a horizontal win in row " +c);
                System.exit(0);
                return true;
            }
            else if(cool==3)
            {

                JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a horizontal win in row " + c);
                System.exit(0);
                return true;
            }
        }
    }
    return false;
}
public static boolean isVerticalWin(char[][] gameboard)
{
    int tally[][]= new int[3][3];
    for(int r = 0; r < 3; r++)
    {
        for(int c = 0; c < 3; r++)
        {
            if(gameboard[r][c]=='O')
            {
                tally[r][c]=10;
            }
            else if(gameboard[r][c]=='X')
            {
                tally[r][c]=1;
            }
            else
            {
                tally[r][c]=0;
            }
        }
    }
    int r=0;
    for(int m = 0; m < 3; m++)
    {
        r++;
        int cool = 0;
        for(int n = 0; n < 3; n++)
        {
            cool += tally[n][m];
            if(cool == 30)
            {
                JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a vertical win in column " +r);
                System.exit(0);
                return true;
            }
            else if(cool==3)
            {

                JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a vertical win in column " + r);
                System.exit(0);
                return true;
            }
        }
    }
    return false;
}
public static boolean isDiagnolWin(char[][] gameboard)
{
    if((gameboard[0][0]=='O'&&gameboard[1][1]=='O'&&gameboard[2][2]=='O')||(gameboard[0][2]=='O'&&gameboard[1][1]=='O'&&gameboard[3][1]=='O'))
    {
        JOptionPane.showMessageDialog(null, "Player 2(O) is the winner with a diagonal win" );
        System.exit(0);
        return true;
    }
    if((gameboard[0][0]=='X'&&gameboard[1][1]=='X'&&gameboard[2][2]=='X')||(gameboard[0][2]=='X'&&gameboard[1][1]=='X'&&gameboard[2][0]=='X'))
    {
        JOptionPane.showMessageDialog(null, "Player 1(X) is the winner with a diagonal win" );
        System.exit(0);
        return true;
    }

    return false;
}

}


共 (1) 个答案

  1. # 1 楼答案

    在IsOrthOnthalWin中,你可能忘记了在第二个循环中把R++变成C++。 这会导致r增加太多倍。 因此,当r达到一个大于条目数的数字时,索引(r)就超出了范围

    for(int r = 0; r < 3; r++)
        {
            for(int c = 0; c < 3; r++)
            {
    

    需要:

    for(int r = 0; r < 3; r++)
        {
            for(int c = 0; c < 3; c++)
            {