我正在制作一个棋盘游戏,我在其中使用按钮来创建实际的棋盘本身。所有空的棋盘位置均具有白色的按钮背景色,并且所有占用的棋盘位置均以玩家颜色上色。现在,控制用户移动的代码可以正常工作,AI类也可以正常工作。方法aiTurn()返回一个包含char的对象,以使类知道正在进行的移动类型以及字符从何处移动到的索引。
该方法的代码和过程正常工作,但是自从添加for循环(以确保AI完全完成转向)以来,仅for循环的最后一次迭代似乎会更新按钮和所有以前的背景色迭代被忽略。例如,如果ai掷出6并将棋子从起始位置移动到主板,并且结果再次滚动并掷出2,则结果应该是将其取自的起始位置应设置为白色,然后主板2上的按钮应设置为播放器颜色。但是,仅设置主板按钮的颜色,因为在第二次滚动后在第二次迭代中完成了此操作,并且在第一次迭代中主按钮的初始颜色未更改为白色。该方法的代码如下:
public void onClickNextTurn(View view){
playerTurn=2;
boolean turnFinished=false;
for (int x=0; x<2 && !turnFinished; x++) {
diceRoll();
MoveTriplet result = ai[0].aiTurn(this.boardPieceLocations, this.homeLocations, this.finishLocations, this.roll, 2);
if (result.getMoveType() == 'a') { //The Artificial Opponent is moving from one board location to another AND gets to roll again
this.boardPieceLocations[result.getPreviousIndex()] = 0;
this.boardPieceLocations[result.getNextIndex()] = playerTurn;
this.buttons[result.getPreviousIndex()].setBackgroundColor(getResources().getColor(R.color.colorHoles));
this.buttons[result.getNextIndex()].setBackgroundColor(colors[playerTurn - 1]);
} else if (result.getMoveType() == 'b') { //The Artificial Opponent is moving from one home location to the board AND gets to roll again
System.out.println("Player turn= " + playerTurn);
System.out.println("Next Location= " + result.getNextIndex());
this.homeLocations[playerTurn - 1][result.getPreviousIndex()] = 0;
this.boardPieceLocations[result.getNextIndex()] = playerTurn;
this.homeButtons[playerTurn - 1][result.getPreviousIndex()].setBackgroundColor(getResources().getColor(R.color.colorHoles));
this.buttons[result.getNextIndex()].setBackgroundColor(colors[playerTurn - 1]);
} else if (result.getMoveType() == 'c') { //The Artificial Opponent is moving from one board location to finish location AND gets to roll again
this.boardPieceLocations[result.getPreviousIndex()] = 0;
this.finishLocations[playerTurn - 1][result.getNextIndex()] = playerTurn;
this.buttons[result.getPreviousIndex()].setBackgroundColor(getResources().getColor(R.color.colorHoles));
this.finishButtons[playerTurn - 1][result.getNextIndex()].setBackgroundColor(colors[playerTurn - 1]);
} else if (result.getMoveType() == 'd') { //The Artificial Opponent is moving from one finish location to another AND gets to roll again
this.finishLocations[playerTurn - 1][result.getPreviousIndex()] = 0;
this.finishLocations[playerTurn - 1][result.getNextIndex()] = playerTurn;
this.finishButtons[playerTurn - 1][result.getPreviousIndex()].setBackgroundColor(getResources().getColor(R.color.colorHoles));
this.finishButtons[playerTurn - 1][result.getNextIndex()].setBackgroundColor(colors[playerTurn - 1]);
} else if (result.getMoveType() == 'e') { //The Artificial Opponent is moving from one board location to finish location
this.finishLocations[playerTurn - 1][result.getPreviousIndex()] = 0;
this.finishLocations[playerTurn - 1][result.getNextIndex()] = playerTurn;
this.finishButtons[playerTurn - 1][result.getPreviousIndex()].setBackgroundColor(getResources().getColor(R.color.colorHoles));
this.finishButtons[playerTurn - 1][result.getNextIndex()].setBackgroundColor(colors[playerTurn - 1]);
turnFinished=true;
} else if (result.getMoveType() == 'f') { //The Artificial Opponent is moving from one finish location to another
this.finishLocations[playerTurn - 1][result.getPreviousIndex()] = 0;
this.finishLocations[playerTurn - 1][result.getNextIndex()] = playerTurn;
this.finishButtons[playerTurn - 1][result.getPreviousIndex()].setBackgroundColor(getResources().getColor(R.color.colorHoles));
this.finishButtons[playerTurn - 1][result.getNextIndex()].setBackgroundColor(colors[playerTurn - 1]);
turnFinished=true;
} else if (result.getMoveType() == 'h') { //The Artificial Opponent is moving from one home location to the board
this.homeLocations[playerTurn - 1][result.getPreviousIndex()] = 0;
this.boardPieceLocations[result.getNextIndex()] = 2;
this.homeButtons[playerTurn - 1][result.getPreviousIndex()].setBackgroundColor(getResources().getColor(R.color.colorHoles));
this.buttons[result.getNextIndex()].setBackgroundColor(colors[playerTurn - 1]);
turnFinished=true;
} else if (result.getMoveType() == 'r') { //The Artificial Opponent is moving from one board location to another
this.boardPieceLocations[result.getPreviousIndex()] = 0;
this.boardPieceLocations[result.getNextIndex()] = 2;
this.buttons[result.getPreviousIndex()].setBackgroundColor(getResources().getColor(R.color.colorHoles));
this.buttons[result.getNextIndex()].setBackgroundColor(colors[playerTurn - 1]);
turnFinished=true;
}
//Else to just roll again with no movement
}
我的问题是什么导致Prorgam仅在for循环的最后一次迭代中更新按钮的颜色,我该如何解决?预先谢谢你