这个第一个代码是我认为你需要关注的全部代码。剩下的代码是为了防止我遗漏的东西。
编辑:确实将第二个j更改为k可以解决问题。我循环j两次,我的数组的值太大了。谢谢!
public static void makeMove(){
char[] path = new char[TicTacToeArray.length];
int[][] DefensiveOppsArray = new int[TicTacToeArray.length][TicTacToeArray.length];
for(int i=0; i < DefensiveOppsArray.length; i++){
for(int j=0; j < DefensiveOppsArray.length; j++){
DefensiveOppsArray[i][j] = 0;
}
}
for(int i=0; i < DefensiveOppsArray.length; i++){
for(int j=0; j < DefensiveOppsArray.length; j++){
//path for straight down
for(j=0; j < DefensiveOppsArray.length; j++){
path[j] = TicTacToeArray[i][j];}
DefensiveOppsArray[i][j]=DefensiveOppsArray[i][j] + 1;
}
}
}
我一直在为一个tic tac toe Java游戏做这个任务但是已经遇到了“线程中的异常”主“java.lang.ArrayIndexOutofBoundsException:3。这个错误来自行DefensiveOppsArray[i][j]=DefensiveOppsArray[i][j] + 1;
。这必然意味着我在某种程度上没有正确定义我的DefensiveOppsArray的大小。我做错了什么?
TicTacToeArray继承自UserTicTacToe。测试类只是
public class Test3{
public static void main(String[] args){
IntelligentTicTacToe2.promptUserTTT();
}
}
我需要创建一个与TicTacToeArray长度相同的DefenseOppsArray,以便我可以操纵数字,这样我就可以确定最佳移动是什么。不幸的是,我正在努力简单地创建和操作DefenseOppsArray中的数字。
public class IntelligentTicTacToe2 extends UserTicTacToe{
public static void promptUserTTT(){
//read the input size
System.out.print("Enter TicTacToe Array Size: ");
int size = UserInput.readInt();
System.out.println("");
//start the game
UserTicTacToe.startTTT(size);
//keep track of consecutive errors
int consecutiveErrors = 0;
//display the initial game board
UserTicTacToe.displayTTT();
//let the user keep playing forever if they want to
while(true){
//get the input symbol from the user
System.out.print("Enter Symbol (X or O): ");
String symbol = UserInput.readString();
System.out.println("");
//hopefully the string is just one character - if not get just
//the first character
char sym = '*';
if(symbol.length() > 0){
sym = symbol.charAt(0);
}
//if the symbol was a Q, then quit
if(sym == 'Q'){
break ;
}
//get the row and column
System.out.print("Enter Row to Place Symbol: ");
int row = UserInput.readInt();
System.out.println("");
System.out.print("Enter Col to Place Symbol: ");
int col = UserInput.readInt();
System.out.println("");
//update the game board and see if input was valid
boolean inputValid = UserTicTacToe.updateTTT(sym,row,col);
//re-display the game board if input was accepted
if(inputValid){
UserTicTacToe.scoreTTT();
UserTicTacToe.displayTTT();
consecutiveErrors = 0;
}
//if input was rejected, print a message, increment error count,
//and quit if we are on the 5th error
else{
System.out.println("Invalid Input!");
if(consecutiveErrors >= 4){
break ;
}
consecutiveErrors++;
}
makeMove();
}
}
答案 0 :(得分:2)
问题是你在嵌套循环中使用j
两次作为计数器
这是出错的地方:
for(int i=0; i < DefensiveOppsArray.length; i++){
for(int j=0; j < DefensiveOppsArray.length; j++){
//path for straight down
for(j=0; j < DefensiveOppsArray.length; j++){ // << here you use j again as counter
path[j] = TicTacToeArray[i][j];
DefensiveOppsArray[i][j]=DefensiveOppsArray[i][j] + 1;
}
}
}
也许你编写了一个循环太多了?
答案 1 :(得分:1)
你正在两次增加int j。
答案 2 :(得分:1)
问题确实是嵌套for
循环,它重用j
作为变量。代码的那部分应该格式化如下:
for(int i=0; i < DefensiveOppsArray.length; i++){
for(int j=0; j < DefensiveOppsArray.length; j++){
//path for straight down
for(j=0; j < DefensiveOppsArray.length; j++){
path[j] = TicTacToeArray[i][j];
}
DefensiveOppsArray[i][j]=DefensiveOppsArray[i][j] + 1;
}
}
这清楚地说明了发生了什么 - j
在内部for
循环中被增加到3,所以当它到达下一行时它会抛出异常。我不知道你的意思是否适合
DefensiveOppsArray[i][j]=DefensiveOppsArray[i][j] + 1;
是否在内部for
循环中,但即使您确实需要循环,也不要在内部j
循环中重用for
。