-
Notifications
You must be signed in to change notification settings - Fork 1
/
othello.c
642 lines (592 loc) · 14 KB
/
othello.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/**
*
@file othello.c
*
@brief Funções auxiliares
*
*
*
*
@author Pedro,
pedro.s.conceicao@ua.pt
*
https://github.com/pedroconceicao/Othello
*
@internal
*
Created 31-Out-2017
*
Company University of Aveiro
*
Copyright Copyright (c) 2017, Pedro
*
*
=====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include "pcolor.h"
#include "othello.h"
/**
* @brief Gets the input to choose either singleplayer or multiplayer
* @param void
* @return Op char. 1 for singleplayer, 2 for multiplayer
*/
char Option(void)
{
char op=0;
while(op!='1' && op!='2')
{
printf("\nType '1' to singleplayer or '2' to multiplayer\nOption:");
op=getchar();
getchar();
fflush(stdin);
}
return op;
}
/**
* @brief InitGame
* @param Pointer to game char array (*jg)
* @return void
*/
void InitGame( othello *jg){ // todas as céluas a VAZIO exceto as 4 centrais
//Inicializar todas as células com vazio ' '
for(int l=0;l<8;l++)
{
for(int c=0;c<8;c++)
{
jg -> T[l][c] = VAZIO;
}
}
//Inicializar células do jogador 1 com 'O'
jg -> T[3][3] = JOGADOR1;
jg -> T[4][4] = JOGADOR1;
//Inicializar células do jogador 2 com 'X'
jg -> T[3][4] = JOGADOR2;
jg -> T[4][3] = JOGADOR2;
}
/**
* @brief Prints game board
* @param Pointer to game char array (*jg)
* @return void
*/
void PrintGameBoard(othello *jg)
{
int l, c;
printf("\n\n");
printf(" a b c d e f g h\n");
for(l=0;l<8;l++)
{
printf(" +-------------------------------+\n");
printf(" %d ",l+1);
for(c=0;c<8;c++)
{
printf("|");
if(jg->T[l][c]==JOGADOR1){textcolor(RESET,BLUE,WHITE);}
if(jg->T[l][c]==JOGADOR2){textcolor(RESET,RED,WHITE);}
printf(" %c ",jg -> T[l][c]);
ResetTextColors();
}
printf("| %d \n",l+1);
}
printf(" +-------------------------------+\n");
printf(" a b c d e f g h\n");
}
/**
* @brief Saves game board
* @param Pointer to game char array (*jg) and pointer to filename
* @return 1 if succesful ; 0 if unsuccesful
*/
int SaveGame(othello *jg, char *filename)
{
FILE *fp=fopen(filename,"w");
if(fp == NULL) return 0;
for(int l=0;l<8;l++)
{
for(int c=0;c<7;c++)
{
fprintf(fp,"%c,",jg -> T[l][c]);
}
fprintf(fp,"%c\n",jg -> T[l][7]);
}
fclose(fp);
return 1;
}
/**
* @brief Loads game board
* @param Pointer to game char array (*jg) and pointer to filename
* @return 1 if succesful ; 0 if unsuccesful
*/
int LoadGame(othello *jg, char *filename)
{
FILE *fp=fopen(filename,"rb");
if(fp == NULL) return 0;
fseek(fp, 0, SEEK_END);
int size = ftell(fp);
fseek(fp, 0, SEEK_SET); //same as rewind(f);
char *str = malloc(size + 1);
fread(str, size, 1, fp);
fclose(fp);
char *token;
int c=0;
token=strtok(str,",\n");
while(token!=NULL)
{
jg -> T[0][c]= * token;
token=strtok(NULL,",\n");
c++;//next column
}
free(str);
return 1;
}
/**
* @brief Reads keyboard and computes player move
* @param void
* @return player move from 11 to 88; 11 to quit; 10 to load game; 9 to save game
*/
int GetPlayerMove(othello *jg)
{
char c_cor, l, aux;
int c=0;
do{
printf("Next move coordinates or command: ");
scanf(" %c",&c_cor);
scanf("%c",&l);
if(isalpha(l)){
aux=l;
l=c_cor;
c_cor=aux;
}
if(c_cor=='q' || c_cor=='Q'){
exit(0);
}
if(c_cor=='s' || c_cor =='S'){
int save_game=SaveGame(jg,"othello.txt");
printf("\nSaving game...\n");
if(save_game) printf("Game saved!\n");
else printf("Game not saved.\n");
c=90;
break;
}
if(c_cor=='l' || c_cor =='L'){
int load_game=LoadGame(jg,"othello.txt");
printf ("\033c");
printf("\nLoading game...\n");
if(load_game){printf("Game loaded!");PrintGameBoard( jg );printf("\n");}
else{printf("Game not found.\nExiting\n");break;}
c=91;
break;
}
if(atoi(&l)<1 || atoi(&l)>8){
c_cor='0';
}
if(c_cor!='l' || c_cor!='L' || c_cor!='s' || c_cor!='S'){
switch(c_cor){
case'a':
case'A':
c=1;break;
case'b':
case'B':
c=2;break;
case'c':
case'C':
c=3;break;
case'd':
case'D':
c=4;break;
case'e':
case'E':
c=5;break;
case'f':
case'F':
c=6;break;
case'g':
case'G':
c=7;break;
case'h':
case'H':
c=8;break;
default:
printf("Invalid Option. Try again! \n");
}
}
}while(c==0);
if(c==90 || c==91){return 9999;}
else{return c*10+atoi(&l);}
}
/**
* @brief Checks if the given move is possible
* @param Pointer to board array, Desired move, player
* @return Int 1 if move is possible, Int 0 if move is not possible
*/
int CheckMove( othello *jg , int move , int player )
{
for(int l=0;l<8;l++)
{
for(int c=0;c<8;c++)
{
if(jg -> T[l][c] == player)
{
int check_right=CheckRight(jg,l,c,player);
if(check_right==move){ReversePieces(jg,move,player);return 1;}
int check_left=CheckLeft(jg,l,c,player);
if(check_left==move){ReversePieces(jg,move,player);return 1;}
int check_up=CheckUp(jg,l,c,player);
if(check_up==move){ReversePieces(jg,move,player);return 1;}
int check_down=CheckDown(jg,l,c,player);
if(check_down==move){ReversePieces(jg,move,player);return 1;}
int check_upright=CheckUpRight(jg,l,c,player);
if(check_upright==move){ReversePieces(jg,move,player);return 1;}
int check_upleft=CheckUpLeft(jg,l,c,player);
if(check_upleft==move){ReversePieces(jg,move,player);return 1;}
int check_downright=CheckDownRight(jg,l,c,player);
if(check_downright==move){ReversePieces(jg,move,player);return 1;}
int check_downleft=CheckDownLeft(jg,l,c,player);
if(check_downleft==move){ReversePieces(jg,move,player);return 1;}
}
}
}
return 0;
}
/**
* @brief Checks the right direction of a given cell
* @param Pointer to board array, int line, int collumn, player
* @return Possible move coordinates if there's any else returns 0
*/
int CheckRight (othello *jg , int l , int c , int player)
{
int opponent=0;
if(player==JOGADOR1){opponent=JOGADOR2;}
if(player==JOGADOR2){opponent=JOGADOR1;}
if(jg -> T[l][c+1] == opponent)
{
do{c++;}while(jg -> T[l][c] == opponent && c<7);
if(jg-> T[l][c] == VAZIO){return (c+1)*10+l+1;}
}
return 0;
}
/**
* @brief Checks the left direction of a given cell
* @param Pointer to board array, int line, int collumn, player
* @return Possible move coordinates if there's any else returns 0
*/
int CheckLeft (othello *jg , int l , int c , int player)
{
int opponent=0;
if(player==JOGADOR1){opponent=JOGADOR2;}
if(player==JOGADOR2){opponent=JOGADOR1;}
if(jg -> T[l][c-1] == opponent)
{
do{c--;}while(jg -> T[l][c] == opponent && c>0);
if(jg-> T[l][c] == VAZIO){return (c+1)*10+l+1;}
}
return 0;
}
/**
* @brief Checks the up direction of a given cell
* @param Pointer to board array, int line, int collumn, player
* @return Possible move coordinates if there's any else returns 0
*/
int CheckUp (othello *jg , int l , int c , int player)
{
int opponent=0;
if(player==JOGADOR1){opponent=JOGADOR2;}
if(player==JOGADOR2){opponent=JOGADOR1;}
if(jg -> T[l-1][c] == opponent)
{
do{l--;}while(jg -> T[l][c] == opponent && l>0);
if(jg-> T[l][c] == VAZIO){return (c+1)*10+l+1;}
}
return 0;
}
/**
* @brief Checks the down direction of a given cell
* @param Pointer to board array, int line, int collumn, player
* @return Possible move coordinates if there's any else returns 0
*/
int CheckDown (othello *jg , int l , int c , int player)
{
int opponent=0;
if(player==JOGADOR1){opponent=JOGADOR2;}
if(player==JOGADOR2){opponent=JOGADOR1;}
if(jg -> T[l+1][c] == opponent)
{
do{l++;}while(jg -> T[l][c] == opponent && l<7);
if(jg-> T[l][c] == VAZIO){return (c+1)*10+l+1;}
}
return 0;
}
/**
* @brief Checks the upright direction of a given cell
* @param Pointer to board array, int line, int collumn, player
* @return Possible move coordinates if there's any else returns 0
*/
int CheckUpRight (othello *jg , int l , int c , int player)
{
int opponent=0;
if(player==JOGADOR1){opponent=JOGADOR2;}
if(player==JOGADOR2){opponent=JOGADOR1;}
if(jg -> T[l-1][c+1] == opponent)
{
do{l--;c++;}while(jg -> T[l][c] == opponent && l>0 && c<7);
if(jg-> T[l][c] == VAZIO){return (c+1)*10+l+1;}
}
return 0;
}
/**
* @brief Checks the upleft direction of a given cell
* @param Pointer to board array, int line, int collumn, player
* @return Possible move coordinates if there's any else returns 0
*/
int CheckUpLeft (othello *jg , int l , int c , int player)
{
int opponent=0;
if(player==JOGADOR1){opponent=JOGADOR2;}
if(player==JOGADOR2){opponent=JOGADOR1;}
if(jg -> T[l-1][c-1] == opponent)
{
do{l--;c--;}while(jg -> T[l][c] == opponent && l>0 && c>0);
if(jg-> T[l][c] == VAZIO){return (c+1)*10+l+1;}
}
return 0;
}
/**
* @brief Checks the downright direction of a given cell
* @param Pointer to board array, int line, int collumn, player
* @return Possible move coordinates if there's any else returns 0
*/
int CheckDownRight (othello *jg , int l , int c , int player)
{
int opponent=0;
if(player==JOGADOR1){opponent=JOGADOR2;}
if(player==JOGADOR2){opponent=JOGADOR1;}
if(jg -> T[l+1][c+1] == opponent)
{
do{l++;c++;}while(jg -> T[l][c] == opponent && l<7 && c<7);
if(jg-> T[l][c] == VAZIO){return (c+1)*10+l+1;}
}
return 0;
}
/**
* @brief Checks the downleft direction of a given cell
* @param Pointer to board array, int line, int collumn, player
* @return Possible move coordinates if there's any else returns 0
*/
int CheckDownLeft (othello *jg , int l , int c , int player)
{
int opponent=0;
if(player==JOGADOR1){opponent=JOGADOR2;}
if(player==JOGADOR2){opponent=JOGADOR1;}
if(jg -> T[l+1][c-1] == opponent)
{
do{l++;c--;}while(jg -> T[l][c] == opponent && l<7 && c>0);
if(jg-> T[l][c] == VAZIO){return (c+1)*10+l+1;}
}
return 0;
}
/**
* @brief Reverses the pieces after a play
* @param Pointer to board array, int move (cell the user chose to play), player
* @return void
*/
void ReversePieces(othello *jg , int move , int player)
{
int l=move%10-1;
int c=move/10-1;
int cc=10, ll=10;
int flag=0;
jg -> T[l][c]=player;
//right
for(cc=c+1 ; cc<8 ; cc++){
if(jg -> T[l][cc] == player){flag=1;break;}
if(jg -> T[l][cc] == VAZIO) break;
}
if(flag){
for(int n=c+1 ; n<cc ; n++){
jg -> T[l][n]=player;
}
}
flag=0;
//left
for(cc=c-1 ; cc>-1 ; cc--){
if(jg -> T[l][cc] == player){flag=1;break;}
if(jg -> T[l][cc] == VAZIO) break;
}
if(flag){
for(int n=c-1 ; n>cc ; n--){
jg -> T[l][n]=player;
}
}
flag=0;
//up
for(ll=l+1 ; ll<8 ; ll++){
if(jg -> T[ll][c] == player){flag=1;break;}
if(jg -> T[ll][c] == VAZIO) break;
}
if(flag){
for(int n=l+1 ; n<ll ; n++){
jg -> T[n][c]=player;
}
}
flag=0;
//down
for(ll=l-1 ; ll>-1 ; ll--){
if(jg -> T[ll][c] == player){flag=1;break;}
if(jg -> T[ll][c] == VAZIO) break;
}
if(flag){
for(int n=l-1 ; n>ll ; n--){
jg -> T[n][c]=player;
}
}
flag=0;
//upright
for(ll=l-1 ; ll>-1 ; ll--){
if(c-(ll-l)>8) break;
if(jg -> T[ll][c-(ll-l)] == player){flag=1;break;}
if(jg -> T[ll][c-(ll-l)] == VAZIO) break;
}
if(flag){
for(int n=l-1 ; n>ll ; n--){
jg -> T[n][c-(n-l)]=player;
}
}
flag=0;
//downright
for(ll=l+1 ; ll<8 ; ll++){
if(c+(ll-l)>8) break;
if(jg -> T[ll][c+(ll-l)] == player){flag=1;break;}
if(jg -> T[ll][c+(ll-l)] == VAZIO) break;
}
if(flag){
for(int n=l+1 ; n<ll ; n++){
jg -> T[n][c+(n-l)]=player;
}
}
flag=0;
//upleft
for(ll=l-1 ; ll>-1 ; ll--){
if(c+(ll-l)<0) break;
if(jg -> T[ll][c+(ll-l)] == player){flag=1;break;}
if(jg -> T[ll][c+(ll-l)] == VAZIO) break;
}
if(flag){
for(int n=l-1 ; n>ll ; n--){
jg -> T[n][c+(n-l)]=player;
}
}
flag=0;
//downleft
for(ll=l+1 ; ll<8 ; ll++){
if(c-(ll-l)<0) break;
if(jg -> T[ll][c-(ll-l)] == player){flag=1;break;}
if(jg -> T[ll][c-(ll-l)] == VAZIO) break;
}
if(flag){
for(int n=l+1 ; n<ll ; n++){
jg -> T[n][c-(n-l)]=player;
}
}
flag=0;
}
/**
* @brief Checks if there are any possible moves for a given player
* @param Pointer to board array, player
* @return 1 if there are no possible moves, 0 if there is at least one possible move
*/
int WinGame(othello *jg,int player)
{
int flag=0, l, c;
for( l=0;l<8;l++)
{
for( c=0;c<8;c++)
{
if(jg->T[l][c]==player){
if(CheckRight(jg,l,c,player)){flag=1;break;}
if(CheckLeft(jg,l,c,player)){flag=1;break;}
if(CheckUp(jg,l,c,player)){flag=1;break;}
if(CheckDown(jg,l,c,player)){flag=1;break;}
if(CheckUpLeft(jg,l,c,player)){flag=1;break;}
if(CheckUpRight(jg,l,c,player)){flag=1;break;}
if(CheckDownLeft(jg,l,c,player)){flag=1;break;}
if(CheckDownRight(jg,l,c,player)){flag=1;break;}
}
}
if(flag) break;
}
if(!flag){
return 1;
}
return 0;
}
/**
* @brief Generates a move for the CPU
* @param Pointer to game board
* @return Move to be played
*/
int GenerateMove(othello *jg)
{
int moves[100]={0};
int moves_count=0;
for(int l=0;l<8;l++)
{
for(int c=0;c<8;c++)
{
if(jg -> T[l][c] == JOGADOR2)
{
int check_right=CheckRight(jg,l,c,JOGADOR2);
if(check_right){
moves[moves_count]=check_right;
moves_count++;
}
int check_left=CheckLeft(jg,l,c,JOGADOR2);
if(check_left){
moves[moves_count]=check_left;
moves_count++;
}
int check_up=CheckUp(jg,l,c,JOGADOR2);
if(check_up){
moves[moves_count]=check_up;
moves_count++;
}
int check_down=CheckDown(jg,l,c,JOGADOR2);
if(check_down){
moves[moves_count]=check_down;
moves_count++;
}
int check_upright=CheckUpRight(jg,l,c,JOGADOR2);
if(check_upright){
moves[moves_count]=check_upright;
moves_count++;
}
int check_upleft=CheckUpLeft(jg,l,c,JOGADOR2);
if(check_upleft){
moves[moves_count]=check_upleft;
moves_count++;
}
int check_downright=CheckDownRight(jg,l,c,JOGADOR2);
if(check_downright){
moves[moves_count]=check_downright;
moves_count++;
}
int check_downleft=CheckDownLeft(jg,l,c,JOGADOR2);
if(check_downleft){
moves[moves_count]=check_downleft;
moves_count++;
}
}
}
}
int i=0;
while(moves[i]!=0)
{
i++;
}
i--;
srand(time(NULL));
if(i>0){i=rand()%i;}
return moves[i];
}