Memento
// Chess.cpp
#ifndef _CHESS_H
#define _CHESS_H
#include <vector>
using namespace std;
class Position;
class Chess {
private:
vector<Position> movementPosition;
public:
virtual void makeMove(int x, int y) = 0;
private:
int currentPos;
public:
void undo();
Position * redo();
void reset();
};
#endif
說明:
- 設計可以『悔棋』之類別。
- 需要陣列容器紀錄歷程。
// Chess.cpp
#include "Chess.h"
#include "Position.h"
void Chess::undo() {
if (!this->currentPos) return;
this->currentPos--;
}
Position * Chess::redo() {
if (this.movementHistory.empty()) return NULL;
if (this->currentPos == this.movementHistory.size() - 1) return NULL;
return ( this.movementHistory.at(++this->currentPos) );
}
void Chess::reset() {
this.movementHistory.clear();
this->currentPos = -1;
}