gpt4 book ai didi

java - 蛇与梯子 - 在计算机转弯和掷骰子上添加延迟

转载 作者:行者123 更新时间:2023-11-30 02:04:35 24 4
gpt4 key购买 nike

我正在创建游戏“蛇与梯子”。我使用的游戏模式可以让你与电脑对战。

当人类玩家掷骰子时,将调用 rollTheDiceAndMove 方法。我们等待 1 秒(骰子正在滚动......)。然后我们调用 move 函数,该函数实际上将棋子移动到指定的图 block 上。如果蛇或梯子被击中,我想再等 1 秒再去最后一 block 瓷砖。例如。我等待 1 秒,掷出 5 个,然后将 my_current_pos + dice_roll 放在前面,然后移动该棋子。如果蛇或梯子击中:再次延迟并再次递归移动。

最后,如果选择“对抗计算机”模式,当人类玩家移动时,我想再等一秒钟,计算机才会自动掷骰子。如下所示,我正在使用 TimerTimerTask 类,但它非常复杂,因为 Timer 范围内的任何内容都会在一段时间后执行,但是计时器之外的代码会立即执行,这导致了许多错误和异步。

您建议使用什么来创建这种延迟?

public void rollTheDiceAndMove() {
int diceRoll = gameBoard.rollDice();
// delay for dice roll.

new Timer().schedule(
new TimerTask() {
@Override
public void run() {
Platform.runLater(new Runnable() {
@Override
public void run() {
////////////////////////////////////////////////////////////////////////////

gameGUI.indicateDiceRoll(diceRoll);
int newIndex = getPlayerIndexAfterRoll(diceRoll);
move(newIndex);
System.out.println("change turns");
swapTurns();
System.out.println(isComputerTurn());
gameGUI.updateCurrentTurnLabel();


if (newIndex == GameBoard.WIN_POINT) {
boolean restartGame = gameBoard.playAgainOrExit();

if (restartGame) {
Player winner = gameBoard.getCurrentPlayer();
gameGUI.updateScore(winner);
gameGUI.playAgain();
} else {
System.exit(0);
}
}

////////////////////////////////////////////////////////////////////////////
}
});
}
}, GameBoard.DICE_ROLL_DELAY
);

// try to recursively call this method again for computer turn.
}


public void move(int currentIndex) {
int[] newCoordinates = gameBoard.getBoardCoordinates(GameBoard.NUMBER_OF_TILES - currentIndex);

gameBoard.getCurrentPlayer().getPlayerPiece().setPosition(currentIndex);
gameGUI.movePieceImages(newCoordinates[0], newCoordinates[1]);

if (gameBoard.getTile(currentIndex).containsLadderOrSnake()) {
// delay for moving to second tile.

new Timer().schedule(
new TimerTask() {
@Override
public void run() {
Platform.runLater(new Runnable() {
@Override
public void run() {

int updatedIndex = gameBoard.getUpdatedPosition(currentIndex);
move(updatedIndex);


}
});
}
}, GameBoard.SECOND_MOVE_DELAY *2
);

return; // we need to return 'cause of recursion. Swap turns will be executed twice, one time for the initial call and the other time on above line.
}
}

最佳答案

创建项目的副本并尝试使用 PauseTransition

public void rollTheDiceAndMove() 
{
int diceRoll = gameBoard.rollDice();
System.out.println("Player: rolling the dice");

PauseTransition pause = new PauseTransition(Duration.seconds(1));
pause.setOnFinished(event ->{
System.out.println("1 second after rolling the dice");
gameGUI.indicateDiceRoll(diceRoll);
int newIndex = getPlayerIndexAfterRoll(diceRoll);
playerMove(newIndex);
if(checkWin(Player))
{
System.out.println("Player won!");
}
else
{
System.out.println("change turns");
swapTurns();
System.out.println(isComputerTurn());
gameGUI.updateCurrentTurnLabel();
computerRollDiceAndMove();
}






});
pause.play();
}


public void computerRollDiceAndMove()
{
int diceRoll = gameBoard.rollDice();
System.out.println("Computer: rolling the dice");

PauseTransition pause = new PauseTransition(Duration.seconds(1));
pause.setOnFinished(event ->{
System.out.println("1 second after rolling the dice");
gameGUI.indicateDiceRoll(diceRoll);
int newIndex = getComputerIndexAfterRoll(diceRoll);
computerMove(newIndex);
if(checkWin(computer))
{
System.out.println("Computer won!");
}
else{
System.out.println(isComputerTurn());
gameGUI.updateCurrentTurnLabel();
}
});
pause.play();
}

关于java - 蛇与梯子 - 在计算机转弯和掷骰子上添加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51725215/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com