我试图在Java中为connect 4编写一个minimax算法,但在尝试迭代可能的移动列表时遇到错误。
它说
线程中的异常" main" java.util.ConcurrentModificationException at java.util.ArrayList $ Itr.checkForComodification(Unknown Source)at java.util.ArrayList $ Itr.next(未知来源)
我已经在网上看了一下,发现了一些事情(关于更新迭代器中的对象),但我不太清楚要做什么。
这是我的代码。
public class AI {
public Move minimax(Board board, int depth, int alpha, int beta, String player, Move move) {
if(depth == 0 || board.getScore() >= 512) {
return move;
}
else if(player.equals("computer")) {
Move maxMove = new Move(0,0);
maxMove.moveScore = -1000000;
Iterator<Move> it = board.possibleMoves.iterator();
while(it.hasNext()) {
Move m = it.next();
board.putPiece(player, m.yMove);
m.moveScore = board.getScore();
if (board.getScore() < minimax(board, depth-1, alpha, beta, "human", m).moveScore) {
maxMove = m;
}
else {
maxMove = minimax(board, depth-1, alpha, beta, "human", m);
}
board.removePiece(m);
alpha = Math.max(alpha, maxMove.moveScore);
if (alpha >= beta) {
break;
}
}
return maxMove;
}
else {
Move minMove = new Move(100, 100);
minMove.moveScore = 1000000;
for(Move m : board.possibleMoves) {
board.putPiece(player, m.yMove);
m.moveScore = board.getScore();
if (m.moveScore < minimax(board, depth-1, alpha, beta, "computer", m).moveScore) {
minMove = m;
}
else {
minMove = minimax(board, depth-1, alpha, beta, "computer", m);
}
board.removePiece(m);
beta = Math.min(beta, minMove.moveScore);
if(alpha >= beta) {
break;
}
}
return minMove;
}
}
}
感谢任何和所有帮助。
谢谢,