-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttt2.c
249 lines (229 loc) · 5.85 KB
/
ttt2.c
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
240
241
242
243
244
245
246
247
248
249
#include "ttt.h"
//helper functions declared here
int helperWinner(Board board, char charGiven);
int helperCompute(Board board);
int max(int a, int b);
int min(int a, int b);
// I didn't see the need to use ptr arithmetic
void init_boards()
{
for (int i = 0; i < HSIZE; i++)
{
htable[i].init = 0;
}
}
// easy boi
int depth(Board board)
{
int depth = 0;
for (int i = 0; i < 9; i++)
{
if (board[pos2idx[i]] == 'X' || board[pos2idx[i]] == 'O')
{
depth++;
}
}
return depth;
}
// I think it gets the int wins from ttt1
char winner(Board board)
{
if (helperWinner(board, 'X'))
{
return 'X';
}
else if (helperWinner(board, 'O'))
{
return 'O';
}
else if (depth(board) == 9)
{
return '-';
}
else
{
return '?';
}
}
// returns 1 if a winner is found else returns 0
int helperWinner(Board board, char charGiven)
{
int checker = 0;
for (int i = 0; i < 8; i++)
{
checker = 0;
for (int j = 0; j < 3; j++)
{
if (board[pos2idx[WINS[i][j]]] == charGiven)
{
checker++;
if (checker == 3)
{
return 1;
}
}
}
}
return 0;
}
char turn(Board board)
{
int boardhash = board_hash(board);
if (depth(board) == 9 || htable[boardhash].winner == 'X' || htable[boardhash].winner == 'O')
{
return '-';
}
else if (depth(board) % 2 == 0)
{
return 'X';
}
else
{
return 'O';
}
}
void init_board(Board board)
{
int hashValue = board_hash(board);
strcpy(htable[hashValue].board, board);
htable[hashValue].init = 1;
htable[hashValue].winner = winner(board);
htable[hashValue].turn = turn(board);
htable[hashValue].depth = depth(board);
}
// first off wth
void join_graph(Board board)
{
// this is the base case
if (depth(board) == 9)
{
return;
}
int hashValue = board_hash(board);
// check for values to set to -1
for (int i = 0; i < 9; i++)
{
if (board[pos2idx[i]] == 'O' || board[pos2idx[i]] == 'X')
{
htable[hashValue].move[i] = -1;
}
}
if (htable[hashValue].winner == 'X' || htable[hashValue].winner == 'O')
{
for (int i = 0; i < 9; i++)
{
htable[hashValue].move[i] = -1;
}
}
char turnChecker;
// for every node that is not -1 we call join graph with an x and an o
for (int i = 0; i < 9; i++)
{
char tempBoard[BSIZE];
strcpy(tempBoard, board);
if (htable[hashValue].move[i] != -1)
{
turnChecker = htable[hashValue].turn;
tempBoard[pos2idx[i]] = turnChecker;
htable[hashValue].move[i] = board_hash(tempBoard);
//printf(" %d\n %s\n",board_hash(tempBoard), tempBoard);
if (htable[board_hash(tempBoard)].init == 0)
{
init_board(tempBoard);
join_graph(tempBoard);
}
}
}
}
void compute_score()
{
for (int i = 9; i > -1; i--)
{
for (int j = 0; j < HSIZE; j++)
{
if (htable[j].init == 1 && htable[j].depth == i)
{
if (htable[j].winner == 'X')
{
htable[j].score = 1;
}
else if (htable[j].winner == 'O')
{
htable[j].score = -1;
}
else if (htable[j].winner == '-')
{
htable[j].score = 0;
}
else if (i != 9)
{
if (htable[j].turn == 'X')
{
int score = 0;
for (int k = 0; k < 9; k++)
{
if (htable[j].move[k] != -1)
{
if (htable[htable[j].move[k]].score > score)
{
score = htable[htable[j].move[k]].score;
}
}
}
htable[j].score = score;
}
else if (htable[j].turn == 'O')
{
int score = 100;
for (int k = 0; k < 9; k++)
{
if (htable[j].move[k] != -1)
{
if (htable[htable[j].move[k]].score < score)
{
score = htable[htable[j].move[k]].score;
}
}
}
htable[j].score = score;
}
}
}
}
}
}
int best_move(int board)
{
int positionHolder = 0;
if (htable[board].turn == 'X')
{
int score = -2;
for (int i = 0; i < 9; i++)
{
if (htable[board].move[i] != -1)
{
if (htable[htable[board].move[i]].score > score)
{
score = htable[htable[board].move[i]].score;
positionHolder = i;
}
}
}
}
else if (htable[board].turn == 'O')
{
int score = 2;
for (int i = 0; i < 9; i++)
{
if (htable[board].move[i] != -1)
{
if (htable[htable[board].move[i]].score < score)
{
score = htable[htable[board].move[i]].score;
positionHolder = i;
}
}
}
}
return positionHolder;
}