forked from tewalds/morat
/
history.h
77 lines (56 loc) · 1.16 KB
/
history.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#include <vector>
#include "../lib/string.h"
#include "board.h"
#include "move.h"
namespace Morat {
namespace BridgIt {
class History {
std::vector<Move> hist;
Board board;
public:
History() { }
History(const Board & b) : board(b) { }
const Move & operator [] (int i) const {
return hist[i];
}
Move last() const {
if(hist.size() == 0)
return M_NONE;
return hist.back();
}
const Board & operator * () const { return board; }
const Board * operator -> () const { return & board; }
std::vector<Move>::const_iterator begin() const { return hist.begin(); }
std::vector<Move>::const_iterator end() const { return hist.end(); }
const Board get_board() const {
Board b(board.get_size());
for(auto m : hist)
b.move(m);
return b;
}
int len() const {
return hist.size();
}
void clear() {
hist.clear();
board = get_board();
}
bool undo() {
if(hist.size() <= 0)
return false;
hist.pop_back();
board = get_board();
return true;
}
bool move(const Move & m) {
if(board.valid_move(m)){
board.move(m);
hist.push_back(m);
return true;
}
return false;
}
};
}; // namespace BridgIt
}; // namespace Morat