Java MinMaxAI checkers的实现
我似乎无法让我的最小最大AI工作。这是到目前为止的代码。我得到一个错误,说我的allScores数组是空的,所以它不会将我的移动设置为得分最高的移动。任何帮助都将不胜感激Java MinMaxAI checkers的实现,java,Java,我似乎无法让我的最小最大AI工作。这是到目前为止的代码。我得到一个错误,说我的allScores数组是空的,所以它不会将我的移动设置为得分最高的移动。任何帮助都将不胜感激 int[] move; public static int makeMove(Board board, int currentPlayerColor, int maxingPlayerColor){ int score = 0; int[] moveIndex={}; if (Rules.game
int[] move;
public static int makeMove(Board board, int currentPlayerColor, int maxingPlayerColor){
int score = 0;
int[] moveIndex={};
if (Rules.gameOver(board)[0]==1){
if(Rules.gameOver(board)[1] == maxingPlayerColor){
System.out.println("1");
return score = 1;
} else if(Rules.gameOver(board)[1] == Math.abs(maxingPlayerColor-1)) {
System.out.println("-1");
return score = -1;
} else{
System.out.println("0");
return score = 0;
}
} else {
int[][] availableMoves = board.availableMoves(board, currentPlayerColor);
int[] allScores = {};
System.out.println("loop length: "+ availableMoves.length);
for(int x = 0; x<availableMoves.length; x++){
Board temp = board;
if(currentPlayerColor == maxingPlayerColor){
Board.doMove(availableMoves[x], temp, 7);
} else {
Board.doMove(availableMoves[x], temp, 0);
}
System.out.println(""+x);
allScores = add(makeMove(temp,Math.abs(maxingPlayerColor-1), maxingPlayerColor), allScores);
System.out.println("allscore len "+ allScores.length);
}
if(currentPlayerColor == maxingPlayerColor){
for(int x = 0; x<allScores.length; x++){
if(score < allScores[x]){
allScores = add(score, allScores);
}
}
for(int x = 0; x<allScores.length; x++){
if(score == allScores[x]){
moveIndex = add(x,moveIndex);
}
}
System.out.println("max "+moveIndex.length);
move = availableMoves[rand.nextInt(moveIndex.length)];
} else {
for(int x = 0; x<allScores.length; x++){
if(score > allScores[x]){
score = allScores[x];
}
}
for(int x = 0; x<allScores.length; x++){
if(score == allScores[x]){
moveIndex = add(x,moveIndex);
}
}
System.out.println("min "+moveIndex.length);
move = availableMoves[rand.nextInt(moveIndex.length)];
}
}
return 0;
}
您正在尝试将元素添加到具有
allScores = add(...
但事实并非如此。实际上,不能将元素添加到数组中,只能更改现有元素
因此,当您制作阵列时,您有:
int[] allScores = {};
但这会创建一个空数组,您无法对其执行任何操作。相反,要这样声明:
int[] allScores = new int[SIZE];
allScores[index] = someNumber;
将大小替换为希望阵列容纳的元素数
然后您可以访问和更改某些元素,如下所示:
int[] allScores = new int[SIZE];
allScores[index] = someNumber;
还有一些格式和语法错误。尝试使用包含语法突出显示的文本编辑器或IDE来查找容易出现的错误
另外,请仔细阅读以了解阵列。您能再解释一下您想要实现的目标吗?为什么它不工作?嗨,欢迎来到Stack Overflow!您肯定来对了地方,可以在编码问题上获得高质量的帮助。这一高质量标准也适用于问题,您会发现您在提出问题时投入的精力越多,其他用户在回答您的问题时投入的精力就越多。您可以参考以确保您的问题是高质量的!请始终记住包含代码试图完成的任务的描述、任何错误消息以及输入和输出的示例。再一次,欢迎!有太多的问题,我认为最好的办法是回到基础,回顾Java语法,在你回到这个程序之前做一些非常基本的问题。Add不是我的问题所在。@Quinn无论函数是什么,都不能将元素添加到数组中。您可以创建一个比上一个大一倍的新数组,复制所有旧数组元素,并将新元素放在末尾。但这是难以置信的低效,有更好的方法来做你想做的事。查找ArrayList,在此处发布问题时,确保您没有成为XY问题的受害者。