-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
950 lines (866 loc) · 27 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
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
#include <gtk/gtk.h>
#include <stdlib.h>
// To-Do:
// Make window deletable, add int on_mainWindow_destroy()
// Remove the necessity to press the status button on restart
// Modify the way restart works
// Initialise the variables only once, even if PvP
// Modify glade code and remove unecessary user_data thats being passed
// Add octaves when buttons are clicked
// Modify text colors for stuff
// Modify the X and O's to be bigger fonts size
// Use more code inheritance when buttons are being pressed
/* All code in master branch was written by Gokul Vasudeva
Uploaded to https://github.com/GokulVSD/tictactoe/ */
// function prototyping
int randomNumberLessThan(int n);
void initialising(GtkButton *buttonInit,int i,int j);
int hasAnyoneWon(int a[3][3]);
void setAllButtonsToBlank();
void computerMove();
// declaring global variables
static GtkButton *statusClick,*diffClick,*gameClick;
static GtkButton *button[3][3]={{NULL,NULL,NULL},{NULL,NULL,NULL},{NULL,NULL,NULL}};
// declaring global flags
static int flag=0, gameNotOver=1, initialise=0, statusFlag=0, pressed[3][3]={{0,0,0},{0,0,0},{0,0,0}};
static int moveCounter=0;
// declaring game parameters
static int arr[3][3]={{0,0,0},{0,0,0},{0,0,0}};
static int gameType=0, gameDifficulty=1;
// main method, start of execution
int main(int argc, char *argv[])
{
GtkBuilder *builder;
GtkWidget *window;
gtk_init(&argc, &argv);
// initialising GTKbuilder with .glade file
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "mainUI.glade", NULL);
// initialising main window widget
window = GTK_WIDGET (gtk_builder_get_object(builder, "mainWindow"));
gtk_builder_connect_signals(builder, NULL);
// dereferencing builder pointer (for reuse later)
g_object_unref(builder);
// showing main window widget
gtk_widget_show(window);
gtk_main();
return 0;
}
// called when quit is selected in settings menu
void on_gameExit_activate()
{
gtk_main_quit();
exit(0);
}
// runs the about dialog box
void on_aboutSelection_activate()
{
GtkWidget *about;
GtkBuilder *builder;
// initialising GTKbuilder with .glade file
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "mainUI.glade", NULL);
// initialising about widget
about = GTK_WIDGET(gtk_builder_get_object(builder, "aboutDialog"));
gtk_builder_connect_signals(builder, NULL);
// dereferencing builder for later use
g_object_unref(builder);
gtk_dialog_run (GTK_DIALOG (about));
// when response signal is triggered
gtk_widget_destroy (about);
}
// when gamemode button is pressed
void on_chooseGamemodeButton_clicked(GtkWidget *click_button, gpointer user_data)
{
gameClick = (GtkButton *) user_data;
if(!statusFlag) return;
if(flag)
{
gtk_button_set_label(statusClick, "RESTART TO SELECT GAMEMODE");
return;
}
flag=1; //enable flag at button press, not here. reset flag to 0 in restart
gtk_button_set_label(statusClick, "SELECT GAMEMODE");
GtkBuilder *builder;
GtkWidget *gameDialog;
// initialising GTKbuilder with .glade file
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "mainUI.glade", NULL);
// initialising gamemode selection widget
gameDialog = GTK_WIDGET(gtk_builder_get_object(builder, "chooseGamemodeDialog"));
gtk_builder_connect_signals(builder, NULL);
g_object_unref(builder);
gtk_dialog_run (GTK_DIALOG (gameDialog));
// when window is closed from the x button in toolbar
gtk_widget_destroy(gameDialog);
}
// runs when PvP is selected in settings menu
void on_pvpButton_clicked(GtkWidget *click_button, gpointer user_data)
{
gameType = 0;
gtk_widget_destroy((GtkWidget *) user_data);
gtk_button_set_label(gameClick, "PvP");
}
// runs the choose difficulty pop up when PvC is selected in settings menu
void on_pvcButton_clicked(GtkWidget *click_button, gpointer user_data)
{
gameType = 1;
gtk_widget_destroy((GtkWidget *) user_data);
gtk_button_set_label(gameClick, "PvC");
}
// when difficulty button is pressed
void on_chooseDifficultyButton_clicked(GtkWidget *click_button, gpointer user_data)
{
diffClick = (GtkButton *) user_data;
gtk_button_set_label(statusClick, "SELECT DIFFICULTY");
GtkBuilder *builder;
GtkWidget *diffDialog;
// initialising GTKbuilder with .glade file
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "mainUI.glade", NULL);
// initialising difficulty selection widget
diffDialog = GTK_WIDGET(gtk_builder_get_object(builder, "chooseDifficultyDialog"));
gtk_builder_connect_signals(builder, NULL);
g_object_unref(builder);
gtk_dialog_run (GTK_DIALOG (diffDialog));
// when window is closed from the x button in toolbar
gtk_widget_destroy(diffDialog);
}
// runs when easy is selected in difficulty selection dialog
void on_easyButton_clicked(GtkWidget *click_button, gpointer user_data)
{
gameDifficulty = 0;
gtk_widget_destroy((GtkWidget *) user_data);
gtk_button_set_label(diffClick, "EASY");
}
// runs when medium is selected in difficulty selection dialog
void on_mediumButton_clicked(GtkWidget *click_button, gpointer user_data)
{
gameDifficulty = 1;
gtk_widget_destroy((GtkWidget *) user_data);
gtk_button_set_label(diffClick, "MEDIUM");
}
// runs when hard is selected in difficulty selection dialog
void on_hardButton_clicked(GtkWidget *click_button, gpointer user_data)
{
gameDifficulty = 2;
gtk_widget_destroy((GtkWidget *) user_data);
gtk_button_set_label(diffClick, "HARD");
}
/* runs when restart is selected in settings menu. Destroys old window,
reinitialises all global variables and runs main method */
void on_restartGame_activate(GtkWidget *click_button, gpointer user_data)
{
int i,j;
if(!statusFlag) return;
gameDifficulty=0; gameType=0; flag=0; moveCounter=0; gameNotOver=1; initialise=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
button[i][j]=NULL;
arr[i][j]=0;
pressed[i][j]=0;
}
}
setAllButtonsToBlank();
}
// runs when status button is clicked, used to pass a reference of the status button
void on_statusButton_clicked(GtkWidget *click_button, gpointer user_data)
{
statusClick = (GtkButton *) user_data;
if(!flag)
{
gtk_button_set_label(statusClick, "SELECT GAMEMODE FROM SETTINGS");
statusFlag=1;
}
}
/* initialising mode is used to get a reference to all the buttons in order to change
their labels in PvC mode. This is a downside of using Glade, as the computer
can not press the button when it's its move */
// runs when button 1,1 is clicked
int on_button11_clicked(GtkWidget *click_button, gpointer user_data)
{
GtkButton *buttonTemp = (GtkButton *) user_data;
// runs if in initialising mode
if(initialise!=0)
{
initialising(buttonTemp,0,0);
return 0;
}
// runs if button has already been pressed either while Initialising or in-game
if(pressed[0][0]) return 0;
// runs when button is pressed in-game
if(flag&&gameNotOver)
{
// runs when the button is pressed during an odd move
if(++moveCounter%2)
{
arr[0][0]=1;
gtk_button_set_label(buttonTemp, "X");
pressed[0][0]=1;
switch(hasAnyoneWon(arr))
{
case 0: if(moveCounter==9)
{
gtk_button_set_label(statusClick, "DRAW");
gameNotOver=0; return 0;
} break;
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 2'S MOVE");
if(gameType==1) computerMove();
}
// runs when the button is pressed during an even move
else
{
arr[0][0]=2;
gtk_button_set_label(buttonTemp, "O");
switch(hasAnyoneWon(arr))
{
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
}
pressed[0][0]=1;
}
return 0;
}
// runs when button 1,2 is clicked
int on_button12_clicked(GtkWidget *click_button, gpointer user_data)
{
GtkButton *buttonTemp = (GtkButton *) user_data;
// runs if in initialising mode
if(initialise!=0)
{
initialising(buttonTemp,0,1);
return 0;
}
// runs if button has already been pressed either while Initialising or in-game
if(pressed[0][1]) return 0;
// runs when button is pressed in-game
if(flag&&gameNotOver)
{
// runs when the button is pressed during an odd move
if(++moveCounter%2)
{
arr[0][1]=1;
gtk_button_set_label(buttonTemp, "X");
pressed[0][1]=1;
switch(hasAnyoneWon(arr))
{
case 0: if(moveCounter==9)
{
gtk_button_set_label(statusClick, "DRAW");
gameNotOver=0; return 0;
} break;
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 2'S MOVE");
if(gameType==1) computerMove();
}
// runs when the button is pressed during an even move
else
{
arr[0][1]=2;
gtk_button_set_label(buttonTemp, "O");
switch(hasAnyoneWon(arr))
{
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
}
pressed[0][1]=1;
}
return 0;
}
// runs when button 1,3 is clicked
int on_button13_clicked(GtkWidget *click_button, gpointer user_data)
{
GtkButton *buttonTemp = (GtkButton *) user_data;
// runs if in initialising mode
if(initialise!=0)
{
initialising(buttonTemp,0,2);
return 0;
}
// runs if button has already been pressed either while Initialising or in-game
if(pressed[0][2]) return 0;
// runs when button is pressed in-game
if(flag&&gameNotOver)
{
// runs when the button is pressed during an odd move
if(++moveCounter%2)
{
arr[0][2]=1;
gtk_button_set_label(buttonTemp, "X");
pressed[0][2]=1;
switch(hasAnyoneWon(arr))
{
case 0: if(moveCounter==9)
{
gtk_button_set_label(statusClick, "DRAW");
gameNotOver=0; return 0;
} break;
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 2'S MOVE");
if(gameType==1) computerMove();
}
// runs when the button is pressed during an even move
else
{
arr[0][2]=2;
gtk_button_set_label(buttonTemp, "O");
switch(hasAnyoneWon(arr))
{
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
}
pressed[0][2]=1;
}
return 0;
}
// runs when button 2,1 is clicked
int on_button21_clicked(GtkWidget *click_button, gpointer user_data)
{
GtkButton *buttonTemp = (GtkButton *) user_data;
// runs if in initialising mode
if(initialise!=0)
{
initialising(buttonTemp,1,0);
return 0;
}
// runs if button has already been pressed either while Initialising or in-game
if(pressed[1][0]) return 0;
// runs when button is pressed in-game
if(flag&&gameNotOver)
{
// runs when the button is pressed during an odd move
if(++moveCounter%2)
{
arr[1][0]=1;
gtk_button_set_label(buttonTemp, "X");
pressed[1][0]=1;
switch(hasAnyoneWon(arr))
{
case 0: if(moveCounter==9)
{
gtk_button_set_label(statusClick, "DRAW");
gameNotOver=0; return 0;
} break;
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 2'S MOVE");
if(gameType==1) computerMove();
}
// runs when the button is pressed during an even move
else
{
arr[1][0]=2;
gtk_button_set_label(buttonTemp, "O");
switch(hasAnyoneWon(arr))
{
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
}
pressed[1][0]=1;
}
return 0;
}
// runs when button 2,2 is clicked
int on_button22_clicked(GtkWidget *click_button, gpointer user_data)
{
GtkButton *buttonTemp = (GtkButton *) user_data;
// runs if in initialising mode
if(initialise!=0)
{
initialising(buttonTemp,1,1);
return 0;
}
// runs if button has already been pressed either while Initialising or in-game
if(pressed[1][1]) return 0;
// runs when button is pressed in-game
if(flag&&gameNotOver)
{
// runs when the button is pressed during an odd move
if(++moveCounter%2)
{
arr[1][1]=1;
gtk_button_set_label(buttonTemp, "X");
pressed[1][1]=1;
switch(hasAnyoneWon(arr))
{
case 0: if(moveCounter==9)
{
gtk_button_set_label(statusClick, "DRAW");
gameNotOver=0; return 0;
} break;
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 2'S MOVE");
if(gameType==1) computerMove();
}
// runs when the button is pressed during an even move
else
{
arr[1][1]=2;
gtk_button_set_label(buttonTemp, "O");
switch(hasAnyoneWon(arr))
{
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
}
pressed[1][1]=1;
}
return 0;
}
// runs when button 2,3 is clicked
int on_button23_clicked(GtkWidget *click_button, gpointer user_data)
{
GtkButton *buttonTemp = (GtkButton *) user_data;
// runs if in initialising mode
if(initialise!=0)
{
initialising(buttonTemp,1,2);
return 0;
}
// runs if button has already been pressed either while Initialising or in-game
if(pressed[1][2]) return 0;
// runs when button is pressed in-game
if(flag&&gameNotOver)
{
// runs when the button is pressed during an odd move
if(++moveCounter%2)
{
arr[1][2]=1;
gtk_button_set_label(buttonTemp, "X");
pressed[1][2]=1;
switch(hasAnyoneWon(arr))
{
case 0: if(moveCounter==9)
{
gtk_button_set_label(statusClick, "DRAW");
gameNotOver=0; return 0;
} break;
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 2'S MOVE");
if(gameType==1) computerMove();
}
// runs when the button is pressed during an even move
else
{
arr[1][2]=2;
gtk_button_set_label(buttonTemp, "O");
switch(hasAnyoneWon(arr))
{
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
}
pressed[1][2]=1;
}
return 0;
}
// runs when button 3,1 is clicked
int on_button31_clicked(GtkWidget *click_button, gpointer user_data)
{
GtkButton *buttonTemp = (GtkButton *) user_data;
// runs if in initialising mode
if(initialise!=0)
{
initialising(buttonTemp,2,0);
return 0;
}
// runs if button has already been pressed either while Initialising or in-game
if(pressed[2][0]) return 0;
// runs when button is pressed in-game
if(flag&&gameNotOver)
{
// runs when the button is pressed during an odd move
if(++moveCounter%2)
{
arr[2][0]=1;
gtk_button_set_label(buttonTemp, "X");
pressed[2][0]=1;
switch(hasAnyoneWon(arr))
{
case 0: if(moveCounter==9)
{
gtk_button_set_label(statusClick, "DRAW");
gameNotOver=0; return 0;
} break;
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 2'S MOVE");
if(gameType==1) computerMove();
}
// runs when the button is pressed during an even move
else
{
arr[2][0]=2;
gtk_button_set_label(buttonTemp, "O");
switch(hasAnyoneWon(arr))
{
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
}
pressed[2][0]=1;
}
return 0;
}
// runs when button 3,2 is clicked
int on_button32_clicked(GtkWidget *click_button, gpointer user_data)
{
GtkButton *buttonTemp = (GtkButton *) user_data;
// runs if in initialising mode
if(initialise!=0)
{
initialising(buttonTemp,2,1);
return 0;
}
// runs if button has already been pressed either while Initialising or in-game
if(pressed[2][1]) return 0;
// runs when button is pressed in-game
if(flag&&gameNotOver)
{
// runs when the button is pressed during an odd move
if(++moveCounter%2)
{
arr[2][1]=1;
gtk_button_set_label(buttonTemp, "X");
pressed[2][1]=1;
switch(hasAnyoneWon(arr))
{
case 0: if(moveCounter==9)
{
gtk_button_set_label(statusClick, "DRAW");
gameNotOver=0; return 0;
} break;
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 2'S MOVE");
if(gameType==1) computerMove();
}
// runs when the button is pressed during an even move
else
{
arr[2][1]=2;
gtk_button_set_label(buttonTemp, "O");
switch(hasAnyoneWon(arr))
{
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
}
pressed[2][1]=1;
}
return 0;
}
// runs when button 3,3 is clicked
int on_button33_clicked(GtkWidget *click_button, gpointer user_data)
{
GtkButton *buttonTemp = (GtkButton *) user_data;
// runs if in initialising mode
if(initialise!=0)
{
initialising(buttonTemp,2,2);
return 0;
}
// runs if button has already been pressed either while Initialising or in-game
if(pressed[2][2]) return 0;
// runs when button is pressed in-game
if(flag&&gameNotOver)
{
// runs when the button is pressed during an odd move
if(++moveCounter%2)
{
arr[2][2]=1;
gtk_button_set_label(buttonTemp, "X");
pressed[2][2]=1;
switch(hasAnyoneWon(arr))
{
case 0: if(moveCounter==9)
{
gtk_button_set_label(statusClick, "DRAW");
gameNotOver=0; return 0;
} break;
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 2'S MOVE");
if(gameType==1) computerMove();
}
// runs when the button is pressed during an even move
else
{
arr[2][2]=2;
gtk_button_set_label(buttonTemp, "O");
switch(hasAnyoneWon(arr))
{
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0; return 0;
case 2: gtk_button_set_label(statusClick, "PLAYER 2 WON");
gameNotOver=0; return 0;
}
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
}
pressed[2][2]=1;
}
return 0;
}
/* function for checking if whether any row, column or diagonal is completely
filled by either 0, 1, or 2 and returns either 0,1 or 2 */
int hasAnyoneWon(int a[3][3])
{
int oneWon=0; int twoWon=0; int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]==1) oneWon++;
if(a[i][j]==2) twoWon++;
}
if(oneWon==3) return 1;
if(twoWon==3) return 2;
oneWon=0; twoWon=0;
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[j][i]==1) oneWon++;
if(a[j][i]==2) twoWon++;
}
if(oneWon==3) return 1;
if(twoWon==3) return 2;
oneWon=0; twoWon=0;
}
for(i=0;i<3;i++)
{
if(a[i][i]==1) oneWon++;
if(a[i][i]==2) twoWon++;
}
if(oneWon==3) return 1;
if(twoWon==3) return 2;
oneWon=0; twoWon=0;
for(i=0,j=2;i<3;i++,j--)
{
if(a[i][j]==1) oneWon++;
if(a[i][j]==2) twoWon++;
}
if(oneWon==3) return 1;
if(twoWon==3) return 2;
return 0;
}
// called after initialising mode is completed to reset buttons
void setAllButtonsToBlank()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
gtk_button_set_label(button[i][j], " ");
}
}
}
/* called when it's the computers move. The logic works in such way as to rank
each playable button with a score. If the button has already been pressed, scoring
that button is skipped. Scoring is done in two steps, the first step involves increasing
the score of the button by 1 for each empty button in its row, column, and diagonal
(if it's either in the primary or secondary diagonal). the second step involves
checking whether Player 1 or Computer is about to win, and setting a higher score
to those buttons which either block the winning move or play the winning move. playing
the winning move is scored higher than blocking Player 1's move. Lastly, scores are
added to certain buttons to block Player 1 from reaching to the point where he/she
is assured a garuanteed win. The move to be played is randomised based on the
difficulty. Higher the difficulty, higher the chance that the ideal move is played. */
void computerMove()
{
int i,j,k,l,best=0,x,y;
int tempGame[3][3];
int score[3][3]={{0,0,0},{0,0,0},{0,0,0}};
moveCounter++;
// coping game into a temporary array, to be used for checking if someone is about to win
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
tempGame[i][j]=arr[i][j];
}
}
// running through all 9 buttons
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
// if a move has already been played in the button
if(pressed[i][j]) continue;
// adds score for column and row
for(k=0;k<3;k++)
{
if(arr[i][k]==0) score[i][j]++;
if(arr[k][j]==0) score[i][j]++;
}
// adds score for primary diagonal
if((i==0&&j==0)||(i==2&&j==2)||(i==1&&j==1))
{
for(k=0;k<3;k++)
if(arr[k][k]==0) score[i][j]++;
}
// adds score for secondary diagonal
if((i==0&&j==2)||(i==2&&j==0)||(i==1&&j==1))
{
for(k=0,l=2;k<3;k++,l--)
if(arr[k][l]==0) score[i][j]++;
}
// checks if playing the button causes player 1 to win
tempGame[i][j]=1;
if(hasAnyoneWon(tempGame)==1) score[i][j]+=100;
// checks if playing the button causes computer to win
tempGame[i][j]=2;
if(hasAnyoneWon(tempGame)==2) score[i][j]+=200;
// resets the temporary game array
tempGame[i][j]=0;
}
}
// adjusting scores for certain edge cases to prevent player 1 from assured victory
if(moveCounter==4)
{
if((arr[0][0]==1&&arr[2][2]==1)||(arr[0][2]==1&&arr[2][0]==1))
score[1][0]=300;
if((arr[0][1]==1&&arr[1][0]==1)||(arr[1][0]==1&&arr[0][2]==1)||(arr[0][1]==1&&arr[2][0]==1))
score[0][0]=300;
if((arr[0][1]==1&&arr[1][2]==1)||(arr[0][1]==1&&arr[2][2]==1)||(arr[1][2]==1&&arr[0][0]==1))
score[0][2]=300;
if((arr[1][2]==1&&arr[2][1]==1)||(arr[1][2]==1&&arr[2][0]==1)||(arr[2][1]==1&&arr[0][2]==1))
score[2][2]=300;
if((arr[2][1]==1&&arr[1][0]==1)||(arr[2][1]==1&&arr[0][0]==1)||(arr[1][0]==1&&arr[2][2]==1))
score[2][0]=300;
}
// finds the highest scored button
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(score[i][j]>best)
{
best=score[i][j]; x=i; y=j;
}
}
}
// alters the highest scored move to be played if a random number matches criteria, lower chance
if(gameDifficulty==1)
{
if(randomNumberLessThan(10)<4)
{
do
{
x=randomNumberLessThan(3); y=randomNumberLessThan(3);
}
while(pressed[x][y]);
}
}
// alters the highest scored move to be played if a random number matches criteria, higher chance
if(gameDifficulty==0)
{
if(randomNumberLessThan(10)<7)
{
do
{
x=randomNumberLessThan(3); y=randomNumberLessThan(3);
}
while(pressed[x][y]);
}
}
// plays the chosen button
arr[x][y]=2;
pressed[x][y]=1;
gtk_button_set_label(button[x][y], "O");
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
// checks to see if anyone won
switch(hasAnyoneWon(arr))
{
case 1: gtk_button_set_label(statusClick, "PLAYER 1 WON");
gameNotOver=0;
break;
case 2: gtk_button_set_label(statusClick, "COMPUTER WON");
gameNotOver=0;
break;
}
}
// runs for the first time each button is pressed
void initialising(GtkButton *buttonInit,int i,int j)
{
if(button[i][j]!=NULL) return;
button[i][j]=buttonInit;
gtk_button_set_label(button[i][j], "OK");
if(++initialise==10)
{
initialise=0;
gtk_button_set_label(statusClick, "PLAYER 1'S MOVE");
setAllButtonsToBlank();
}
}
// random number generator, in the range of 0 < x < n
int randomNumberLessThan(int n)
{
return (double)rand()*n/RAND_MAX;
}
// end of program