1. 程式人生 > >設計模式-備忘錄模式實現悔棋操作

設計模式-備忘錄模式實現悔棋操作

exc turn color new label isp lis args set

利用設計模式中的備忘錄模式實現多步悔棋的操作

  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 }

思路參考--劉偉技術博客

設計模式-備忘錄模式實現悔棋操作