Beispiel #1
0
        // 下棋
        public static void Play(Chessman chess)
        {
            // 保存备忘录
            mementoCaretaker.SetMemento(chess.Save());
            index++;

            Console.WriteLine("棋子 {0} 当前位置为 第 {1} 行 第 {2} 列", chess.Label, chess.X, chess.Y);
        }
Beispiel #2
0
        // 撤销悔棋
        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);
        }
Beispiel #3
0
        // 悔棋
        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);
        }
Beispiel #4
0
        public static void MultiRedoDemo()
        {
            Chessman chess = new Chessman("车", 1, 1);

            Play(chess);
            chess.Y = 4;
            Play(chess);
            chess.X = 5;
            Play(chess);

            Undo(chess, index);
            Undo(chess, index);
            Redo(chess, index);
            Redo(chess, index);
        }
Beispiel #5
0
        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);
        }
Beispiel #6
0
 public static void Display(Chessman chess)
 {
     Console.WriteLine("棋子 {0} 当前位置为:第 {1} 行 第 {2} 列", chess.Label, chess.X, chess.Y);
 }