-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalBoard.cpp
85 lines (77 loc) · 1.91 KB
/
LocalBoard.cpp
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
78
79
80
81
82
83
84
85
#include "LocalBoard.h"
LocalBoard::LocalBoard()
{
field = new Cell*[3];
for (size_t i = 0; i < 3; i++) {
field[i] = new Cell[3];
for (size_t j = 0; j < 3; j++) {
field[i][j] = Cell::Empty;
}
}
winner = Cell::Empty;
}
LocalBoard::~LocalBoard()
{
for (size_t i = 0; i < 3; i++) {
delete[] field[i];
}
delete[] field;
}
void LocalBoard::set(const size_t y, const size_t x, const Cell& cell)
{
history.push(Coord(y, x));
field[y][x] = cell;
check();
}
void LocalBoard::revert()
{
Coord lastMove = history.top();
field[lastMove.y][lastMove.x] = Cell::Empty;
winner = Cell::Empty;
history.pop();
}
Cell LocalBoard::get(const size_t y, const size_t x) const
{
return field[y][x];
}
Cell LocalBoard::getWinner() const
{
return winner;
}
Coord LocalBoard::getLastMove() const
{
return history.top();
}
void LocalBoard::check()
{
// vertical and horizontal checks
for (size_t i = 0; i < 3; i++) {
if (field[0][i] != Cell::Empty && field[0][i] == field[1][i] && field[0][i] == field[2][i]) {
winner = field[0][i];
return;
}
if (field[i][0] != Cell::Empty && field[i][0] == field[i][1] && field[i][0] == field[i][2]) {
winner = field[i][0];
return;
}
}
// diagonal checks
if (field[1][1] != Cell::Empty) {
if (field[1][1] == field[0][0] && field[1][1] == field[2][2]) {
winner = field[1][1];
return;
}
if (field[1][1] == field[0][2] && field[1][1] == field[2][0]) {
winner = field[1][1];
return;
}
}
// draw case check
if (history.size() > 8) {
winner = Cell::Any;
}
// else winner is not specified yet
else {
winner = Cell::Empty;
}
}