我正在尝试在没有接口atm的情况下编写Java Domino游戏,并且遇到了并发修改异常,并且在迭代时未添加或删除。而且,当我遍历它们时,我只是在打印它们的值
我设计了一个类系统,其中的类播放器具有列表。在本课程中,我删除每个玩家的多米诺骨牌。我已经尝试过使用迭代器进行迭代,但是看不到任何更改。
do {
if (counter >= players.size()) {
counter = 0;
}
Player p = players.get(counter);
System.out.println("It is " + p.getName() + " turn");
if (lastdominoes.size() == 0) {
System.out.println("There is no dominoes in the table. Please make a move:");
} else {
for (Entry<String, Domino> e : lastdominoes.entrySet()) {
System.out.print("The last dominoes played are:");
System.out.print(e.getValue().toString());
}
}
System.out.println();
System.out.println("Your dominoes are:");
int numberaux = 0;
String dominoesnumber = "";
Iterator<Domino> iteratordominoes = p.getDominoes().iterator();
while (iteratordominoes.hasNext()) {
Domino d = iteratordominoes.next();
System.out.print(d.toString());
dominoesnumber += " " + numberaux + " ";
numberaux++;
}
System.out.println();
System.out.printf(dominoesnumber);
System.out.println("Please choose a domino:");
int choosenDominoNumber = scan.nextInt();
System.out.println("You are about to move the Domino" + p.getDominoes().get(choosenDominoNumber).toString());
placeaDomino(p, p.getDominoes().get(choosenDominoNumber));
System.out.printf("\n\n");
if(p.endofGame()){endofgame=true;}
counter++;
} while (!endofgame);
这是Domino方法的地方
public boolean placeaDomino(Player p, Domino d) {
if (playedDominoes.isEmpty()) {
playedDominoes.add(d);
unplayeddominoes.remove(d);
lastdominoes.put(d.getDirection(), d);
p.makeaMove(d);
return true;
} else if (lastdominoes.get(d.getDirection()).match(d)) {
playedDominoes.add(d);
unplayeddominoes.remove(d);
lastdominoes.put(d.getDirection(), d);
p.makeaMove(d);
return true;
}
return false;
}
p.makeaMove基本上只是删除多米诺骨牌
public void makeaMove(Domino d){
this.dominoes.remove(d);
}
How many players you want to play with
2
There are 2 players in this table
It is Player 1 turn
There is no dominoes in the table. Please make a move:
Your dominoes are:
3/5 2/2 2/6 3/3 5/3 4/5 1/6
0 1 2 3 4 5 6 Please choose a domino:
2
You are about to move the Domino2/6
Then exception occurs
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1239)
at java.util.ArrayList$SubList.listIterator(ArrayList.java:1099)
at java.util.AbstractList.listIterator(AbstractList.java:299)
at java.util.ArrayList$SubList.iterator(ArrayList.java:1095)
at java.util.AbstractCollection.remove(AbstractCollection.java:282)
at dominolibrary.Player.makeaMove(Player.java:72)
at dominolibrary.Table.placeaDomino(Table.java:116)
at dominolibrary.Table.<init>(Table.java:79)
at dominolibrary.DominoLibrary.main(DominoLibrary.java:18)
Java Result: 1
我想要在每次迭代中做的是从玩家的区域中删除一个多米诺骨牌,直到一个玩家列表为空。