-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
92 lines (83 loc) · 2.5 KB
/
Game.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
#include "Game.h"
bool Game::is_correct_move(size_t x, size_t y, bool player) const {
return (player == is_mark_move) && field[y][x] == EMPTY;
}
Game::status Game::make_move(size_t x, size_t y, bool player) {
if (is_correct_move(x, y, player)) {
history.push_back(field);
field[y][x] = is_mark_move ? MARK : ZERO;
Game::status state = get_status();
is_mark_move = !is_mark_move;
return state;
}
return Game::IS_GOING;
}
std::string Game::to_string() const {
std::string res;
res += std::string(WIDTH * 2 + 1, '-');
res += '\n';
for (auto it: field) {
res += '|';
for (auto jit : it) {
switch (jit) {
case EMPTY:
res += ' ';
break;
case MARK:
res += 'x';
break;
case ZERO:
res += 'o';
break;
}
res += '|';
}
res += '\n';
res += std::string(WIDTH * 2 + 1, '-');
res += '\n';
}
return res;
}
Game::status Game::get_status() const {
int counter = 0;
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
if ((field[i][j] == MARK && is_mark_move) || (field[i][j] == ZERO && !is_mark_move))
++counter;
else
counter = 0;
}
if (counter == 3)
return is_mark_move ? MARK_WON : ZERO_WON;
}
counter = 0;
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
if ((field[j][i] == MARK && is_mark_move) || (field[j][i] == ZERO && !is_mark_move))
++counter;
else
counter = 0;
}
if (counter == 3)
return is_mark_move ? MARK_WON : ZERO_WON;
}
counter = 0;
for (int i = 0; i < WIDTH; ++i) {
if ((field[i][i] == MARK && is_mark_move) || (field[i][i] == ZERO && !is_mark_move))
++counter;
}
if (counter == 3) return is_mark_move ? MARK_WON : ZERO_WON;
counter = 0;
for (int i = 0; i < WIDTH; ++i) {
if ((field[i][WIDTH - 1 - i] == MARK && is_mark_move) || (field[i][WIDTH - 1 - i] == ZERO && !is_mark_move))
++counter;
}
if (counter == 3) return is_mark_move ? MARK_WON : ZERO_WON;
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
if (field[i][j] == EMPTY)
return IS_GOING;
}
}
return DRAW;
}