所以我有一个国际象棋游戏。我正在尝试实现一种方法,用户可以在其中撤消他们的最后一步。
我为菜单选项创建了适当的 Action 监听器。 Action 监听器将游戏的撤消 boolean 值设置为 true。
我的游戏循环的伪代码:
while (!game end)
{
if (undo)
{
undo the last move
continue;
}
prompt user to input next move
user inputs move into console
model, view are updated
}
这个逻辑不成立。游戏循环处理撤消的唯一时间是在用户输入下一步之后。所以.. 基本上它只是撤消了他们刚刚输入的 Action 。我需要这样做,以便每当用户在 GUI 上单击撤消移动时,游戏循环都会执行 if 语句中的操作。
我觉得有一种更聪明的方法来处理这个问题。
最佳答案
听起来这个阶段正在阻止您的 while
循环执行
prompt user to input next move
user inputs move into console
我认为您需要研究线程
,特别是为移动输入、绘制和撤消创建单独的线程。或者找到一种方法让用户在不阻塞 while 循环执行的情况下输入他们的内容。
这里有一个很好的并发教程 http://docs.oracle.com/javase/tutorial/essential/concurrency/
关于java - 游戏循环中的 "Undo move",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896290/