我相信您只需要查看第一个代码。其余的代码是为了以防我遗漏了一些东西。
编辑:确实将第二个 j 更改为 ak 可以解决问题。我将 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();
}
}