-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuttt.cpp
148 lines (134 loc) · 4.28 KB
/
uttt.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
// uttt.cpp
// Jeffrey Drost
#include "uttt.h"
std::ostream &operator<<(std::ostream& os, const Player &p) {
if (p == Player::None) {
os << ".";
} else if (p == Player::X) {
os << "X";
} else if (p == Player::O) {
os << "O";
} else if (p == Player::Active) {
os << "*";
}
return os;
}
std::ostream &operator<<(std::ostream& os, const State &s) {
for (int r=0; r<9; r++) {
for (int c=0; c<9; c++) {
os << s.board[r][c];
}
os << std::endl;
}
os << std::endl;
for (int r=0; r<3; r++) {
for (int c=0; c<3; c++) {
os << s.macroboard[r][c];
}
os << std::endl;
}
return os;
}
std::ostream &operator<<(std::ostream& os, const Move &m) {
os << m.x << " " << m.y;
return os;
}
Player getCurrentPlayer(const State &state)
{
int countX = 0;
int countO = 0;
for (int r=0; r<9; r++) {
for (int c=0; c<9; c++) {
if (state.board[r][c] == Player::X) countX++;
else if (state.board[r][c] == Player::O) countO++;
}
}
return (countX > countO ? Player::O : Player::X);
}
Player getWinner(const State &state, int row, int col)
{
for (int r=0; r<3; r++)
if (state.board[row*3+r][col*3] == state.board[row*3+r][col*3+1] &&
state.board[row*3+r][col*3+1] == state.board[row*3+r][col*3+2] &&
state.board[row*3+r][col*3+2] != Player::None)
return state.board[row*3+r][col*3];
for (int c=0; c<3; c++)
if (state.board[row*3][col*3+c] == state.board[row*3+1][col*3+c] &&
state.board[row*3+1][col*3+c] == state.board[row*3+2][col*3+c] &&
state.board[row*3][col*3+c] != Player::None)
return state.board[row*3][col*3+c];
if (state.board[row*3][col*3] == state.board[row*3+1][col*3+1] &&
state.board[row*3+1][col*3+1] == state.board[row*3+2][col*3+2] &&
state.board[row*3][col*3] != Player::None)
return state.board[row*3][col*3];
if (state.board[row*3][col*3+2] == state.board[row*3+1][col*3+1] &&
state.board[row*3+1][col*3+1] == state.board[row*3+2][col*3] &&
state.board[row*3][col*3+2] != Player::None)
return state.board[row*3][col*3+2];
for (int r=0; r<3; r++)
for (int c=0; c<3; c++)
if (state.board[row*3+r][col*3+c] == Player::None)
return Player::Active;
return Player::None;
}
State doMove(const State &state, const Move &m)
{
State result = state;
if (state.macroboard[m.y/3][m.x/3] != Player::Active) {
return result; // Invalid move
}
result.board[m.y][m.x] = getCurrentPlayer(state);
for (int r=0; r<3; r++) {
for (int c=0; c<3; c++) {
result.macroboard[r][c] = getWinner(result, r, c);
}
}
int empty = 0;
for (int r=0; r<9; r++)
for (int c=0; c<9; c++)
if (result.board[r][c] == Player::None)
empty++;
bool stillPlaying = empty > 0 && getWinner(result) == Player::None;
if (result.macroboard[m.y%3][m.x%3] == Player::Active)
for (int r=0; r<3; r++)
for (int c=0; c<3; c++)
if ((r!=m.y%3 || c!=m.x%3 || !stillPlaying) && result.macroboard[r][c] == Player::Active)
result.macroboard[r][c] = Player::None;
return result;
}
Player getWinner(const State &state)
{
for (int r=0; r<3; r++)
if (state.macroboard[r][0] == state.macroboard[r][1] &&
state.macroboard[r][1] == state.macroboard[r][2] &&
state.macroboard[r][2] != Player::None &&
state.macroboard[r][2] != Player::Active)
return state.macroboard[r][2];
for (int c=0; c<3; c++)
if (state.macroboard[0][c] == state.macroboard[1][c] &&
state.macroboard[1][c] == state.macroboard[2][c] &&
state.macroboard[2][c] != Player::None &&
state.macroboard[2][c] != Player::Active)
return state.macroboard[2][c];
if (state.macroboard[0][0] == state.macroboard[1][1] && state.macroboard[1][1] == state.macroboard[2][2] &&
state.macroboard[0][0] != Player::None && state.macroboard[0][0] != Player::Active)
return state.macroboard[0][0];
if (state.macroboard[0][2] == state.macroboard[1][1] && state.macroboard[1][1] == state.macroboard[2][0] &&
state.macroboard[0][2] != Player::None && state.macroboard[0][2] != Player::Active)
return state.macroboard[0][2];
return Player::None;
}
std::vector<Move> getMoves(const State &state)
{
std::vector<Move> moves;
if (getWinner(state) == Player::None) {
for (int r=0; r<9; r++) {
for (int c=0; c<9; c++) {
if (state.macroboard[r/3][c/3] == Player::Active && state.board[r][c] == Player::None) {
moves.push_back(Move{c,r});
}
}
}
}
return moves;
}