【发布时间】:2011-05-23 19:51:10
【问题描述】:
我正在开发一个井字游戏,我想实现一个撤消方法。我认为最好的方法是设置另一个(多个?)堆栈,并复制刚刚发生的“移动”。然后,如果调用 undo,只需弹出最后一步并重新填充游戏板。
是的,我有这个想法,但不知道如何实现它。
我拥有的一些东西:
设置:
public void set(Position p, int v, int n) throws IOException {
if (board[p.x][p.y][p.z]!= 0) throw new IOException("Position taken");
//Restrict 222 until all other's have been used
if (n != 26) {
if (p.x == 1 && p.y == 1 && p.z ==1) {
throw new IOException("[2,2,2] cannot be played until all other positions have been taken");
}
}
//Enforce x=1 for first 9, x=3 for next 9
if (n < 9 ) {
if (p.x != 0) throw new IOException("Please play on x=1 for the first 9 moves");
}
if (n >= 9 && n < 18) {
if (p.x != 2) throw new IOException("Please play on x=3 for the first 9 moves");
}
board[p.x][p.y][p.z] = v;
}
然后有一个board方法来搭建board,一个display方法,当然还有一个可以连续检查3个的方法。
感谢您的建议
【问题讨论】:
-
在 GOF 中搜索 Memento 设计模式
标签: java stack undo tic-tac-toe