设计模式-备忘录模式实现悔棋操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式-备忘录模式实现悔棋操作相关的知识,希望对你有一定的参考价值。

利用设计模式中的备忘录模式实现多步悔棋的操作

  1 import java.util.*;
  2 class Chessman {
  3     private String label;
  4     private int x;
  5     private int y;
  6     public static int index=-1;
  7     public Chessman(String label,int x,int y) {
  8         this.label = label;
  9         this.x = x;
 10         this.y = y;
 11     }
 12 
 13     public void setLabel(String label) {
 14         this.label = label; 
 15     }
 16 
 17     public void setX(int x) {
 18         this.x = x; 
 19     }
 20 
 21     public void setY(int y) {
 22         this.y = y; 
 23     }
 24 
 25     public String getLabel() {
 26         return (this.label); 
 27     }
 28 
 29     public int getX() {
 30         return (this.x); 
 31     }
 32 
 33     public int getY() {
 34         return (this.y); 
 35     }
 36     
 37     //保存状态
 38     public ChessmanMemento save() {
 39         return new ChessmanMemento(this.label,this.x,this.y);
 40     }
 41     
 42     //恢复状态
 43     public void restore(ChessmanMemento memento) {
 44         this.label = memento.getLabel();
 45         this.x = memento.getX();
 46         this.y = memento.getY();
 47     }
 48     public void display() {
 49         System.out.println("棋子" + label + "当前位置为:" + "第" + x + "行" + "第" + y + "列。");
 50     }
 51     public void play(MementoCaretaker mc,int toX,int toY) {
 52         index ++;
 53         mc.setMemento(this.save()); //保存备忘录
 54         this.setX(toX);
 55         this.setY(toY);
 56         System.out.println("棋子" + label + "当前位置为:" + "第" + x + "行" + "第" + y + "列。");
 57     }
 58     public void undo(MementoCaretaker mc,int i) {
 59         try
 60         {
 61             mc.getMemento(i+2);
 62         }
 63         catch(Exception e)
 64         {
 65             mc.setMemento(this.save()); //保存备忘录
 66         }
 67         System.out.println("******悔棋******");
 68         this.index --; 
 69         this.restore(mc.getMemento(i)); //撤销到上一个备忘录
 70         System.out.println("棋子" + label + "当前位置为:" + "第" + x + "行" + "第" + y + "列。");
 71         }
 72         
 73     //撤销悔棋
 74     public void redo(MementoCaretaker mc,int i) {
 75         System.out.println("******撤销悔棋******");    
 76         this.restore(mc.getMemento(i+2)); //恢复到下一个备忘录
 77         this.index ++; 
 78         System.out.println("棋子" + label + "当前位置为:" + "第" + x + "行" + "第" + y + "列。");
 79         }
 80 }
 81 
 82 //象棋棋子备忘录类:备忘录
 83 class ChessmanMemento {
 84     private String label;
 85     private int x;
 86     private int y;
 87 
 88     public ChessmanMemento(String label,int x,int y) {
 89         this.label = label;
 90         this.x = x;
 91         this.y = y;
 92     }
 93 
 94     public void setLabel(String label) {
 95         this.label = label; 
 96     }
 97 
 98     public void setX(int x) {
 99         this.x = x; 
100     }
101 
102     public void setY(int y) {
103         this.y = y; 
104     }
105 
106     public String getLabel() {
107         return (this.label); 
108     }
109 
110     public int getX() {
111         return (this.x); 
112     }
113 
114     public int getY() {
115         return (this.y); 
116     }    
117 }
118 
119 class MementoCaretaker {
120     //定义一个集合来存储多个备忘录
121     private ArrayList mementolist = new ArrayList();
122 
123     public ChessmanMemento getMemento(int i) {
124         return (ChessmanMemento)mementolist.get(i);
125     }
126 
127     public void setMemento(ChessmanMemento memento) {
128         mementolist.add(memento);
129     }
130 }
131 
132 
133 class Client {
134         public static void main(String args[]) {
135         MementoCaretaker mc = new MementoCaretaker();
136         Chessman chess = new Chessman("车",1,1);//ArrayList[0]的内容,index=-1;
137         chess.display();
138         chess.play(mc,1,2);//ArrayList[1]的内容,index=0        
139         chess.play(mc,2,3);//ArrayList[2]的内容,index=1
140         chess.play(mc,4,5);//ArrayList[3]的内容,index=2    (还没存);
141         chess.undo(mc,chess.index);//2,1
142         chess.undo(mc,chess.index);//1,0
143         chess.undo(mc,chess.index);//0,-1
144         chess.redo(mc,chess.index);//需要ArrayList[1]的内容,此时index=-1;所以i要+2,index++得0
145         chess.redo(mc,chess.index);//需要ArrayList[2]的内容,此时index为0,---------,index++得1
146         chess.redo(mc,chess.index);
147     }
148 } 

思路参考--刘伟技术博客

 

以上是关于设计模式-备忘录模式实现悔棋操作的主要内容,如果未能解决你的问题,请参考以下文章

备忘录模式

设计模式的征途—20.备忘录(Memento)模式

打游戏要存进度-备忘录模式

设计模式之备忘录模式

(十三)备忘录模式-代码实现

19-备忘录(Memento)模式Ruby实现