我正在開發一個tic tac toe遊戲,並且想要實現撤消方法。我覺得做這件事的最好方法就是設置另一個(多個)堆棧,並複製剛剛發生的「移動」。然後,如果撤消被調用,只需彈出最後一步並重新填充遊戲板。Java - 使用多個堆棧以允許「撤消」方法
所以是的,我有想法,但無法弄清楚如何實現它。
一些什麼,我有:
設置:
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;
}
然後有一個板的方法,建立董事會,顯示方法,當然一個的連續檢查3 。
感謝您的任何意見
尋找GOF中的紀念品設計圖案 – pastjean 2010-12-11 02:32:52