在尝试调用2D数组中可能的void元素时,如何避免java.lang.ArrayIndexOutOfBoundsException
我正在尝试做一个井字游戏,我是java新手。我在方法中的第一个if语句isHorizontalWin中得到了突破,我假设它也会在isVerticalWin中发生。如果我错了,请纠正我,但我相信它正在发生,因为gameboard的一部分没有价值,因此if语句超出边界,但我不确定如何修复此问题。附带说明:粘贴代码时,格式有点混乱,因此我不得不将一些JoptionPane放在多行上以适应。谢谢在尝试调用2D数组中可能的void元素时,如何避免java.lang.ArrayIndexOutOfBoundsException,java,Java,我正在尝试做一个井字游戏,我是java新手。我在方法中的第一个if语句isHorizontalWin中得到了突破,我假设它也会在isVerticalWin中发生。如果我错了,请纠正我,但我相信它正在发生,因为gameboard的一部分没有价值,因此if语句超出边界,但我不确定如何修复此问题。附带说明:粘贴代码时,格式有点混乱,因此我不得不将一些JoptionPane放在多行上以适应。谢谢 import javax.swing.JOptionPane; public class TicTa
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;
}
import javax.swing.JOptionPane;
公共类Tictatcoe
{
公共静态void main(字符串[]args)
{
char gameboard[][]=新字符[3][3];
printCurrentState(游戏板);
整圈=0;
int gameon=0;
while(gameon 2)| |(y2))
{
JOptionPane.showMessageDialog(null,“请输入一行并”+
“至少为1且不大于3的列。请重试!”;
yo=JOptionPane.showInputDialog(“输入第一行和第一列”+
“一个接一个。第1行和第2列的示例类型为\“12\”);
aa=yo.charAt(0)+;
x=Integer.parseInt(aa)-1;
ba=yo.charAt(1)+”;
y=整数。parseInt(ba)-1;
}
如果(游戏板[x][y]='x'| |游戏板[x][y]='O')
{
JOptionPane.showMessageDialog(null,“已获取”+
“通过一个“+游戏板[x][y]+”。再去一次!”;
yo=JOptionPane.showInputDialog(“输入行和列”+
“一个接一个。第1行和第2列的示例类型为\“12\”);
aa=yo.charAt(0)+;
x=Integer.parseInt(aa)-1;
ba=yo.charAt(1)+”;
y=整数。parseInt(ba)-1;
}
如果(转动==0)
{
游戏板[x][y]=“x”;
printCurrentState(游戏板);
isHorizontalWin(游戏板);
isVerticalWin(游戏板);
isDiagnolWin(游戏板);
Turn++;
}
否则如果(圈数==1)
{
游戏板[x][y]=“O”;
printCurrentState(游戏板);
isHorizontalWin(游戏板);
isVerticalWin(游戏板);
isDiagnolWin(游戏板);
转--;
}
gameon++;
}
}
如果(isHorizontalWin(gameboard)==false和isVerticalWin(gameboard)==false和isDiagnolWin(gameboard)==false)
{
showMessageDialog(null,“本游戏没有赢家”);
}
}
公共静态无效printCurrentState(char gameboard[])
{
System.out.println(“列”);
System.out.println(“1 2 3”);
系统输出打印(gameboard[0][0]+“|”+gameboard[0][1]+“|”+gameboard[0][2]+“1R”+“\n”
+“——————————\n”+
游戏板[1][0]+“|”+游戏板[1][1]+“|”+游戏板[1][2]+“2 O”+“\n”
+“——————————\n”+
游戏板[2][0]+“|”+游戏板[2][1]+“|”+游戏板[2][2]+“3 W”+“\n”);
System.out.println();
}
公共静态布尔isHorizontalWin(char[][]游戏板)
{
整数计数[][]=新整数[3][3];
对于(int r=0;r<3;r++)
{
对于(int c=0;c<3;r++)
{
如果(游戏板[r][c]='O')
{
理货数量[r][c]=10;
}
else if(游戏板[r][c]='X')
{
计数[r][c]=1;
}
其他的
{
计数[r][c]=0;
}
}
}
int c=0;
对于(int m=0;m<3;m++)
{
C++;
int cool=0;
对于(int n=0;n<3;n++)
{
冷却+=计数[m][n];
如果(冷==30)
{
showMessageDialog(null,“玩家2(O)是第行中水平获胜的赢家”+c);
系统出口(0);
返回true;
}
否则如果(冷==3)
{
showMessageDialog(null,“玩家1(X)是第行中水平获胜的赢家”+c);
系统出口(0);
返回true;
}
}
}
返回false;
}
公共静态布尔值isVerticalWin(char[][]游戏板)
{
整数计数[][]=新整数[3][3];
对于(int r=0;r<3;r++)
{
对于(int c=0;c<3;r++)
{
如果(游戏板[r][c]='O')
{
理货数量[r][c]=10;
}
else if(游戏板[r][c]='X')
{
计数[r][c]=1;
}
其他的
{
计数[r][c]=0;
}
}
}
int r=0;
对于(int m=0;m<3;m++)
{
r++;
int cool=0;
对于(int n=0;n<3;n++)
{
冷却+=计数[n][m];
如果(冷==30)
{
showMessageDialog(null,“玩家2(O)是在“+r”列中垂直获胜的赢家);
系统出口(0);
返回true;
}
否则如果(冷==3)
{
showMessageDialog(null,“玩家1(X)是在“+r”列中垂直获胜的赢家);
系统出口(0);
返回true;
}
}
}
返回false;
}
公共静态布尔值isDiagnolWin(char[][]游戏板)
{
如果((游戏板[0][0]='O'和游戏板[1][1]='O'和游戏板[2][2]='O')|(游戏板[0][2]='O'和游戏板[1][1]='O'和游戏板[3][1]='O'))
{
showMessageDialog(null,“玩家2(O)是对角线获胜的赢家”);
系统出口(0);
返回true;
}
如果((游戏板[0][0]='X'&游戏板[1][1]='X'&游戏板[2][2]='X')|(游戏板[0][2]='X'&游戏板[1][1]='X'&游戏板[2][0]='X'))
{
showMessageDialog(空,“播放器1(X
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++)
{