static void Main(string[] args) { MementoCaretaker mc = new MementoCaretaker(); Chessman chess = new Chessman("车", 1, 1); Display(chess); // 保存状态 mc.Memento = chess.Save(); chess.Y = 4; Display(chess); // 保存状态 mc.Memento = chess.Save(); Display(chess); chess.X = 5; Display(chess); Console.WriteLine("---------- Sorry,俺悔棋了 ---------"); // 恢复状态 chess.Restore(mc.Memento); Display(chess); Console.ReadKey(); }
// 撤销悔棋 public static void Redo(Chessman chess, int i) { Console.WriteLine("---------- Sorry,撤销悔棋 ---------"); index++; // 恢复到下一个备忘录 chess.Restore(mementoCaretaker.GetMemento(i + 1)); Console.WriteLine("棋子 {0} 当前位置为 第 {1} 行 第 {2} 列", chess.Label, chess.X, chess.Y); }
// 悔棋 public static void Undo(Chessman chess, int i) { Console.WriteLine("---------- Sorry,俺悔棋了 ---------"); index--; // 撤销到上一个备忘录 chess.Restore(mementoCaretaker.GetMemento(i - 1)); Console.WriteLine("棋子 {0} 当前位置为 第 {1} 行 第 {2} 列", chess.Label, chess.X, chess.Y); }
public static void SingleRedoDemo() { MementoCaretaker mc = new MementoCaretaker(); Chessman chess = new Chessman("车", 1, 1); Display(chess); // 保存状态 mc.Memento = chess.Save(); chess.Y = 4; Display(chess); // 保存状态 mc.Memento = chess.Save(); Display(chess); chess.X = 5; Display(chess); Console.WriteLine("---------- Sorry,俺悔棋了 ---------"); // 恢复状态 chess.Restore(mc.Memento); Display(chess); }