-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1540 lines (1481 loc) · 85.5 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
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <SDL3/SDL.h>
#ifdef SDL_PLATFORM_WINDOWS
#include <time.h>
#include <SDL3/SDL_image.h>
#include <SDL3/SDL_ttf.h>
#else
#include <SDL3_image/SDL_image.h>
#include <SDL3_ttf/SDL_ttf.h>
#endif
#include <ctype.h>
#include "game_board.h"
#include "decision_tree.h"
#define THREADS 2
#define DLS_LIMIT 8
#define CREDITS "Authors:\n- MOUHOUS Mathya (G3)\n- AIT MEDDOUR Fouâd-Eddine (G1)\nSoftware Used:\n- SDL3 master (https://github.com/libsdl-org/SDL)\n- SDL3_ttf master (https://github.com/libsdl-org/SDL_ttf)\n- SDL3_image master (https://github.com/libsdl-org/SDL_image)\n- CMake 3.30.6 (https://gitlab.kitware.com/cmake/cmake)\nFont : Acme 9 Regular\nTested On :\n- Ubuntu 24.10\n- Gentoo amd64 Stable\n- Windows 10 KVM/QEMU\n- Windows 11"
#define RESIZE_HANDLER \
int h, w;\
SDL_GetWindowSize(window, &w, &h);\
window_resolution.x = (float) w;\
window_resolution.y = (float) h;\
scale_ratio = (SDL_FPoint){window_resolution.x / 1366, window_resolution.y / 768};\
TTF_SetFontSize(font,SDL_min(scale_ratio.x, scale_ratio.y) * 30);\
TTF_SetFontSize(font_underlined,SDL_min(scale_ratio.x, scale_ratio.y) * 30);\
TTF_SetFontSize(font_credits,SDL_min(scale_ratio.x, scale_ratio.y) * 23);\
board_dims = window_resolution.y - 300;\
piece_size = board_dims / 5;\
empty_square_size = piece_size / 3;\
graphical_board = (SDL_FRect){\
(window_resolution.x - board_dims) / 2, (window_resolution.y - board_dims) / 2 + 70,\
board_dims, board_dims\
};\
hot_points[0] = (SDL_FPoint){graphical_board.x, graphical_board.y};\
hot_points[1] = (SDL_FPoint){graphical_board.x + board_dims / 2, graphical_board.y};\
hot_points[2] = (SDL_FPoint){graphical_board.x + board_dims, graphical_board.y};\
hot_points[3] = (SDL_FPoint){graphical_board.x, graphical_board.y + board_dims / 2};\
hot_points[4] = (SDL_FPoint){\
graphical_board.x + board_dims / 2, graphical_board.y + board_dims / 2\
};\
hot_points[5] = (SDL_FPoint){\
graphical_board.x + board_dims, graphical_board.y + board_dims / 2\
};\
hot_points[6] = (SDL_FPoint){graphical_board.x, graphical_board.y + board_dims};\
hot_points[7] = (SDL_FPoint){\
graphical_board.x + board_dims / 2, graphical_board.y + board_dims\
};\
hot_points[8] = (SDL_FPoint){\
graphical_board.x + board_dims, graphical_board.y + board_dims\
};\
text_w = 0;\
text_h = 0;\
TTF_GetTextSize(CREDITS_text, &text_w, &text_h);\
CREDITS_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 75, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(WELCOME_text, &text_w, &text_h);\
WELCOME_x = (window_resolution.x - (float) text_w) / 2;\
TTF_GetTextSize(ORDER_text, &text_w, &text_h);\
ORDER_x = (window_resolution.x - (float) text_w) / 2;\
TTF_GetTextSize(ABOUT_TITLE_text, &text_w, &text_h);\
ABOUT_TITLE_x = (window_resolution.x - (float) text_w) / 2;\
TTF_GetTextSize(SETTINGS_TITLE_text, &text_w, &text_h);\
SETTINGS_TITLE_x = (window_resolution.x - (float) text_w) / 2;\
TTF_GetTextSize(ROUNDS_text, &text_w, &text_h);\
ROUNDS_x = (window_resolution.x - (float) text_w) / 2;\
TTF_GetTextSize(AI_IS_THINKING_text, &text_w, &text_h);\
AI_THINKING_x = (window_resolution.x - (float) text_w) / 2;\
AI_THINKING_y = (window_resolution.y - (float) text_h);\
TTF_GetTextSize(BACK_text, &text_w, &text_h);\
BACK_rect = (SDL_FRect){\
0, (window_resolution.y - (float) text_h), (float) text_w, (float) text_h\
};\
TTF_GetTextSize(RETRY_text, &text_w, &text_h);\
RETRY_rect = (SDL_FRect){\
0, (window_resolution.y - (float) text_h) / 2, (float) text_w, (float) text_h\
};\
TTF_GetTextSize(MAIN_MENU_text, &text_w, &text_h);\
MAIN_MENU_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w), (window_resolution.y - (float) text_h) / 2,\
(float) text_w,\
(float) text_h\
};\
menu_y = scale_ratio.y * 50;\
menu_x = 0;\
TTF_GetTextSize(PVP_text, &text_w, &text_h);\
PVP_rect = (SDL_FRect){\
menu_x, menu_y += scale_ratio.y * 100, (float) text_w, (float) text_h\
};\
TTF_GetTextSize(PVA_text, &text_w, &text_h);\
PVA_rect = (SDL_FRect){\
menu_x, menu_y += scale_ratio.y * 100, (float) text_w, (float) text_h\
};\
TTF_GetTextSize(AVA_text, &text_w, &text_h);\
AVA_rect = (SDL_FRect){\
menu_x, menu_y += scale_ratio.y * 100, (float) text_w, (float) text_h\
};\
TTF_GetTextSize(SETTINGS_text, &text_w, &text_h);\
SETTINGS_rect = (SDL_FRect){\
menu_x, menu_y += scale_ratio.y * 100, (float) text_w, (float) text_h\
};\
TTF_GetTextSize(ABOUT_text, &text_w, &text_h);\
ABOUT_rect = (SDL_FRect){\
menu_x, menu_y += scale_ratio.y * 100, (float) text_w, (float) text_h\
};\
TTF_GetTextSize(QUIT_text, &text_w, &text_h);\
QUIT_rect = (SDL_FRect){\
menu_x, menu_y += scale_ratio.y * 100, (float) text_w, (float) text_h\
};\
TTF_GetTextSize(AIFirst_text, &text_w, &text_h);\
AIFirst_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 4, scale_ratio.y * 400, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(PlayerFirst_text, &text_w, &text_h);\
PlayerFirst_rect = (SDL_FRect){\
3 * (window_resolution.x - (float) text_w) / 4, scale_ratio.y * 400, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(ENTROPY_text, &text_w, &text_h);\
ENTROPY_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 100, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(FULLSCREEN_text, &text_w, &text_h);\
FULLSCREEN_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 100, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(PLAYER1_COLOR_text, &text_w, &text_h);\
PLAYER1_COLOR_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 300, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(PLAYER1_COLOR_setting_text, &text_w, &text_h);\
PLAYER1_COLOR_setting_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 400, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(PLAYER2_COLOR_text, &text_w, &text_h);\
PLAYER2_COLOR_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 500, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(PLAYER2_COLOR_setting_text, &text_w, &text_h);\
PLAYER2_COLOR_setting_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 600, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(AI_ONE_text, &text_w, &text_h);\
AI_ONE_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 200, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(AI_TWO_text, &text_w, &text_h);\
AI_TWO_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 400, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(NO_RANDOMNESS_text, &text_w, &text_h);\
NO_RANDOMNESS_rect = (SDL_FRect){0, scale_ratio.y * 300, (float) text_w, (float) text_h};\
TTF_GetTextSize(SOME_RANDOMNESS_text, &text_w, &text_h);\
SOME_RANDOMNESS_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 300, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(ALL_RANDOMNESS_text, &text_w, &text_h);\
ALL_RANDOMNESS_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w), scale_ratio.y * 300, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(NO_RANDOMNESS2_text, &text_w, &text_h);\
NO_RANDOMNESS2_rect = (SDL_FRect){0, scale_ratio.y * 500, (float) text_w, (float) text_h};\
TTF_GetTextSize(SOME_RANDOMNESS2_text, &text_w, &text_h);\
SOME_RANDOMNESS2_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w) / 2, scale_ratio.y * 500, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(ALL_RANDOMNESS2_text, &text_w, &text_h);\
ALL_RANDOMNESS2_rect = (SDL_FRect){\
(window_resolution.x - (float) text_w), scale_ratio.y * 500, (float) text_w,\
(float) text_h\
};\
TTF_GetTextSize(NEXT_text, &text_w, &text_h);\
TTF_DrawRendererText(NEXT_text, window_resolution.x - (float) text_w,\
window_resolution.y - (float) text_h);\
NEXT_rect = (SDL_FRect){\
window_resolution.x - (float) text_w, window_resolution.y - (float) text_h,\
(float) text_w, (float) text_h\
};
typedef enum ACHI_SCENE {
ACHI_MENU,
ACHI_SETTINGS,
ACHI_ABOUT,
ACHI_PREGAME_ROUNDS,
ACHI_PREGAME_PVA,
ACHI_PREGAME_AVA,
ACHI_GAME_START,
ACHI_END
} ACHI_SCENE;
typedef enum GAME_MODE {
NONE,
GAME_MODE_PVP,
GAME_MODE_PVA,
GAME_MODE_AVA
} GAME_MODE;
typedef enum RANDOMNESS {
NO_RAND,
SOME_RAND,
ALL_RAND
} RANDOMNESS;
const int adjacencyMatrix2[9][3] = {
{1, 3, 4},
{0, 2, 4},
{1, 5, 4},
{0, 6, 4},
{4, 4, 4}, // Place holder
{2, 8, 4},
{3, 7, 4},
{6, 8, 4},
{7, 5, 4}
};
const int adjacent_to_center[8] = {0, 1, 2, 3, 5, 6, 7, 8};
int main(void) {
#ifdef SDL_PLATFORM_WINDOWS
srand(time(0));
#endif
SDL_Window *window;
SDL_Renderer *renderer;
SDL_FPoint window_resolution = {1366, 768};
SDL_FPoint scale_ratio = {window_resolution.x / 1366, window_resolution.y / 768};
float board_dims = window_resolution.y - 300;
float piece_size = board_dims / 5;
float empty_square_size = piece_size / 3;
int player1_color_hex = 0xd0baff;
int player2_color_hex = 0xcfffdd;
SDL_Color player1_color = {
player1_color_hex / 0x10000, (player1_color_hex % 0x10000) / 0x100, (player1_color_hex % 0x10000) % 0x100,
SDL_ALPHA_OPAQUE
};
SDL_Color player2_color = {
player2_color_hex / 0x10000, (player2_color_hex % 0x10000) / 0x100, (player2_color_hex % 0x10000) % 0x100,
SDL_ALPHA_OPAQUE
};
if (!SDL_Init(SDL_INIT_VIDEO) || !TTF_Init()) {
SDL_LogCritical(SDL_LOG_CATEGORY_VIDEO, "Error initializing SDL : %s\n", SDL_GetError());
return 1;
}
if (!SDL_CreateWindowAndRenderer("Achi Game", (int) window_resolution.x, (int) window_resolution.y,
SDL_WINDOW_RESIZABLE, &window,
&renderer)) {
SDL_LogCritical(SDL_LOG_CATEGORY_RENDER, "Error Creating Window and Renderer : %s\n", SDL_GetError());
return 1;
}
SDL_Texture *unoccupied_square = IMG_LoadTexture(renderer, "../media/unoccupied_piece.svg");
if (unoccupied_square == nullptr) {
SDL_Log("Cannot import assets : %s\n", SDL_GetError());
return 1;
}
SDL_Texture *player_occupied = IMG_LoadTexture(renderer, "../media/player_piece.svg");
if (player_occupied == nullptr) {
SDL_Log("Cannot import assets : %s\n", SDL_GetError());
return 1;
}
ACHI_SCENE scene = ACHI_MENU;
bool quit = false;
bool skip_cycle = false;
int max_rounds = 0;
int round = 1;
int selected = -1;
int selected_color = 0;
int state = 0;
GAME_MODE game_mode = NONE;
RANDOMNESS randomness_first_ai = NO_RAND;
RANDOMNESS randomness_second_ai = NO_RAND;
bool ai_first = true;
bool order_selected = false;
bool second_ai_rand_selected = false;
SDL_SetWindowIcon(window, IMG_Load("../media/miku.png"));
TTF_Font *font = TTF_OpenFont("../media/Acme 9 Regular.ttf", 30);
TTF_Font *font_underlined = TTF_OpenFont("../media/Acme 9 Regular.ttf", 30);
TTF_Font *font_credits = TTF_OpenFont("../media/Acme 9 Regular.ttf", 23);
TTF_SetFontWrapAlignment(font_credits, TTF_HORIZONTAL_ALIGN_CENTER);
TTF_SetFontStyle(font_underlined,TTF_STYLE_UNDERLINE);
TTF_TextEngine *text_engine = TTF_CreateRendererTextEngine(renderer);
SDL_Cursor *pointing_cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_POINTER);
SDL_Cursor *default_cursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
board game_board = nullptr;
board board_cleaner = nullptr;
SDL_FRect squares[9];
char buf[1024] = {0};
char ai_desc[100] = {0};
char ai2_desc[100] = {0};
SDL_Event event;
SDL_FRect graphical_board = {
(window_resolution.x - board_dims) / 2, (window_resolution.y - board_dims) / 2 + 70, board_dims, board_dims
};
SDL_FPoint hot_points[9];
hot_points[0] = (SDL_FPoint){graphical_board.x, graphical_board.y};
hot_points[1] = (SDL_FPoint){graphical_board.x + board_dims / 2, graphical_board.y};
hot_points[2] = (SDL_FPoint){graphical_board.x + board_dims, graphical_board.y};
hot_points[3] = (SDL_FPoint){graphical_board.x, graphical_board.y + board_dims / 2};
hot_points[4] = (SDL_FPoint){
graphical_board.x + board_dims / 2, graphical_board.y + board_dims / 2
};
hot_points[5] = (SDL_FPoint){graphical_board.x + board_dims, graphical_board.y + board_dims / 2};
hot_points[6] = (SDL_FPoint){graphical_board.x, graphical_board.y + board_dims};
hot_points[7] = (SDL_FPoint){graphical_board.x + board_dims / 2, graphical_board.y + board_dims};
hot_points[8] = (SDL_FPoint){graphical_board.x + board_dims, graphical_board.y + board_dims};
TTF_Text *WELCOME_text = TTF_CreateText(text_engine, font, "Welcome to the Achi game!", 0);
TTF_Text *PVP_text = TTF_CreateText(text_engine, font, "1) Player VS Player", 0);
TTF_Text *PVA_text = TTF_CreateText(text_engine, font, "2) Player VS AI", 0);
TTF_Text *AVA_text = TTF_CreateText(text_engine, font, "3) AI VS AI", 0);
TTF_Text *SETTINGS_text = TTF_CreateText(text_engine, font, "4) Settings", 0);
TTF_Text *ABOUT_text = TTF_CreateText(text_engine, font, "5) About", 0);
TTF_Text *QUIT_text = TTF_CreateText(text_engine, font, "6) Quit to desktop", 0);
TTF_SetTextColor(QUIT_text, 0xFF, 0x00, 0x00,SDL_ALPHA_OPAQUE);
TTF_Text *TITLE_text = nullptr;
TTF_Text *INPUT_text = nullptr;
TTF_Text *ROUND_text = nullptr;
TTF_Text *FULLSCREEN_text = TTF_CreateText(text_engine, font, "Toggle Full Screen",
0);
TTF_Text *PLAYER1_COLOR_text = TTF_CreateText(text_engine, font, "Player 1 Piece Color",
0);
TTF_Text *PLAYER2_COLOR_text = TTF_CreateText(text_engine, font, "Player 2 Piece Color",
0);
TTF_Text *PLAYER1_COLOR_setting_text = TTF_CreateText(text_engine, font, "#d0baff",
0);
TTF_Text *PLAYER2_COLOR_setting_text = TTF_CreateText(text_engine, font, "#cfffdd",
0);
TTF_Text *ROUNDS_text = TTF_CreateText(text_engine, font, "How many rounds do you want to play? (Minimum 6)",
0);
TTF_Text *NEXT_text = TTF_CreateText(text_engine, font_underlined, "Next", 0);
TTF_Text *BACK_text = TTF_CreateText(text_engine, font_underlined, "Go Back", 0);
TTF_Text *PVP_TITLE_text = TTF_CreateText(text_engine, font, "Player VS Player Mode", 0);
TTF_Text *PVA_TITLE_text = TTF_CreateText(text_engine, font, "Player VS Minimax Mode", 0);
TTF_Text *AVA_TITLE_text = TTF_CreateText(text_engine, font, "Minimax VS Minimax Mode", 0);
TTF_Text *ORDER_text = TTF_CreateText(text_engine, font, "Who would you like to start first ?", 0);
TTF_Text *ENTROPY_text = TTF_CreateText(text_engine, font, "Would you like to add Randomness ?", 0);
TTF_Text *AIFirst_text = TTF_CreateText(text_engine, font, "Minimax", 0);
TTF_Text *AI_IS_THINKING_text = TTF_CreateText(text_engine, font, "AI is thinking...", 0);
TTF_Text *PlayerFirst_text = TTF_CreateText(text_engine, font, "Player", 0);
TTF_Text *AI_ONE_text = TTF_CreateText(text_engine, font_underlined, "AI N°1 Mode: ", 0);
TTF_Text *AI_TWO_text = TTF_CreateText(text_engine, font_underlined, "AI N°2 Mode: ", 0);
TTF_Text *NO_RANDOMNESS_text = TTF_CreateText(text_engine, font, "No Randomness", 0);
TTF_Text *SOME_RANDOMNESS_text = TTF_CreateText(text_engine, font, "Some Randomness", 0);
TTF_Text *ALL_RANDOMNESS_text = TTF_CreateText(text_engine, font, "All Randomness", 0);
TTF_Text *NO_RANDOMNESS2_text = TTF_CreateText(text_engine, font, "No Randomness", 0);
TTF_Text *SOME_RANDOMNESS2_text = TTF_CreateText(text_engine, font, "Some Randomness", 0);
TTF_Text *ALL_RANDOMNESS2_text = TTF_CreateText(text_engine, font, "All Randomness", 0);
TTF_Text *RETRY_text = TTF_CreateText(text_engine, font_underlined, "Play Again", 0);
TTF_Text *MAIN_MENU_text = TTF_CreateText(text_engine, font_underlined, "Main Menu", 0);
TTF_Text *ABOUT_TITLE_text = TTF_CreateText(text_engine, font, "About the project", 0);
TTF_Text *SETTINGS_TITLE_text = TTF_CreateText(text_engine, font, "Settings", 0);
TTF_Text *CREDITS_text = TTF_CreateText(text_engine, font_credits,CREDITS, 0);
int text_w = 0;
int text_h = 0;
TTF_GetTextSize(CREDITS_text, &text_w, &text_h);
SDL_FRect CREDITS_rect = {(window_resolution.x - (float) text_w) / 2, 75, (float) text_w, (float) text_h};
TTF_GetTextSize(WELCOME_text, &text_w, &text_h);
float WELCOME_x = (window_resolution.x - (float) text_w) / 2;
TTF_GetTextSize(ORDER_text, &text_w, &text_h);
float ORDER_x = (window_resolution.x - (float) text_w) / 2;
TTF_GetTextSize(ABOUT_TITLE_text, &text_w, &text_h);
float ABOUT_TITLE_x = (window_resolution.x - (float) text_w) / 2;
TTF_GetTextSize(SETTINGS_TITLE_text, &text_w, &text_h);
float SETTINGS_TITLE_x = (window_resolution.x - (float) text_w) / 2;
TTF_GetTextSize(ROUNDS_text, &text_w, &text_h);
float ROUNDS_x = (window_resolution.x - (float) text_w) / 2;
TTF_GetTextSize(AI_IS_THINKING_text, &text_w, &text_h);
float AI_THINKING_x = (window_resolution.x - (float) text_w) / 2;
float AI_THINKING_y = (window_resolution.y - (float) text_h);
TTF_GetTextSize(BACK_text, &text_w, &text_h);
SDL_FRect BACK_rect = {0, (window_resolution.y - (float) text_h), (float) text_w, (float) text_h};
TTF_GetTextSize(RETRY_text, &text_w, &text_h);
SDL_FRect RETRY_rect = {0, (window_resolution.y - (float) text_h) / 2, (float) text_w, (float) text_h};
TTF_GetTextSize(MAIN_MENU_text, &text_w, &text_h);
SDL_FRect MAIN_MENU_rect = {
(window_resolution.x - (float) text_w), (window_resolution.y - (float) text_h) / 2, (float) text_w,
(float) text_h
};
float menu_y = 50;
float menu_x = 0;
TTF_GetTextSize(PVP_text, &text_w, &text_h);
SDL_FRect PVP_rect = {menu_x, menu_y += 100, (float) text_w, (float) text_h};
TTF_GetTextSize(PVA_text, &text_w, &text_h);
SDL_FRect PVA_rect = {menu_x, menu_y += 100, (float) text_w, (float) text_h};
TTF_GetTextSize(AVA_text, &text_w, &text_h);
SDL_FRect AVA_rect = {menu_x, menu_y += 100, (float) text_w, (float) text_h};
TTF_GetTextSize(SETTINGS_text, &text_w, &text_h);
SDL_FRect SETTINGS_rect = {menu_x, menu_y += 100, (float) text_w, (float) text_h};
TTF_GetTextSize(ABOUT_text, &text_w, &text_h);
SDL_FRect ABOUT_rect = {menu_x, menu_y += 100, (float) text_w, (float) text_h};
TTF_GetTextSize(QUIT_text, &text_w, &text_h);
SDL_FRect QUIT_rect = {menu_x, menu_y += 100, (float) text_w, (float) text_h};
TTF_GetTextSize(AIFirst_text, &text_w, &text_h);
SDL_FRect AIFirst_rect = {(window_resolution.x - (float) text_w) / 4, 400, (float) text_w, (float) text_h};
TTF_GetTextSize(PlayerFirst_text, &text_w, &text_h);
SDL_FRect PlayerFirst_rect = {3 * (window_resolution.x - (float) text_w) / 4, 400, (float) text_w, (float) text_h};
TTF_GetTextSize(ENTROPY_text, &text_w, &text_h);
SDL_FRect ENTROPY_rect = {(window_resolution.x - (float) text_w) / 2, 100, (float) text_w, (float) text_h};
TTF_GetTextSize(FULLSCREEN_text, &text_w, &text_h);
SDL_FRect FULLSCREEN_rect = {(window_resolution.x - (float) text_w) / 2, 100, (float) text_w, (float) text_h};
TTF_GetTextSize(PLAYER1_COLOR_text, &text_w, &text_h);
SDL_FRect PLAYER1_COLOR_rect = {(window_resolution.x - (float) text_w) / 2, 300, (float) text_w, (float) text_h};
TTF_GetTextSize(PLAYER1_COLOR_setting_text, &text_w, &text_h);
SDL_FRect PLAYER1_COLOR_setting_rect = {
(window_resolution.x - (float) text_w) / 2, 400, (float) text_w, (float) text_h
};
TTF_GetTextSize(PLAYER2_COLOR_text, &text_w, &text_h);
SDL_FRect PLAYER2_COLOR_rect = {(window_resolution.x - (float) text_w) / 2, 500, (float) text_w, (float) text_h};
TTF_GetTextSize(PLAYER2_COLOR_setting_text, &text_w, &text_h);
SDL_FRect PLAYER2_COLOR_setting_rect = {
(window_resolution.x - (float) text_w) / 2, 600, (float) text_w, (float) text_h
};
TTF_GetTextSize(AI_ONE_text, &text_w, &text_h);
SDL_FRect AI_ONE_rect = {(window_resolution.x - (float) text_w) / 2, 200, (float) text_w, (float) text_h};
TTF_GetTextSize(AI_TWO_text, &text_w, &text_h);
SDL_FRect AI_TWO_rect = {(window_resolution.x - (float) text_w) / 2, 400, (float) text_w, (float) text_h};
TTF_GetTextSize(NO_RANDOMNESS_text, &text_w, &text_h);
SDL_FRect NO_RANDOMNESS_rect = {0, 300, (float) text_w, (float) text_h};
TTF_GetTextSize(SOME_RANDOMNESS_text, &text_w, &text_h);
SDL_FRect SOME_RANDOMNESS_rect = {(window_resolution.x - (float) text_w) / 2, 300, (float) text_w, (float) text_h};
TTF_GetTextSize(ALL_RANDOMNESS_text, &text_w, &text_h);
SDL_FRect ALL_RANDOMNESS_rect = {(window_resolution.x - (float) text_w), 300, (float) text_w, (float) text_h};
TTF_GetTextSize(NO_RANDOMNESS2_text, &text_w, &text_h);
SDL_FRect NO_RANDOMNESS2_rect = {0, 500, (float) text_w, (float) text_h};
TTF_GetTextSize(SOME_RANDOMNESS2_text, &text_w, &text_h);
SDL_FRect SOME_RANDOMNESS2_rect = {(window_resolution.x - (float) text_w) / 2, 500, (float) text_w, (float) text_h};
TTF_GetTextSize(ALL_RANDOMNESS2_text, &text_w, &text_h);
SDL_FRect ALL_RANDOMNESS2_rect = {(window_resolution.x - (float) text_w), 500, (float) text_w, (float) text_h};
TTF_GetTextSize(NEXT_text, &text_w, &text_h);
TTF_DrawRendererText(NEXT_text, window_resolution.x - (float) text_w, window_resolution.y - (float) text_h);
SDL_FRect NEXT_rect = {
window_resolution.x - (float) text_w, window_resolution.y - (float) text_h, (float) text_w, (float) text_h
};
SDL_FPoint mouse;
SDL_PropertiesID input_properties_id = SDL_CreateProperties();
SDL_SetNumberProperty(input_properties_id,SDL_PROP_TEXTINPUT_TYPE_NUMBER, SDL_TEXTINPUT_TYPE_NUMBER);
float x_pos = 0;
float y_pos = 0;
int cursor_poll_delay = 500;
bool pointing = false;
while (!quit) {
SDL_GetMouseState(&x_pos, &y_pos);
mouse.x = x_pos;
mouse.y = y_pos;
SDL_RenderClear(renderer);
if (cursor_poll_delay <= 0) {
if (pointing) {
SDL_SetCursor(pointing_cursor);
pointing = false;
}
else
SDL_SetCursor(default_cursor);
cursor_poll_delay = 500;
} else
cursor_poll_delay--;
switch (scene) {
case ACHI_MENU:
if (SDL_PointInRectFloat(&mouse, &PVP_rect)) {
SDL_SetRenderDrawColor(renderer, 0x15, 0x15, 0x15,SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &PVP_rect);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00,SDL_ALPHA_OPAQUE);
pointing = true;
}
if (SDL_PointInRectFloat(&mouse, &PVA_rect)) {
SDL_SetRenderDrawColor(renderer, 0x15, 0x15, 0x15,SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &PVA_rect);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00,SDL_ALPHA_OPAQUE);
pointing = true;
}
if (SDL_PointInRectFloat(&mouse, &AVA_rect)) {
SDL_SetRenderDrawColor(renderer, 0x15, 0x15, 0x15,SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &AVA_rect);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00,SDL_ALPHA_OPAQUE);
pointing = true;
}
if (SDL_PointInRectFloat(&mouse, &SETTINGS_rect)) {
SDL_SetRenderDrawColor(renderer, 0x15, 0x15, 0x15,SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &SETTINGS_rect);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00,SDL_ALPHA_OPAQUE);
pointing = true;
}
if (SDL_PointInRectFloat(&mouse, &ABOUT_rect)) {
SDL_SetRenderDrawColor(renderer, 0x15, 0x15, 0x15,SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &ABOUT_rect);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00,SDL_ALPHA_OPAQUE);
pointing = true;
}
if (SDL_PointInRectFloat(&mouse, &QUIT_rect)) {
SDL_SetRenderDrawColor(renderer, 0x15, 0x15, 0x15,SDL_ALPHA_OPAQUE);
SDL_RenderFillRect(renderer, &QUIT_rect);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00,SDL_ALPHA_OPAQUE);
pointing = true;
}
TTF_DrawRendererText(WELCOME_text, WELCOME_x, 0);
TTF_DrawRendererText(PVP_text, PVP_rect.x, PVP_rect.y);
TTF_DrawRendererText(PVA_text, PVA_rect.x, PVA_rect.y);
TTF_DrawRendererText(AVA_text, AVA_rect.x, AVA_rect.y);
TTF_DrawRendererText(SETTINGS_text, SETTINGS_rect.x, SETTINGS_rect.y);
TTF_DrawRendererText(ABOUT_text, ABOUT_rect.x, ABOUT_rect.y);
TTF_DrawRendererText(QUIT_text, QUIT_rect.x, QUIT_rect.y);
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_WINDOW_RESIZED:
RESIZE_HANDLER
break;
case SDL_EVENT_QUIT: quit = true;
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
if (event.button.button == SDL_BUTTON_LEFT) {
if (SDL_PointInRectFloat(&mouse, &PVP_rect)) {
scene = ACHI_PREGAME_ROUNDS;
TITLE_text = PVP_TITLE_text;
game_mode = GAME_MODE_PVP;
}
if (SDL_PointInRectFloat(&mouse, &PVA_rect)) {
scene = ACHI_PREGAME_ROUNDS;
TITLE_text = PVA_TITLE_text;
game_mode = GAME_MODE_PVA;
}
if (SDL_PointInRectFloat(&mouse, &AVA_rect)) {
scene = ACHI_PREGAME_ROUNDS;
TITLE_text = AVA_TITLE_text;
game_mode = GAME_MODE_AVA;
}
if (SDL_PointInRectFloat(&mouse, &SETTINGS_rect)) {
scene = ACHI_SETTINGS;
}
if (SDL_PointInRectFloat(&mouse, &ABOUT_rect)) {
scene = ACHI_ABOUT;
}
if (SDL_PointInRectFloat(&mouse, &QUIT_rect)) {
quit = true;
}
}
break;
default:
break;
}
}
break;
case ACHI_PREGAME_ROUNDS:
TTF_GetTextSize(TITLE_text, &text_w, &text_h);
TTF_DrawRendererText(TITLE_text, (window_resolution.x - (float) text_w) / 2, 0);
TTF_DrawRendererText(BACK_text, 0, BACK_rect.y);
TTF_DrawRendererText(ROUNDS_text, ROUNDS_x, scale_ratio.y * 200);
if (INPUT_text != nullptr)
TTF_DestroyText(INPUT_text);
INPUT_text = TTF_CreateText(text_engine, font, buf, 0);
TTF_GetTextSize(INPUT_text, &text_w, &text_h);
TTF_DrawRendererText(INPUT_text, (window_resolution.x - (float) text_w) / 2, scale_ratio.y * 300);
if (strlen(buf) > 0)
max_rounds = (int) strtol(buf, nullptr, 10);
else
max_rounds = 0;
SDL_StartTextInputWithProperties(window, input_properties_id);
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_WINDOW_RESIZED:
RESIZE_HANDLER
break;
case SDL_EVENT_QUIT: quit = true;
break;
case SDL_EVENT_TEXT_INPUT:
if (event.text.text[0] >= '0' && event.text.text[0] <= '9' && strlen(buf) < 3)
strcat(buf, event.text.text);
break;
case SDL_EVENT_KEY_DOWN:
if (event.key.key == SDLK_BACKSPACE && strlen(buf) > 0)
buf[strlen(buf) - 1] = '\0';
else if (event.key.key == SDLK_RETURN && max_rounds >= 6) {
switch (game_mode) {
case GAME_MODE_PVP: scene = ACHI_GAME_START;
break;
case GAME_MODE_PVA: scene = ACHI_PREGAME_PVA;
break;
case GAME_MODE_AVA: scene = ACHI_PREGAME_AVA;
break;
default: SDL_Log("Shouldn't happen yet here we are");
exit(EXIT_FAILURE);
}
}
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
if (event.button.button == SDL_BUTTON_LEFT) {
if (SDL_PointInRectFloat(&mouse, &NEXT_rect) && max_rounds >= 6) {
switch (game_mode) {
case GAME_MODE_PVP: scene = ACHI_GAME_START;
break;
case GAME_MODE_PVA: scene = ACHI_PREGAME_PVA;
break;
case GAME_MODE_AVA: scene = ACHI_PREGAME_AVA;
break;
default: SDL_Log("Shouldn't happen yet here we are");
exit(EXIT_FAILURE);
}
}
if (SDL_PointInRectFloat(&mouse, &BACK_rect)) {
scene = ACHI_MENU;
game_mode = NONE;
}
}
break;
default: ;
}
}
SDL_StopTextInput(window);
if (max_rounds >= 6) {
TTF_DrawRendererText(NEXT_text, NEXT_rect.x, NEXT_rect.y);
if (SDL_PointInRectFloat(&mouse, &NEXT_rect))
pointing = true;
}
if (SDL_PointInRectFloat(&mouse, &BACK_rect))
pointing = true;
break;
case ACHI_PREGAME_PVA:
TTF_GetTextSize(TITLE_text, &text_w, &text_h);
TTF_DrawRendererText(TITLE_text, (window_resolution.x - (float) text_w) / 2, 0);
TTF_DrawRendererText(BACK_text, 0, BACK_rect.y);
TTF_DrawRendererText(ORDER_text, ORDER_x, 200);
TTF_DrawRendererText(AIFirst_text, AIFirst_rect.x, AIFirst_rect.y);
TTF_DrawRendererText(PlayerFirst_text, PlayerFirst_rect.x, PlayerFirst_rect.y);
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_WINDOW_RESIZED:
RESIZE_HANDLER
break;
case SDL_EVENT_QUIT: quit = true;
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
if (SDL_PointInRectFloat(&mouse, &BACK_rect)) {
scene = ACHI_PREGAME_ROUNDS;
}
if (SDL_PointInRectFloat(&mouse, &AIFirst_rect)) {
ai_first = true;
order_selected = true;
TTF_SetTextColor(AIFirst_text, 0xFF, 0x00, 0x00,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(PlayerFirst_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
}
if (SDL_PointInRectFloat(&mouse, &PlayerFirst_rect)) {
ai_first = false;
order_selected = true;
TTF_SetTextColor(PlayerFirst_text, 0xFF, 0x00, 0x00,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(AIFirst_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
}
if (SDL_PointInRectFloat(&mouse, &NEXT_rect) && order_selected)
scene = ACHI_GAME_START;
break;
case SDL_EVENT_KEY_DOWN:
if (event.key.key == SDLK_RETURN && order_selected)
scene = ACHI_GAME_START;
break;
default: ;
}
}
if (order_selected) {
TTF_DrawRendererText(NEXT_text, NEXT_rect.x, NEXT_rect.y);
if (SDL_PointInRectFloat(&mouse, &NEXT_rect))
pointing = true;
}
if (SDL_PointInRectFloat(&mouse, &BACK_rect) || SDL_PointInRectFloat(&mouse, &PlayerFirst_rect) ||
SDL_PointInRectFloat(&mouse, &AIFirst_rect))
pointing = true;
break;
case ACHI_PREGAME_AVA:
TTF_GetTextSize(TITLE_text, &text_w, &text_h);
TTF_DrawRendererText(TITLE_text, (window_resolution.x - (float) text_w) / 2, 0);
TTF_DrawRendererText(BACK_text, 0, BACK_rect.y);
TTF_DrawRendererText(ENTROPY_text, ENTROPY_rect.x, ENTROPY_rect.y);
TTF_DrawRendererText(AI_ONE_text, AI_ONE_rect.x, AI_ONE_rect.y);
TTF_DrawRendererText(AI_TWO_text, AI_TWO_rect.x, AI_TWO_rect.y);
TTF_DrawRendererText(NO_RANDOMNESS_text, NO_RANDOMNESS_rect.x, NO_RANDOMNESS_rect.y);
TTF_DrawRendererText(SOME_RANDOMNESS_text, SOME_RANDOMNESS_rect.x, SOME_RANDOMNESS_rect.y);
TTF_DrawRendererText(ALL_RANDOMNESS_text, ALL_RANDOMNESS_rect.x, ALL_RANDOMNESS_rect.y);
TTF_DrawRendererText(NO_RANDOMNESS2_text, NO_RANDOMNESS2_rect.x, NO_RANDOMNESS2_rect.y);
TTF_DrawRendererText(SOME_RANDOMNESS2_text, SOME_RANDOMNESS2_rect.x, SOME_RANDOMNESS2_rect.y);
TTF_DrawRendererText(ALL_RANDOMNESS2_text, ALL_RANDOMNESS2_rect.x, ALL_RANDOMNESS2_rect.y);
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_WINDOW_RESIZED:
RESIZE_HANDLER
break;
case SDL_EVENT_QUIT: quit = true;
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
if (SDL_PointInRectFloat(&mouse, &BACK_rect)) {
scene = ACHI_PREGAME_ROUNDS;
}
if (SDL_PointInRectFloat(&mouse, &NO_RANDOMNESS_rect)) {
order_selected = true;
randomness_first_ai = NO_RAND;
TTF_SetTextColor(NO_RANDOMNESS_text, 0xFF, 0x00, 0x00,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(SOME_RANDOMNESS_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(ALL_RANDOMNESS_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
strcpy(ai_desc, "Minimax");
}
if (SDL_PointInRectFloat(&mouse, &SOME_RANDOMNESS_rect)) {
order_selected = true;
randomness_first_ai = SOME_RAND;
TTF_SetTextColor(SOME_RANDOMNESS_text, 0xFF, 0x00, 0x00,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(ALL_RANDOMNESS_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(NO_RANDOMNESS_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
strcpy(ai_desc, "Half-Random");
}
if (SDL_PointInRectFloat(&mouse, &ALL_RANDOMNESS_rect)) {
order_selected = true;
randomness_first_ai = ALL_RAND;
TTF_SetTextColor(ALL_RANDOMNESS_text, 0xFF, 0x00, 0x00,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(SOME_RANDOMNESS_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(NO_RANDOMNESS_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
strcpy(ai_desc, "Random");
}
if (SDL_PointInRectFloat(&mouse, &NO_RANDOMNESS2_rect)) {
second_ai_rand_selected = true;
randomness_second_ai = NO_RAND;
TTF_SetTextColor(NO_RANDOMNESS2_text, 0xFF, 0x00, 0x00,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(SOME_RANDOMNESS2_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(ALL_RANDOMNESS2_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
strcpy(ai2_desc, "Minimax");
}
if (SDL_PointInRectFloat(&mouse, &SOME_RANDOMNESS2_rect)) {
second_ai_rand_selected = true;
randomness_second_ai = SOME_RAND;
TTF_SetTextColor(SOME_RANDOMNESS2_text, 0xFF, 0x00, 0x00,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(ALL_RANDOMNESS2_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(NO_RANDOMNESS2_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
strcpy(ai2_desc, "Half-Random");
}
if (SDL_PointInRectFloat(&mouse, &ALL_RANDOMNESS2_rect)) {
second_ai_rand_selected = true;
randomness_second_ai = ALL_RAND;
TTF_SetTextColor(ALL_RANDOMNESS2_text, 0xFF, 0x00, 0x00,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(SOME_RANDOMNESS2_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
TTF_SetTextColor(NO_RANDOMNESS2_text, 0xFF, 0xFF, 0xFF,SDL_ALPHA_OPAQUE);
strcpy(ai2_desc, "Random");
}
if (SDL_PointInRectFloat(&mouse, &NEXT_rect) && order_selected && second_ai_rand_selected) {
TTF_DestroyText(TITLE_text);
char buf2[1024];
sprintf(buf2, "%s AI VS %s AI Mode", ai_desc, ai2_desc);
TITLE_text = TTF_CreateText(text_engine, font, buf2, 0);
scene = ACHI_GAME_START;
}
break;
case SDL_EVENT_KEY_DOWN:
if (event.key.key == SDLK_RETURN && order_selected) {
TTF_DestroyText(TITLE_text);
char buf2[1024];
sprintf(buf2, "%s AI VS %s AI Mode", ai_desc, ai2_desc);
TITLE_text = TTF_CreateText(text_engine, font, buf2, 0);
scene = ACHI_GAME_START;
}
break;
default: ;
}
}
if (order_selected && second_ai_rand_selected) {
TTF_DrawRendererText(NEXT_text, NEXT_rect.x, NEXT_rect.y);
if (SDL_PointInRectFloat(&mouse, &NEXT_rect))
pointing = true;
}
if (SDL_PointInRectFloat(&mouse, &BACK_rect) || SDL_PointInRectFloat(&mouse, &ALL_RANDOMNESS_rect) ||
SDL_PointInRectFloat(&mouse, &SOME_RANDOMNESS_rect) || SDL_PointInRectFloat(
&mouse, &NO_RANDOMNESS_rect) || SDL_PointInRectFloat(&mouse, &ALL_RANDOMNESS2_rect) ||
SDL_PointInRectFloat(&mouse, &SOME_RANDOMNESS2_rect) || SDL_PointInRectFloat(
&mouse, &NO_RANDOMNESS2_rect))
pointing = true;
break;
case ACHI_GAME_START:
if (round <= max_rounds) {
int dls_limit = DLS_LIMIT;
int turn = (round % 2 != 0) ? 1 : 2;
TTF_GetTextSize(TITLE_text, &text_w, &text_h);
TTF_DrawRendererText(TITLE_text, (window_resolution.x - (float) text_w) / 2, 0);
if (ROUND_text != nullptr)
TTF_DestroyText(ROUND_text);
if (game_board == nullptr) {
game_board = create_board();
board_cleaner = game_board;
}
if (is_winning(game_board) || round > max_rounds) {
state = is_winning(game_board);
scene = ACHI_END;
}
switch (game_mode) {
case GAME_MODE_PVA:
if (ai_first)
sprintf(buf, "Round N°%d - %s Phase - %s", round, (round <= 6) ? "Placement" : "Moving",
(turn == 1) ? "AI's turn" : "Players turns");
else
sprintf(buf, "Round N°%d - %s Phase - %s", round, (round <= 6) ? "Placement" : "Moving",
(turn == 2) ? "AI's turn" : "Players turns");
break;
case GAME_MODE_PVP:
sprintf(buf, "Round N°%d - %s Phase - Player %d turn", round,
(round <= 6) ? "Placement" : "Moving", turn);
break;
case GAME_MODE_AVA:
sprintf(buf, "Round N°%d - %s turn Phase - %s", round,
(round <= 6) ? "Placement" : "Moving",
(round % 2 != 0) ? ai_desc : ai2_desc);
default: ;
}
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00,SDL_ALPHA_OPAQUE);
ROUND_text = TTF_CreateText(text_engine, font, buf, 0);
TTF_GetTextSize(ROUND_text, &text_w, &text_h);
TTF_DrawRendererText(ROUND_text, (window_resolution.x - (float) text_w) / 2, 75);
SDL_SetRenderDrawColor(renderer, 0xCE, 0xF1, 0xF2,SDL_ALPHA_OPAQUE);
SDL_RenderRect(renderer, &graphical_board);
SDL_RenderLine(renderer, hot_points[1].x, hot_points[1].y, hot_points[7].x, hot_points[7].y);
SDL_RenderLine(renderer, hot_points[2].x, hot_points[2].y, hot_points[6].x, hot_points[6].y);
SDL_RenderLine(renderer, hot_points[3].x, hot_points[3].y, hot_points[5].x, hot_points[5].y);
SDL_RenderLine(renderer, hot_points[0].x, hot_points[0].y, hot_points[8].x, hot_points[8].y);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00,SDL_ALPHA_OPAQUE);
for (int i = 0; i < 9; ++i) {
switch (game_board[i].occupied_by) {
case -1:
squares[i].x = hot_points[i].x - piece_size / 2;
squares[i].y = hot_points[i].y - piece_size / 2;
squares[i].w = squares[i].h = piece_size;
if (i == selected) {
squares[i].w = squares[i].h = piece_size + 20;
squares[i].x = hot_points[i].x - (piece_size + 20) / 2;
squares[i].y = hot_points[i].y - (piece_size + 20) / 2;
} else
squares[i].w = squares[i].h = piece_size;
SDL_SetTextureColorModFloat(player_occupied, player2_color.r, player2_color.g,
player2_color.b);
SDL_RenderTexture(renderer, player_occupied, nullptr, &squares[i]);
break;
case 0:
squares[i].x = hot_points[i].x - empty_square_size / 2;
squares[i].y = hot_points[i].y - empty_square_size / 2;
squares[i].w = squares[i].h = empty_square_size;
if (round > 6 && selected != -1) {
if (selected == 4) {
if (SDL_PointInRectFloat(&mouse, &squares[i]))
pointing = true;
SDL_SetTextureColorModFloat(unoccupied_square, 0xFF, 0x00, 0x00);
} else {
for (int j = 0; j < 3; ++j) {
if (i == adjacencyMatrix2[selected][j]) {
if (SDL_PointInRectFloat(&mouse, &squares[i]))
pointing = true;
SDL_SetTextureColorModFloat(unoccupied_square, 0xFF, 0x00, 0x00);
break;
} else
SDL_SetTextureColorModFloat(unoccupied_square, 0xFF, 0xff, 0xFF);
}
}
} else
SDL_SetTextureColorModFloat(unoccupied_square, 0xFF, 0xff, 0xFF);
SDL_RenderTexture(renderer, unoccupied_square, nullptr, &squares[i]);
break;
case 1:
squares[i].x = hot_points[i].x - piece_size / 2;
squares[i].y = hot_points[i].y - piece_size / 2;
squares[i].w = squares[i].h = piece_size;
if (i == selected) {
squares[i].w = squares[i].h = piece_size + 20;
squares[i].x = hot_points[i].x - (piece_size + 20) / 2;
squares[i].y = hot_points[i].y - (piece_size + 20) / 2;
} else
squares[i].w = squares[i].h = piece_size;
SDL_SetTextureColorModFloat(player_occupied, player1_color.r, player1_color.g,
player1_color.b);
SDL_RenderTexture(renderer, player_occupied, nullptr, &squares[i]);
break;
default: ;
}
}
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_WINDOW_RESIZED:
RESIZE_HANDLER
break;
case SDL_EVENT_QUIT: quit = true;
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
if (game_mode == GAME_MODE_PVP) {
if (round <= 6) {
for (int i = 0; i < 9; ++i) {
if (game_board[i].occupied_by == 0 &&
SDL_PointInRectFloat(&mouse, &squares[i])) {
game_board = next_board(game_board, i, round++);
free(board_cleaner);
board_cleaner = game_board;
break;
}
}
} else {
for (int i = 0; i < 9; ++i) {
if ((game_board[i].occupied_by == ((turn == 2) ? -1 : turn)) &&
SDL_PointInRectFloat(&mouse, &squares[i])) {
selected = i;
}
if (selected != -1) {
int number_played = 0;
int player_squares[3] = {-1, -1, -1};
int number_adjacent = 0;
int adjacent_squares[3] = {-1, -1, -1};
int selected_index;
get_played(game_board, &number_played, ((turn == 2) ? -1 : turn),
player_squares);
for (int j = 0; j < number_played; ++j) {
if (selected == player_squares[j]) {
selected_index = j;
break;
}
}
get_adjacent(game_board, &number_adjacent, selected, adjacent_squares);
for (int j = 0; j < number_adjacent; ++j) {
int current_adjacent = (selected == 4)
? adjacent_to_center[adjacent_squares[j]]
: adjacencyMatrix2[selected][
adjacent_squares
[j]];
if (SDL_PointInRectFloat(&mouse, &squares[current_adjacent])) {
selected = -1;
game_board =
next_board(game_board, selected_index * 3 + j, round++);
free(board_cleaner);
board_cleaner = game_board;
break;
}
}
}
}
}
}
if (game_mode == GAME_MODE_PVA) {
if (turn == ((ai_first) ? 2 : 1)) {
if (round <= 6) {
for (int i = 0; i < 9; ++i) {
if (game_board[i].occupied_by == 0 &&
SDL_PointInRectFloat(&mouse, &squares[i])) {
game_board = next_board(game_board, i, round++);
free(board_cleaner);
board_cleaner = game_board;
break;
}
}
} else {
for (int i = 0; i < 9; ++i) {
if ((game_board[i].occupied_by == ((ai_first) ? -1 : 1)) &&
SDL_PointInRectFloat(&mouse, &squares[i])) {
selected = i;
}