-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.cpp
239 lines (219 loc) · 6.2 KB
/
tictactoe.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <iostream>
#include <string.h>
#include <cassert>
#include <ctime>
#include <cstdlib>
#define MAXSCORE (2 << 16)
#define INF (2 << 17)
#define SQUARE(x) (x) * (x)
#define DEBUG
#ifdef DEBUG
#define DPRINT(...) fprintf(stderr, __VA_ARGS__);
#define dprint() print()
#else /* DEBUG */
#define DPRINT(...)
#endif /* DEBUG */
using namespace std;
struct state{
char grid[9];
int score;
int next_state_cnt;
state *next_states[9];
state(){
next_state_cnt = 0;
fill(next_states, next_states + 9, nullptr);
memset(grid,'-',9);
score = 0;
}
state(const state& rhs){
next_state_cnt = 0;
fill(next_states, next_states + 9, nullptr);
memcpy(grid, rhs.grid, 9);
score = 0;
}
~state(){
for(auto s : next_states){
delete s;
}
}
void operator=(const state& rhs){
memcpy(grid, rhs.grid, 9);
}
bool operator==(const state& rhs) const{
for(int i = 0; i < 9; ++i){
if(grid[i] != rhs.grid[i])
return false;
}
return true;
}
char who_won(){
int sum;
// check rows
for(int i = 0; i <= 6; i+=3){
sum = 0;
for(int j = i; j < i+3; ++j){
sum += grid[j];
}
if(sum == 3*'O') return 1;
else if(sum == 3*'X') return -1;
}
// check columns
for(int i = 0; i < 3; i++){
sum = 0;
for(int j = i; j <= i+6; j += 3){
sum += grid[j];
}
if(sum == 3*'O') return 1;
else if(sum == 3*'X') return -1;
}
// check diagonals
for(int i = 0; i < 3; i+=2){
sum = 0;
for(int j = i; j <= i+(4-i)*2; j += 4-i){
sum += grid[j];
}
if(sum == 3*'O') return 1;
else if(sum == 3*'X') return -1;
}
return 0;
}
bool is_full(){
for(char x : grid){
if(x == '-')
return false;
}
return true;
}
void print(){
for(int i = 0; i < 9; ++i){
cout << grid[i] << ' ';
if(i % 3 == 2) cout << '\n';
}
cout << '\n';
}
#ifndef DEBUG
void dprint(){
}
#endif // DEBUG
};
void generate_tree(state* current, bool turn){
int res;
assert(current);
if(current->is_full()){
current->score = current->who_won() * MAXSCORE;
return;
}
if(res = current->who_won()){
current->score = res * MAXSCORE;
return;
}
for(int i = 0; i < 9; ++i){
if(current->grid[i] == '-'){
state* next_state = new state(*current);
next_state->grid[i] = (turn ? 'X' : 'O');
generate_tree(next_state, !turn);
current->next_states[i] = next_state;
current->next_state_cnt++;
}
}
int sum = 0, win_ratio = 1;
for(int i = 0; i < 9; ++i){
if(current->next_states[i] != nullptr){
if(current->next_states[i]->who_won()){
current->score = current->next_states[i]->score;
return;
}
else
current->score += current->next_states[i]->score;
}
}
current->score /= current->next_state_cnt;
}
void player_move(state** current){
int x;
cout << "Your move "; cin >> x;
while(0 <= x and x < 9 and (*current)->next_states[x] == nullptr){
cout << "Invalid move. Try again "; cin >> x;
}
*current = (*current)->next_states[x];
}
void pc_move(state** current, bool player_first){
int pc_score = (player_first ? INF : -INF);
int pc_pos = 0;
for(int i = 0; i < 9; ++i){
if((*current)->next_states[i] == nullptr) continue;
DPRINT("%d\n",(*current)->next_states[i]->score);
(*current)->next_states[i]->dprint();
if(player_first){
if((*current)->next_states[i]->score < pc_score){
pc_score = (*current)->next_states[i]->score;
pc_pos = i;
}
}
else {
if((*current)->next_states[i]->score > pc_score){
pc_score = (*current)->next_states[i]->score;
pc_pos = i;
}
}
}
cout << "PC move " << pc_pos << endl;
*current = (*current)->next_states[pc_pos];
}
int main(){
state* init = new state;
srand(time(NULL));
generate_tree(init,0);
cout << "[Move guide table]\n";
for(int i = 0; i < 9; ++i){
cout << i << ' ';
if(i % 3 == 2) cout << '\n';
}
cout.put('\n');
state* current = init;
bool player_first = rand()%2;
current->print();
if(player_first){
//player first
cout << "You are going first!\n";
while(!current->is_full() and !current->who_won()){
player_move(¤t);
current->print();
if(!current->is_full() and !current->who_won()){
pc_move(¤t, player_first);
current->print();
}
}
}
else {
// PC first
cout << "PC is going first!\n";
while(!current->is_full() and !current->who_won()){
pc_move(¤t, player_first);
current->print();
if(!current->is_full() and !current->who_won()){
player_move(¤t);
current->print();
}
}
}
DPRINT("%d\n", current->score);
current->dprint();
switch (current->who_won()){
case 1 :
printf("%s won!\n", player_first ? "Player" : "PC");
break;
case -1 :
printf("%s won!\n", player_first ? "PC" : "Player");
break;
default :
cout << "Tie!\n" << endl;
break;
}
delete init;
cout << "Press any key to continue... ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
return 0;
}