-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
534 lines (302 loc) · 9.59 KB
/
main.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
#include "l_header.h"
#include "f_header.h"
int main(int argc, char **argv) {
int size = 11, difficulty = 1, user = 0, swap = 0, nextPlayer, canSwap, winCond, contMove, conti, contj, i, j; /* 0 is white */
/*
Command Line Parameter Choices -- Executes if we have parameters (argc > 1)
*/
if(argc > 1) {
char *n,*d,*b,*s;
/*
argv choices:
-n size
-d difficulty
-b user is black player
-s swap rule
*/
for (i = 1 ; i < argc ; ) {
if(!strcmp("-n",argv[i])) { /* if we have -n */
size = atoi(argv[i+1]);
i+=2;
} else if(!strcmp("-d",argv[i])) { /* if we have -d */
difficulty = atoi(argv[i+1]);
i+=2;
} else if(!strcmp("-b",argv[i])) { /* if we have -b */
user = 1;
i++;
} else if(!strcmp("-s",argv[i])) { /* if we have -s */
swap = 1;
i++;
}
}
}
/*
In-Game Commands
*/
char cmd[9999], temp[999999], **board;
FILE *filePath;
do {
fflush(stdout); //This is not essential, but is added for the Hex Cup.
/* Read the Command */
printf("Hex Game> ");
gets(cmd);
/* Decide what the Command is and act accordingly
-- every single one of these outside ifs
correspond to a different in-game command */
subStr(cmd, temp, 1);
if(!strcmp(temp, "newgame")) {
winCond = 0, canSwap = 2;
if(partCount(cmd) >= 2) { /* Here User is selected */
char tempUsr[30];
subStr(cmd, tempUsr, 2);
if(!strcmp(tempUsr, "black"))
user = 1;
if(partCount(cmd) >= 3) { /* Here swap option is defined */
char tempSwp[30];
subStr(cmd, tempSwp, 3);
if(!strcmp(tempSwp, "swapon"))
swap = 1;
if(partCount(cmd) >= 4) { /* Here size option is defined */
char tempSz[30];
subStr(cmd, tempSz, 4);
size = atoi(tempSz);
}
}
}
/* Board Making */
board = malloc(size * sizeof(char *));
for(i = 0 ; i < size ; i++)
*(board + i) = malloc(size * sizeof(char));
for(i = 0 ; i < size ; i++)
for(j = 0 ; j < size ; j++)
board[i][j] = 'n';
boardDisp(board, size);
nextPlayer = 0;
next(nextPlayer, user);
filePath = fopen("moves.hex", "w+");
} else if(!strcmp(temp, "play")) {
if(!winCond && (nextPlayer == user)) { // (!winCond) for local 1v1
char move[10];
canSwap--;
subStr(cmd, move, 2);
/* Translate move to numbers */
int y = *move - 64, x = *(move + 1) - 48;
if(strcmp(move + 2, "\0")) {
x *= 10;
x += *(move + 2) - 48;
}
/* Make the move -- if possible */
if(board[x-1][y-1] != 'n') {
boardDisp(board, size);
printf("Move can't be played! Position is occupied\n");
} else {
if(!nextPlayer)
board[x-1][y-1] = 'w';
else
board[x-1][y-1] = 'b';
/* Log the move */
fputs(move, filePath);
fflush(filePath);
boardDisp(board, size);
printf("Move played: %s\n", move);
winCond = win(board, size, nextPlayer);
nextPlayer = !nextPlayer;
}
if(!winCond)
next(nextPlayer, user);
} else if(winCond) {
printf("Game has ended!\n");
} else if(!(nextPlayer == user)) {
printf("You must wait for your turn to play.\n");
}
} else if(!strcmp(temp, "cont")) {
if(nextPlayer != user) {
canSwap--;
contMove = -99999, conti = size / 2, contj = size / 2;
int temp = -99999;
/* Find the best evaluated move from all the possible ones */
for(i = 0 ; i < size ; i++) {
for (j = 0 ; j < size ; j++) {
if(board[i][j] == 'n') {
if(!user)
board[i][j] = 'w';
else
board[i][j] = 'b';
temp = minimax(board, size, i, j, difficulty-1, 1, user);
board[i][j] = 'n';
contMove = max(contMove, temp);
if(contMove != temp) {
conti = i;
contj = j;
}
}
}
}
/* Make the best evaluated move */
if(!user)
board[conti][contj] = 'b';
else
board[conti][contj] = 'w';
boardDisp(board, size);
printf("Move played: %c%d\n", contj+65, conti+1);
/* Log the move */
int tempi, tempCond = 0; //Condition for the case where j is double digit so we have to write it digit by digit
conti++;
if(conti > 9) {
tempCond = 1;
tempi = (conti) % 10;
conti /= 10;
}
fputc(contj + 65, filePath);
fputc(conti + 48, filePath);
if(tempCond)
fputc(tempi + 48, filePath);
fflush(filePath);
winCond = win(board, size, nextPlayer);
nextPlayer = !nextPlayer;
if(!winCond)
next(nextPlayer, user);
} else {
printf("Cannnot use cont right now, it is user's turn.\n");
}
} else if(!strcmp(temp, "undo")) {
/* To make undos possible, we log each move played to a temporary moves.hex file */
if(!winCond) {
int extraCh = 0;
nextPlayer = !nextPlayer;
/* Find numbers of characters of last move */
/* We have to remember that fgetc() gets our
pointer 1 position to the right so we must
always fix the position after using it */
fseek(filePath, -2 , SEEK_CUR); //-2 pointerseats
if (fgetc(filePath) < 58) { /* this if means we have 3 characters since this char is a number(<58) */
//+1 pointerseats
fseek(filePath, -1, SEEK_CUR); //-1 pointerseats
extraCh = 1;
}
fseek(filePath, -1 , SEEK_CUR);//-1 pointerseats -- -2 total for 2 chars / -3 for 3 chars (ready for x)
/* Identify and undo last move */
int y = fgetc(filePath) - 64;
int x = fgetc(filePath) - 48;
if(extraCh) {
x *= 10;
x += fgetc(filePath) - 48;
}
board[x-1][y-1] = 'n';
fseek(filePath, -(2 + extraCh) , SEEK_CUR); /* File Pointer will stay in a place where undos are
to its right, so as future undos will not be impacted */
boardDisp(board, size);
next(nextPlayer, user);
} else {
printf("Game not in progress!\n");
}
} else if(!strcmp(temp, "suggest")) {
contMove = -99999;
int temp = -99999;
/* Find the best evaluated move from all the possible ones */
for(i = 0 ; i < size ; i++) {
for (j = 0 ; j < size ; j++) {
if(board[i][j] == 'n') {
if(!user)
board[i][j] = 'b';
else
board[i][j] = 'w';
temp = minimax(board, size, i, j, difficulty-1, 0, user);
board[i][j] = 'n';
contMove = min(contMove, temp);
if(contMove != temp) {
conti = i;
contj = j;
}
}
}
}
printf("Suggested move: %c%d\n", contj+65, conti+1);
} else if(!strcmp(temp, "level")) {
if(partCount(cmd) == 2) {
char tempLvl[5];
subStr(cmd, tempLvl, 2);
difficulty = atoi(tempLvl);
} else {
printf("Current game level is: %d\n", difficulty);
}
} else if(!strcmp(temp, "swap")) {
if(canSwap == 1) {
if(nextPlayer == user) {
//user = !user; This can be uncommented to make local 1v1 possible with some other adjustments
/* Following part is sane thing from the undo command */
int extraCh = 0;
fseek(filePath, -2 , SEEK_CUR);
if (fgetc(filePath
) < 58) {
fseek(filePath, -1, SEEK_CUR);
extraCh = 1;
}
fseek(filePath, -1 , SEEK_CUR);
int y = fgetc(filePath) - 64;
int x = fgetc(filePath) - 48;
if(extraCh) {
x *= 10;
x += fgetc(filePath) - 48;
}
board[x-1][y-1] = 'n';
board[y-1][x-1] = 'b';
nextPlayer = !nextPlayer;
boardDisp(board, size);
printf("Move played: swap\n");
next(nextPlayer, user);
} else {
printf("You must wait for your turn to swap\n");
}
} else {
printf("Only black player can swap on his first move.\n");
}
} else if(!strcmp(temp, "save")) {
char fileName[100];
subStr(cmd, fileName, 2);
FILE *fp;
fp = fopen(fileName, "wb");
fwrite(&size, 1, 1, fp);
if(nextPlayer) {
fwrite("b", 1, 1, fp);
} else {
fwrite("w", 1, 1, fp);
}
for(i = 0 ; i < size ; i++) {
fwrite(*(board+ i), sizeof(char), size, fp);
}
printf("State of game saved at: %s\n", fileName);
fclose(fp);
} else if(!strcmp(temp, "load")) {
char fileName[100], nextP[5];
subStr(cmd, fileName, 2);
FILE *fp;
fp = fopen(fileName, "rb");
fread(&size, 1, 1, fp);
fread(nextP, 1, 1, fp);
if (!(strcmp(nextP, "w"))) {
nextPlayer = 0;
} else {
nextPlayer = 1;
}
board = malloc(size * sizeof(char *));
for(i = 0 ; i < size ; i++)
*(board + i) = malloc(size * sizeof(char));
for(i = 0 ; i < size ; i++)
fread(*(board + i), sizeof(char), size, fp);
fclose(fp);
filePath = fopen(fileName, "r+");
fseek(filePath, 0, SEEK_END);
boardDisp(board, size);
next(nextPlayer, user);
winCond = 0;
} else if(!strcmp(temp, "showstate")) {
boardDisp(board, size);
next(nextPlayer, user);
} else if(!strcmp(temp, "quit")) {
fclose(filePath);
remove("moves.hex");
return 0;
} else { printf("Uknown Command!\n"); } //Maybe add help command here
} while(1);
}