-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomputer.c
586 lines (527 loc) · 15.4 KB
/
computer.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#include "common.h"
#include "core.h"
#include "computer.h"
#include "helpers.h"
#include "io.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <threads.h>
#include <time.h>
/* Macro: sets var to value if table[row][col] should be filled
*
* @param row cell row
* @param col cell row
* @param value value to use
* @param var variable to be set
* */
#define FILL_EMPTY(row, col, value, var) \
if (table[row][col] == PLACEHOLDER){\
if (var != -1){\
var = -1; break;\
}\
var = value; continue;\
}\
if (table[row][col] != player){\
var = -1; break;\
}
PositionAlignments *create(int pos, int aligns){
PositionAlignments *result = malloc(sizeof(PositionAlignments));
*result = (PositionAlignments){.pos = pos, .aligns = aligns, .next = NULL};
return result;
}
PositionAlignments *append(PositionAlignments *head, int pos, int aligns){
if (head == NULL){
return NULL;
}
if (head->next == NULL){
head->next = create(pos, aligns);
return head->next;
}
return append(head->next, aligns, pos);
}
int completeRow(char table[3][3], char player){
for (int i = 0; i < 3; i++){
int empty = -1;
for (int j = 0; j < 3; j++){
FILL_EMPTY(i, j, j, empty);
}
if (empty != -1){
return position(i, empty);
}
}
return -1;
}
int completeCol(char table[3][3], char player){
for (int j = 0; j < 3; j++){
int empty = -1;
for (int i = 0; i < 3; i++){
FILL_EMPTY(i, j, i, empty);
}
if (empty != -1){
return position(empty, j);
}
}
return -1;
}
int completeDiag(char table[3][3], char player){
// diagonal 7-5-3
int empty = -1;
for (int i = 0; i < 3; i++){
FILL_EMPTY(i, i, i, empty);
}
if (empty != -1){
return position(empty, empty);
}
// diagonal 1-5-9
empty = -1;
for (int i = 0; i < 3; i++){
FILL_EMPTY(i, 2 - i, i, empty)
}
if (empty != -1){
return position(empty, 2 - empty);
}
return -1;
}
bool containsPos(PositionAlignments *haystack, int needle){
for (PositionAlignments *x = haystack; x != NULL; x = x->next){
if (x->pos == needle){
return true;
}
}
return false;
}
void printList(PositionAlignments *head){
for (PositionAlignments *x = head; x != NULL; x = x->next){
printf("(%d, %d) ", x->pos, x->aligns);
}
printf("\n");
}
void freeList(PositionAlignments *head){
if (head != NULL){
freeList(head->next);
free(head);
}
}
PositionAlignments *merge(size_t num, ...){
va_list lists;
va_start(lists, num);
// -1 is not a valid position and all positions > -1
PositionAlignments *result = create(-1, 0);
for (size_t i = 0; i < num; i++){
PositionAlignments *head = va_arg(lists, PositionAlignments *);
for (PositionAlignments *p1= head; p1!= NULL; p1= p1->next){
PositionAlignments *p2= result;
for (;p2->next != NULL && p1->pos >= p2->next->pos; p2= p2->next){}
if (p2->pos == p1->pos){
continue;
}
PositionAlignments *p3 = p2->next;
p2->next = create(p1->pos, p1->aligns); p2 = p2->next; p2->next = p3;
}
}
va_end(lists);
PositionAlignments *p = result->next; free(result);
return p;
}
int complete(char table[3][3], char player){
int positions[] = {
completeRow(table, player), completeCol(table, player), completeDiag(table, player)
};
for (int i = 0; i < 3; i++){
if (positions[i] != -1){
return positions[i];
}
}
return -1;
}
bool firstMove(char table[3][3]){
// search starts from both table ends
int xCount = 0;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (table[i][j] == 'o'){
return false;
}
if (table[i][j] == 'x'){
xCount++;
if (xCount > 1){
return false;
}
}
}
}
return true;
}
void cpy(char table[3][3], char copy[3][3]){
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
copy[i][j] = table[i][j];
}
}
}
int diagonal(int position){
if (contains(3, (int[]){7, 5, 3}, position)){
return 1;
}
return contains(3, (int[]){1, 5, 9}, position) ? -1 : 0;
}
bool colFilled(char table[3][3], int col, char player){
for (int i = 0; i < 3; i++){
if (table[i][col] != player){
return false;
}
}
return true;
}
bool rowFilled(char table[3][3], int row, char player){
for (int j = 0; j < 3; j++){
if (table[row][j] != player){
return false;
}
}
return true;
}
bool diagFilled(char table[3][3], int diagonal, char player){
if (diagonal == 0){
return false;
}
for (int i = 0; i < 3; i++){
if (table[i][diagonal == 1 ? i : 2 - i] != player){
return false;
}
}
return true;
}
bool hasFilled(char table[3][3], int position, char player){
return
colFilled(table, col(position), player) ||
rowFilled(table, row(position), player) ||
diagFilled(table, diagonal(position), player) ||
// check the diagonal 1-5-9
(position == 5 ? diagFilled(table, -1, player) : 0);
}
char other(char player){
return player == 'x' ? 'o' : (player == 'o' ? 'x' : ' ');
}
bool hasRowAlignment(char table[3][3], int pos, char player){
bool has = false;
int c = col(pos); int r = row(pos);
for (int j = 0; j < 3; j++){
if (j != c){
if (table[r][j] == player){
has = true; continue;
}
if (table[r][j] == other(player)){
return false;
}
}
}
return has;
}
bool hasColAlignment(char table[3][3], int pos, char player){
bool has = false;
int c = col(pos); int r = row(pos);
for (int i = 0; i < 3; i++){
if (i != r){
if (table[i][c] == player){
has = true; continue;
}
if (table[i][c] == other(player)){
return false;
}
}
}
return has;
}
int diagAlignments(char table[3][3], int pos, char player){
bool hasMainAlignment = false;
bool hasSecondAlignment = false;
int d = diagonal(pos);
if (d == 1){
for (int i = 0; i < 3; i++){
if (position(i, i) != pos){
if (table[i][i] == player){
hasMainAlignment = true; continue;
}
if (table[i][i] == other(player)){
hasMainAlignment = false; break;
}
}
}
}
d = pos == 5 ? -1 : d;
if (d == -1){
for (int i = 0; i < 3; i++){
if (position(i, 2 - i) != pos){
if (table[i][2 - i] == player){
hasSecondAlignment = true; continue;
}
if (table[i][2 - i] == other(player)){
hasSecondAlignment = false; break;
}
}
}
}
return (hasMainAlignment ? 1 : 0) + (hasSecondAlignment ? 1 : 0);
}
int alignments(char table[3][3], int position, char player){
return
(hasRowAlignment(table, position, player) ? 1 : 0) +
(hasColAlignment(table, position, player) ? 1 : 0) +
diagAlignments(table, position, player);
}
size_t len(PositionAlignments *head){
size_t len = 0;
for (PositionAlignments *x = head; x != NULL; x = x->next, len++);
return len;
}
PositionAlignments *max(PositionAlignments *head){
PositionAlignments *m = head;
for (PositionAlignments *x = head; x != NULL; x = x->next){
if (m->aligns < x->aligns){
m = x;
}
}
return m;
}
PositionAlignments *allAlignments(char table[3][3], char player){
PositionAlignments *x, *head = NULL;
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (table[i][j] == PLACEHOLDER){
int p = position(i, j);
if (head == NULL){
head = create(p, alignments(table, p, player)); x = head; continue;
}
x = append(x, p, alignments(table, p, player));
}
}
}
return head;
}
PositionAlignments *getDoubleAlignment(PositionAlignments *head){
for (PositionAlignments *x = head; x != NULL; x = x->next){
if (x->aligns == 2){
return x;
}
}
return NULL;
}
void getListPositions(PositionAlignments *head, int arr[]){
int i = 0;
for (PositionAlignments *x = head; x != NULL; x = x->next, i++){
arr[i] = x->pos;
}
}
PositionAlignments *maxoccurs(PositionAlignments *head){
if (head == NULL){
return NULL;
}
int maxAligns = max(head)->aligns;
PositionAlignments *result = NULL;
for (PositionAlignments *x = head; x != NULL; x = x->next){
PositionAlignments *last;
if (x->aligns == maxAligns){
if (result == NULL){
result = create(x->pos, x->aligns); last = result; continue;
}
last = append(last, x->pos, x->aligns);
}
}
return result;
}
bool rowBlock(char table[3][3], int pos, char player){
int c = col(pos); int r = row(pos);
for (int j = 0; j < 3; j++){
if (j != c){
if (table[r][j] != other(player)){
return false;
}
}
}
return true;
}
bool colBlock(char table[3][3], int pos, char player){
int c = col(pos); int r = row(pos);
for (int i = 0; i < 3; i++){
if (i != r){
if (table[i][c] != other(player)){
return false;
}
}
}
return true;
}
bool diagBlock(char table[3][3], int pos, char player){
int d = diagonal(pos); bool mainDiagBlock = d == 1;
if (d == 1){
for (int i = 0; i < 3; i++){
if (position(i, i) != pos){
if (table[i][i] != other(player)){
mainDiagBlock = false; break;
}
}
}
if (mainDiagBlock){
return true;
}
}
d = pos == 5 ? -1 : d;
bool secondDiagBlock = d == -1;
if (d == -1){
for (int i = 0; i < 3; i++){
if (position(i, 2 - i) != pos){
if (table[i][2 - i] != other(player)){
secondDiagBlock = false; break;
}
}
}
}
return mainDiagBlock || secondDiagBlock;
}
bool block(char table[3][3], int pos, char player){
return
rowBlock(table, pos, player) ||
colBlock(table, pos, player) ||
diagBlock(table, pos, player);
}
int arrmaxind(int arr[], size_t len){
int maxind = 0;
for (int i = 0; i < len; i++){
if (arr[maxind] < arr[i]){
maxind = i;
}
}
return maxind;
}
int arrminind(int arr[], size_t len){
int minind = 0;
for (int i = 0; i < len; i++){
if (arr[minind] > arr[i]){
minind = i;
}
}
return minind;
}
int next(char table[3][3], char player){
// at start
if (firstMove(table)){
if (player == 'o' && table[1][1] == PLACEHOLDER){
return 5;
}
int corners[] = {1, 3, 7, 9};
srand(time(NULL));
return corners[rand() % 4];
}
// finish
int finish = complete(table, player);
if (finish != -1){
return finish;
}
// defense
finish = complete(table, other(player));
if (finish != -1){
return finish;
}
PositionAlignments *alignsListHead = allAlignments(table, player);
if (alignsListHead == NULL){
return -1;
}
// move has double alignments, i.e certain win
PositionAlignments *doubleAlign = getDoubleAlignment(alignsListHead);
if (doubleAlign != NULL){
int pos = doubleAlign->pos; freeList(alignsListHead);
return pos;
}
// only consider options with max alignments
PositionAlignments *tmp = maxoccurs(alignsListHead);
freeList(alignsListHead); alignsListHead = tmp;
// only one move
if (len(alignsListHead) == 1){
int pos = alignsListHead->aligns; freeList(alignsListHead);
return pos;
}
// all positions lead to a tie
if (alignsListHead->aligns == 0){
int pos = alignsListHead->pos; freeList(alignsListHead);
return pos;
}
int l = len(alignsListHead);
int positions[l];
getListPositions(alignsListHead, positions);
freeList(alignsListHead);
int positionMovesToWin[l];
int freeMovesAfterPosition[l];
for (int i = 0; i < 3; i++){
char decoy[3][3]; cpy(table, decoy); setsqr(decoy, positions[i], player);
char virtual = other(player); int movesToWin = 0, freeMoves = 0;
while (true){
int nextPosition = next(decoy, virtual); setsqr(decoy, nextPosition, virtual);
if (virtual == player){
movesToWin++;
if (hasFilled(decoy, nextPosition, player)){
positionMovesToWin[i] = movesToWin; freeMovesAfterPosition[i] = freeMoves;
break;
}
if (block(decoy, nextPosition, player)){
freeMovesAfterPosition[i] = freeMoves;
// there can only be 9 moves total
positionMovesToWin[i] = 10; break;
}
freeMoves++;
}
if (hasFilled(table, nextPosition, other(player))){
positionMovesToWin[i] = 10; freeMovesAfterPosition[i] = -1;
break;
}
if (tie(decoy)){
freeMovesAfterPosition[i] = freeMoves; positionMovesToWin[i] = movesToWin;
break;
}
virtual = other(virtual);
}
}
// get most optimal position index by most free moves
int optimalIndexByFreeMoves = arrmaxind(freeMovesAfterPosition, l);
if (positionMovesToWin[optimalIndexByFreeMoves] != -1){
return positions[optimalIndexByFreeMoves];
}
int optimalIndexByWins = arrminind(positionMovesToWin, l);
return positions[optimalIndexByWins];
}
void startPvE(char table[3][3]){
init(table);
char player;
do {
printf(BOLD"choose player (%sx%s/%so"EXIT"): ", LIGHT_RED, EXIT, BOLD""GREEN);
player = getchar();
// break line
getchar();
} while (player != 'x' && player != 'o');
char env = other(player);
char current = 'x';
while (true){
clrscr(); print(table);
if (current == env){
setsqr(table, next(table, env), env);
} else {
printf("%s%c"EXIT" chooses: ", player == 'x' ? LIGHT_RED : GREEN, player);
int position = readint("");
if (!valid(table, position)){
continue;
}
setsqr(table, position, player);
}
if (wins(table, current)){
printf(BOLD"%s"EXIT""BOLD" wins.\n"EXIT, current == env ? LIGHT_RED"computer" : GREEN"player");
break;
}
if (tie(table)){
clrscr(); printf(BOLD""GRAY"tie.\n"EXIT);
break;
}
current = other(current);
}
}