备忘录模式—悔棋操作

备忘录:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。

通俗的讲就像课本举得例子:一般玩单机游戏都会有存档这个选项,这就可以利用备忘录模式来实现;一些撤销,悔棋操作,都是回到之前的状态。

下面来说一下象棋中的悔棋操作如何利用备忘录模式来实现。

package 象棋悔棋操作;

public class GameRole {
	private String name;
	private int x;
	private int y;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public void StateDisplay() {
		System.out.println("这颗棋子的信息:"+this.name+"("+this.x+","+this.y+")");
	}
	public void GetInitState() {
		this.name="马";
		this.x=2;
		this.y=1;
	}
	public void Run1() {
		this.name="马";
		this.x=1;
		this.y=2;
	}
	public void Run2() {
		this.name="马";
		this.x=3;
		this.y=2;
	}
	public void Run3() {
		this.name="马";
		this.x=4;
		this.y=1;
	}
	public RoleStateMemento SaveState() {
		return (new RoleStateMemento(name,x,y));
	}
	public void RecoveryState(RoleStateMemento memento) {
		this.name=memento.getName();
		this.x=memento.getX();
		this.y=memento.getY();
	}

}


package 象棋悔棋操作;

public class RoleStateMemento {
		private String name;
		private int x;
		private int y;
		public RoleStateMemento(String name, int x, int y) {
			super();
			this.name = name;
			this.x = x;
			this.y = y;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getX() {
			return x;
		}
		public void setX(int x) {
			this.x = x;
		}
		public int getY() {
			return y;
		}
		public void setY(int y) {
			this.y = y;
		}
		

}


package 象棋悔棋操作;

public class RoleStateCaretaker {
	private RoleStateMemento memento;
	public RoleStateMemento getMemento() {
		return memento;
	}
	public void setMemento(RoleStateMemento memento) {
		this.memento = memento;
	}
	

}



package 象棋悔棋操作;

public class Test {

	public static void main(String[] args) {
		System.out.println("开始时棋子的位置:");
		GameRole a1=new GameRole();
		a1.GetInitState();
		a1.StateDisplay();
		
		RoleStateCaretaker stateAdmin=new RoleStateCaretaker();
		stateAdmin.setMemento(a1.SaveState());
		System.out.println("移动后棋子的位置:");
		a1.Run1();
		a1.StateDisplay();
		System.out.println("悔棋后棋子的位置:");
		a1.RecoveryState(stateAdmin.getMemento());
		a1.StateDisplay();
		// TODO Auto-generated method stub

	}

}

运行结果:

uml图:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
备忘录模式是一种行为设计模式,它允许在不破坏对象封装的前提下保存和恢复对象之前的状态。在象棋游戏中,可以使用备忘录模式实现悔棋功能。 下面是一个简单的 Java 实现: 首先,我们需要定义一个备忘录类,用于存储棋盘状态: ```java public class ChessboardMemento { private String[][] boardState; public ChessboardMemento(String[][] boardState) { this.boardState = boardState; } public String[][] getBoardState() { return boardState; } } ``` 然后,我们需要一个游戏状态管理类,用于保存和恢复游戏状态: ```java public class ChessboardCaretaker { private List<ChessboardMemento> mementoList = new ArrayList<>(); public void saveState(String[][] boardState) { mementoList.add(new ChessboardMemento(boardState)); } public String[][] restoreState(int index) { return mementoList.get(index).getBoardState(); } } ``` 最后,我们需要一个游戏类,用于实现悔棋功能: ```java public class ChessGame { private String[][] boardState = new String[8][8]; private ChessboardCaretaker caretaker = new ChessboardCaretaker(); public ChessGame() { // 初始化棋盘状态 // ... } public void makeMove(int x1, int y1, int x2, int y2) { // 执行棋子移动操作 // ... // 保存棋盘状态 caretaker.saveState(boardState); } public void undo() { // 恢复上一次的棋盘状态 boardState = caretaker.restoreState(caretaker.size() - 1); } } ``` 这样,我们就可以在游戏中实现悔棋功能了。每次执行棋子移动操作时,都会保存当前的棋盘状态。当需要悔棋时,只需要恢复上一次的棋盘状态即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值