-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField.cpp
217 lines (197 loc) · 5.77 KB
/
Field.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "Field.h"
Field::Field()
{
board = new GlobalBoard;
turn = Cell::Cross;
}
Field::~Field()
{
delete board;
}
void Field::move(const GlobalCoord &coord)
{
move(coord.by, coord.bx, coord.y, coord.x);
}
void Field::move(const Coord &global, Coord &local)
{
move(global.y, global.x, local.y, local.x);
}
void Field::move(size_t by, size_t bx, size_t y, size_t x)
{
board->set(by, bx, y, x, turn);
turn = nextCell(turn);
}
void Field::revert()
{
board->revert();
turn = nextCell(turn);
}
bool Field::isLegalMove(const GlobalCoord &coord) const
{
return isLegalMove(coord.by, coord.bx, coord.y, coord.x);
}
bool Field::isLegalMove(const Coord &global, const Coord &local) const
{
return isLegalMove(global.y, global.x, local.y, local.x);
}
bool Field::isLegalMove(size_t by, size_t bx, size_t y, size_t x) const
{
if (by > 2 || bx > 2 || y > 2 || x > 2) {
return false;
}
Coord next = board->getNextBoard();
if (next.y != -1 && (next.y != by || next.x != bx)) {
return false;
}
if (board->getWinner(by, bx) != Cell::Empty) {
return false;
}
if (board->get(by, bx, y, x) != Cell::Empty) {
return false;
}
return true;
}
Cell Field::get(const GlobalCoord &coord) const
{
return get(coord.by, coord.bx, coord.y, coord.x);
}
Cell Field::get(const Coord &global, const Coord &local) const
{
return get(global.y, global.x, local.y, local.x);
}
Cell Field::get(size_t by, size_t bx, size_t y, size_t x) const
{
return board->get(by, bx, y, x);
}
Cell Field::getTurn() const
{
return turn;
}
Cell Field::getWinner() const
{
return board->getWinner();
}
Cell Field::getWinner(const size_t by, const size_t bx) const
{
return board->getWinner(by, bx);
}
Coord Field::getNextBoard() const
{
return board->getNextBoard();
}
std::vector<std::vector<Coord>> Field::getEmptyCells() const
{
std::vector <std::vector <Coord>> moves(9, std::vector <Coord>());
for (size_t by = 0; by < 3; by++) {
for (size_t bx = 0; bx < 3; bx++) {
for (size_t y = 0; y < 3; y++) {
for (size_t x = 0; x < 3; x++) {
if (board->get(by, bx, y, x) == Cell::Empty) {
moves[by * 3 + bx].push_back(Coord(y, x));
}
}
}
}
}
return moves;
}
std::vector<GlobalCoord> Field::getValidMoves() const
{
std::vector <GlobalCoord> moves;
size_t by = board->getNextBoard().y;
size_t bx = board->getNextBoard().x;
if (by == -1) {
for (size_t by = 0; by < 3; by++) {
for (size_t bx = 0; bx < 3; bx++) {
if (getWinner(by, bx) != Cell::Empty) {
continue;
}
for (size_t y = 0; y < 3; y++) {
for (size_t x = 0; x < 3; x++) {
if (board->get(by, bx, y, x) == Cell::Empty) {
moves.push_back(GlobalCoord(by, bx, y, x));
}
}
}
}
}
}
else {
for (size_t y = 0; y < 3; y++) {
for (size_t x = 0; x < 3; x++) {
if (board->get(by, bx, y, x) == Cell::Empty) {
moves.push_back(GlobalCoord(by, bx, y, x));
}
}
}
}
return moves;
}
std::ostream &operator<<(std::ostream &out, Field &field)
{
Coord nextBoard = field.getNextBoard();
out << "\n 1 2 3 bx\n 1 2 3 1 2 3 1 2 3 x\n +-----+-----+-----+\n";
for (size_t by = 0; by < 3; by++) {
for (size_t y = 0; y < 3; y++) {
if (y == 1) {
out << (by + 1) << " ";
}
else {
out << " ";
}
out << (y + 1) << " |";
for (size_t bx = 0; bx < 3; bx++) {
switch (field.getWinner(by, bx)) {
case Cell::Cross:
out << "x x x|";
break;
case Cell::Null:
out << "o o o|";
break;
case Cell::Any:
out << "/ / /|";
break;
default:
char cell = ' ';
if (nextBoard.y == -1 || nextBoard == Coord(by, bx)) {
cell = '.';
}
for (size_t x = 0; x < 3; x++) {
if (field.get(by, bx, y, x) == Cell::Empty) {
out << cell;
}
else {
switch (field.get(by, bx, y, x)) {
case Cell::Cross:
out << "X";
break;
case Cell::Null:
out << "O";
break;
default:
throw GameException();
break;
}
}
if (x < 2) {
out << " ";
}
else {
out << "|";
}
}
}
}
out << "\n";
}
if (by == 2) {
out << "byy ";
}
else {
out << " ";
}
out << "+-----+-----+-----+\n";
}
out << "\n";
return out;
}