-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1663 lines (1462 loc) · 53 KB
/
main.cpp
File metadata and controls
1663 lines (1462 loc) · 53 KB
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 <string>
#include <iostream>
#include <ctime>
#include <map>
#include <sstream>
#include <cstring>
using namespace std;
struct Player
{
bool first = false;
char name;
};
string mainStatus[3][12] = {{"5Y", "0", "0", "0", "3X", "0", "5X", "0", "0", "0", "0", "2Y"},
{"0", "0", "0", "0", "FX", "D1", "D2",
"FY", "0", "0", "0", "0"},
{"5X", "0", "0", "0", "3Y", "0", "5Y", "0", "0", "0", "0", "2X"}};
string newStr[3][12] = {};
void welcomeSms();
void startPlayer(int *); //decides heither player X or Y is going to start
void firstPlayer(string mainStatus[3][12], int, Player &, Player &); //give some privileges to the first player
void play(string mainStatus[3][12], Player, Player); //start to play the game
void log(char, int, int);
void log(int, int);
void initialState(string mainStatus[][12]);
void initialStateConsole(string mainStatus[][12]); // just to test
void dice_in_grapth(string mainStatus[][12], int, int);
string to_string(char);
int char_to_int(char);
string int_to_char(int);
char string_to_char(string, int);
void move_x(string mainStatus[3][12], int, char[]);
void move_y(string mainStatus[3][12], int, char[]);
void decrement(int, int, string a[3][12]);
char get_first_letter(char[]);
int get_number(char[]);
int letter_to_number(char);
string get_latter_from_grapth(string);
int fromFileX = 0;
int fromFileY = 0;
void fromFile(int *, int *);
void toFile(int, int);
bool checkFlag(string mainStatus[][12], char, int, int);
void pick_flakes(string mainStatus[][12], int, int *, int *);
void DiceInFile(string mainStatus[3][12], int, int);
void DiceFromFile(string mainStatus[3][12], int *, int *);
//void actual_dices(string mainStatus[][12], int, int);
int main(void)
{
initialStateConsole(mainStatus);
welcomeSms();
return 0;
}
//ELIMINAR INITIALSTATE CONSOLE ORGANIZAR O GRAFICO AO IMPRIMIR
void welcomeSms()
{
initialState(mainStatus);
printf("WELCOME TO OUR GAME, ENTER A NUMBER\n");
printf("1==> NEW GAME\n2==> CONTINUE GAME\n3==> QUIT\n");
int a;
if (scanf("%d", &a) == 1)
{
switch (a)
{
case 1:
{
initialState(mainStatus);
Player X, Y;
int start_player = 0;
startPlayer(&start_player);
//set privilegies to the first player
firstPlayer(mainStatus, start_player, X, Y);
play(mainStatus, X, Y);
initialState(mainStatus);
}
break;
case 2:
{
DiceFromFile(newStr, &fromFileX, &fromFileY);
initialState(newStr);
int comp = char_to_int(string_to_char(newStr[1][5],0));
if(comp == 0)
{
printf("NO PREVIOUS GAME RECORDED!\n");
break;
}
else
{
//initialStateConsole(newStr);
Player X, Y;
int start_player = 0;
startPlayer(&start_player);
//set privilegies to the first player
firstPlayer(mainStatus, start_player, X, Y);
play(newStr, X, Y);
initialState(newStr);
}
}
break;
case 3:
printf("BYE BYE!!\n");
break;
}
}
}
void DiceInFile(string mainStatus[3][12], int a, int b)
{
//using sequential file
FILE *continueGame;
if ((continueGame = fopen("continueGame.dat", "w")) == NULL)
{
printf("LOG.txt file not found \n");
}
else
{
fprintf(continueGame, "%d %d ", a, b);
for (int i = 0; i < 3; i++)
{
for (int t = 0; t < 12; t++)
{
string a = mainStatus[i][t];
fprintf(continueGame, "%s ", a.c_str());
}
}
fclose(continueGame);
}
}
void DiceFromFile(string newStr[3][12], int *a, int *b)
{
FILE *continueGame;
if ((continueGame = fopen("continueGame.dat", "r")) == NULL)
{
printf("LOG.txt file not found \n");
}
else
{
int xa, xb;
fscanf(continueGame, "%d %d ", &xa, &xb);
for (int i = 0; i < 3; i++)
{
for (int t = 0; t < 12; t++)
{
string a = mainStatus[i][t];
fscanf(continueGame, "%s ", newStr[i][t].c_str());
}
}
fclose(continueGame);
}
}
//defining every function here
string int_to_char(int a)
{
char sx[2];
sprintf(sx, "%d", a);
return sx;
}
int char_to_int(char a)
{
string b = to_string(a);
stringstream to_int(b);
int x = 0;
to_int >> x;
return x;
}
//check if the slot we want to move belongs to the actual user;
string get_latter_from_grapth(string a)
{
string x = a;
char oneOnly = x.at(1);
string s;
s.push_back(oneOnly);
return s;
}
char get_first_letter(char slot[])
{
//get the first letter of the slot
return slot[0];
}
int get_number(char slot[])
{
// return the number after the letter eg. A1 ==> 1
return slot[1] - 48;
}
int letter_to_number(char a)
{
return a - 0x41;
}
string to_string(char x)
{
char oneOnly = x;
string s;
s.push_back(oneOnly);
return s;
}
char string_to_char(string a, int position)
{
int n = a.length();
char char_array[n + 1];
strcpy(char_array, a.c_str());
return char_array[position];
}
//choose the player that is going to start the game
void startPlayer(int *start_player)
{
int a, b;
printf("X PLAYER THROW THE DICE (any number between 1-6)\n");
scanf("%d", &a);
printf("Y PLAYER THROW THE DICE (any number between 1-6)\n");
scanf("%d", &b);
log(a, b);
//******************I HAVE TO WRITE IT IN THE FILE log.dat
srand(time(0));
int dice1 = (rand() % 6 + 1);
int dice2 = (rand() % 6 + 1);
if (dice1 > dice2)
{
printf("X PLAYER IS GOIND TO START THE GAME!\n");
*start_player = 1; //means its X
}
else
{
printf("Y PLAYER IS GOIND TO START THE GAME!\n");
*start_player = 2; //means its Y
}
}
void firstPlayer(string a[3][12], int start_player, Player &x, Player &y)
{
x.name = 'X';
y.name = 'Y';
string comp = a[1][8];
if(string_to_char(comp,0)=='X'){
x.first = true;
}else if (string_to_char(comp,0)=='Y'){
y.first = true;
}
else{
if (start_player == 1)
{
x.first = true;
}
else
{
y.first = true;
}
}
}
bool same_user(string a[3][12], int entered_slot_int, int slot_position, Player p)
{
bool permit = false;
if (entered_slot_int == 1)
{
string t;
t.push_back(p.name);
if (get_latter_from_grapth(a[0][slot_position]) == t)
{
permit = true;
}
}
else if (entered_slot_int == 5)
{
string t;
t.push_back(p.name);
if (get_latter_from_grapth(a[2][slot_position]) == t)
{
permit = true;
}
}
else
{
printf("YOU ARE ONLY ALLOWED TO MOVE VALID SLOTS\n");
}
return permit;
}
bool moviment_allowed(string a[3][12], int dice1, int dice2, char slot1[], char slot2[], Player p1)
{
bool go = false;
char entered_slot1_char = get_first_letter(slot1); //get the first latter
char entered_slot2_char = get_first_letter(slot2);
int entered_slot1_int = get_number(slot1); // Will help us to get the row E´1' means the first row
int entered_slot2_int = get_number(slot2);
int firstSlotPosition = letter_to_number(entered_slot1_char); // return the equivalent number. if A ==> 0
int secondSlotPosition = letter_to_number(entered_slot2_char);
bool permitSlot1 = same_user(a, entered_slot1_int, firstSlotPosition, p1);
bool permitSlot2 = same_user(a, entered_slot2_int, secondSlotPosition, p1);
if (permitSlot1 && permitSlot2)
{
go = true;
}
return go;
}
void log(int a, int b)
{
FILE *log;
if ((log = fopen("log.dat", "a")) == NULL)
{
printf("LOG.dat file not found \n");
}
else
{
fprintf(log, "%d\n%d\n\n", a, b);
fclose(log);
}
}
void log(char c, int a, int b)
{
FILE *log;
if ((log = fopen("log.dat", "a")) == NULL)
{
printf("LOG.dat file not found \n");
}
else
{
fprintf(log, "%c %d %d\n", c, a, b);
fclose(log);
}
}
void play(string str[3][12], Player player1, Player player2)
{
if (player1.first == true)
{
while (1 == 1)
{
int a, b;
char slot1[2], slot2[2];
printf("%c PLAYER THROW DICE1 and DICE2: NUMBER BETWEEN 1-6 (0 to finish or save for future play)\n", player1.name);
scanf("%d%d", &a, &b);
while ((a != 0 && a > 6) || (b != 0 && b > 6))
{
printf("INVALID NUMBERS, ENTER NUMBER BETWEEN 1-6\n");
scanf("%d%d", &a, &b);
}
if (a == 0 || b == 0)
{
printf("PRESS\n1==>END THE GAME\n2==>SAVE FOR FUTURE PLAY!\n");
int g;
scanf("%d", &g);
switch (g)
{
case 1:
{
printf("GAME ENDED BY THE USER!\n");
//clean the grapth
string array[3][12] = {};
for (int i = 0; i < 3; i++)
{
for (int t = 0; t < 12; t++)
{
array[i][t] = "0";
}
}
DiceInFile(array, 0, 0);
}
break;
case 2:
{
DiceInFile(str, fromFileX, fromFileY);
printf("GAME WAS SAVED SUCCESSFULLY, BYE BYE!\n");
}
break;
}
break;
}
else
{
if (checkFlag(str, 'X', fromFileX, fromFileY))
{
pick_flakes(str, 0, &fromFileX, &fromFileY);
}
else
{
//write the information in the file log.txt
log(player1.name, a, b);
dice_in_grapth(str, a, b);
initialState(str);
//CHECK IF IS A SUPER PLAYER
if (a == b)
{
printf("\n DICE1= %d \n DICE2= %d\n", a, b);
printf("ENTER THE SLOT THAT WILL BE MOVED TWICE: CAPITAL LETTERS A1, B..\n");
scanf("%s", slot1);
if (slot1[0] >= 'a' && slot1[0] <= 'l')
{
slot1[0] = slot1[0] - 32;
}
bool result = moviment_allowed(mainStatus, a, b, slot1, slot1, player1);
while (result != 1)
{
printf("THE ENTERED SLOT ARE NOT ALLOWED TO MOVED!\n");
printf("ENTER VALID SLOT\n");
scanf("%s", slot1);
result = moviment_allowed(mainStatus, a, b, slot1, slot1, player1);
}
initialState(str);
move_x(str, a, slot1); //move the slot one twice
move_x(str, a, slot1); //
str[1][8] = "Y";
initialState(str);
}
else
{
printf("\n DICE1= %d \n DICE2= %d\n", a, b);
printf("ENTER THE DESIRED SLOTS: CAPITAL LETTERS A1, B...\n");
scanf("%s%s", slot1, slot2);
bool result = moviment_allowed(mainStatus, a, b, slot1, slot2, player1);
while (result != 1)
{
printf("THE ENTERED SLOTS ARE NOT ALLOWED TO MOVED!\n");
printf("ENTER VALID SLOTS\n");
scanf("%s%s", slot1, slot2);
result = moviment_allowed(mainStatus, a, b, slot1, slot2, player1);
}
initialState(str);
move_x(str, a, slot1); //move the slot1
move_x(str, b, slot2); // move slot2
str[1][8] = "Y";
initialState(str);
}
}
}
//next player
printf("%c PLAYER THROW DICE1 and DICE2: NUMBER BETWEEN 1-6 (0 to finish or save the for future play)\n", player2.name);
scanf("%d%d", &a, &b);
while ((a != 0 && a > 6) || (b != 0 && b > 6))
{
printf("INVALID NUMBERS, ENTER NUMBER BETWEEN 1-6\n");
scanf("%d%d", &a, &b);
}
if (a == 0 || b == 0)
{
printf("PRESS\n1==>END THE GAME\n2==>SAVE FOR FUTURE PLAY!\n");
int g;
scanf("%d", &g);
switch (g)
{
case 1:
{
printf("GAME ENDED BY THE USER!\n");
//clean the grapth
string array[3][12] = {};
for (int i = 0; i < 3; i++)
{
for (int t = 0; t < 12; t++)
{
array[i][t] = "0";
}
}
DiceInFile(array, 0, 0);
}
break;
case 2:
{
DiceInFile(str, fromFileX, fromFileY);
printf("GAME WAS SAVED SUCCESSFULLY, BYE BYE!\n");
}
break;
}
break;
}
else
{
if (checkFlag(str, 'Y', fromFileX, fromFileY))
{
pick_flakes(str, 0, &fromFileX, &fromFileY);
}
else
{
log(player2.name, a, b);
dice_in_grapth(str, a, b);
initialState(str);
//CHECK IF IS A SUPER PLAYER
if (a == b)
{
printf("\n DICE1= %d \n DICE2= %d\n", a, b);
printf("ENTER THE SLOT THAT WILL BE MOVED TWICE: CAPITAL LETTERS A1, B...\n");
scanf("%s", slot1);
bool result = moviment_allowed(mainStatus, a, b, slot1, slot1, player2);
while (result != 1)
{
printf("THE ENTERED SLOT ARE NOT ALLOWED TO MOVED!\n");
printf("ENTER VALID SLOT\n");
scanf("%s", slot1);
result = moviment_allowed(mainStatus, a, b, slot1, slot1, player2);
}
initialState(str);
move_y(str, a, slot1); //move the slot one twice
move_y(str, a, slot1); //
str[1][8] = "X";
initialState(str);
}
else
{
printf("DICE1= %d \n DICE2= %d\n", a, b);
printf("ENTER THE DESIRED SLOTS: CAPITAL LETTERS A1, B...\n");
scanf("%s%s", slot1, slot2);
bool result = moviment_allowed(mainStatus, a, b, slot1, slot2, player2);
while (result != 1)
{
printf("THE ENTERED SLOTS ARE NOT ALLOWED TO MOVED!\n ");
printf("ENTER VALID SLOTS\n");
scanf("%s%s", slot1, slot2);
result = moviment_allowed(mainStatus, a, b, slot1, slot2, player2);
}
initialState(str);
move_y(str, a, slot1); //move the slot1
move_y(str, b, slot2); // move slot2
str[1][8] = "X";
initialState(str);
}
}
}
}
}
else if (player2.first == true)
{
while (1 == 1)
{
int a, b;
char slot1[2], slot2[2];
printf("%c PLAYER THROW DICE1 and DICE2: NUMBER BETWEEN 1-6 (0 to finish or save for future play)\n", player2.name);
scanf("%d%d", &a, &b);
while ((a != 0 && a > 6) || (b != 0 && b > 6))
{
printf("INVALID NUMBERS, ENTER NUMBER BETWEEN 1-6\n");
scanf("%d%d", &a, &b);
}
if (a == 0 || b == 0)
{
printf("PRESS\n1==>END THE GAME\n2==>SAVE FOR FUTURE PLAY!\n");
int g;
scanf("%d", &g);
switch (g)
{
case 1:
{
printf("GAME ENDED BY THE USER!\n");
string array[3][12] = {};
for (int i = 0; i < 3; i++)
{
for (int t = 0; t < 12; t++)
{
array[i][t] = "0";
}
}
DiceInFile(array, 0, 0);
}
break;
case 2:
{
DiceInFile(str, fromFileX, fromFileY);
printf("GAME WAS SAVED SUCCESSFULLY, BYE BYE!\n");
}
break;
}
break;
}
else
{
if (checkFlag(str, 'Y', fromFileX, fromFileY))
{
pick_flakes(str, 0, &fromFileX, &fromFileY);
}
else
{
log(player2.name, a, b);
dice_in_grapth(str, a, b);
initialState(str);
//CHECK IF IS A SUPER PLAYER
if (a == b)
{
printf("\n DICE1= %d \n DICE2= %d\n", a, b);
printf("ENTER THE SLOT THAT WILL BE MOVED TWICE: CAPITAL LETTERS A1, B...\n");
scanf("%s", slot1);
bool result = moviment_allowed(mainStatus, a, b, slot1, slot1, player2);
while (result != 1)
{
printf("THE ENTERED SLOT ARE NOT ALLOWED TO MOVED!\n");
printf("ENTER VALID SLOT\n");
scanf("%s", slot1);
result = moviment_allowed(mainStatus, a, b, slot1, slot1, player2);
}
initialState(str);
move_y(str, a, slot1); //move the slot one twice
move_y(str, a, slot1); //
str[1][8] = "X";
initialState(str);
}
else
{
printf("DICE1= %d \n DICE2= %d\n", a, b);
printf("ENTER THE DESIRED SLOTS: CAPITAL LETTERS A1, B...\n");
scanf("%s%s", slot1, slot2);
bool result = moviment_allowed(mainStatus, a, b, slot1, slot2, player2);
while (result != 1)
{
printf("THE ENTERED SLOTS ARE NOT ALLOWED TO MOVED!\n ");
printf("ENTER VALID SLOTS\n");
scanf("%s%s", slot1, slot2);
result = moviment_allowed(mainStatus, a, b, slot1, slot2, player2);
}
initialState(str);
move_y(str, a, slot1); //move the slot1
move_y(str, b, slot2); // move slot2
str[1][8] = "X";
initialState(str);
}
}
}
//for the next user
printf("%c PLAYER THROW DICE1 and DICE2: NUMBER BETWEEN 1-6 (0 to finish or save for future play)\n", player1.name);
scanf("%d%d", &a, &b);
while ((a != 0 && a > 6) || (b != 0 && b > 6))
{
printf("INVALID NUMBERS, ENTER NUMBER BETWEEN 1-6\n");
scanf("%d%d", &a, &b);
}
if (a == 0 || b == 0)
{
printf("PRESS\n1==>END THE GAME\n2==>SAVE FOR FUTURE PLAY!\n");
int g;
scanf("%d", &g);
switch (g)
{
case 1:
{
printf("GAME ENDED BY THE USER!\n");
//clean the grapth
string array[3][12] = {};
for (int i = 0; i < 3; i++)
{
for (int t = 0; t < 12; t++)
{
array[i][t] = "0";
}
}
DiceInFile(array, 0, 0);
}
break;
case 2:
{
DiceInFile(str, fromFileX, fromFileY);
printf("GAME WAS SAVED SUCCESSFULLY, BYE BYE!\n");
}
break;
}
break;
}
else
{
if (checkFlag(str, 'X', fromFileX, fromFileY))
{
pick_flakes(str, 0, &fromFileX, &fromFileY);
}
else
{
log(player1.name, a, b);
dice_in_grapth(str, a, b);
initialState(str);
//CHECK IF IS A SUPER PLAYER
if (a == b)
{
printf("\n DICE1= %d \n DICE2= %d\n", a, b);
printf("ENTER THE SLOT THAT WILL BE MOVED TWICE: CAPITAL LETTERS A1, B..\n");
scanf("%s", slot1);
if (slot1[0] >= 'a' && slot1[0] <= 'l')
{
slot1[0] = slot1[0] - 32;
}
bool result = moviment_allowed(mainStatus, a, b, slot1, slot1, player1);
while (result != 1)
{
printf("THE ENTERED SLOT ARE NOT ALLOWED TO MOVED!\n");
printf("ENTER VALID SLOT\n");
scanf("%s", slot1);
result = moviment_allowed(mainStatus, a, b, slot1, slot1, player1);
}
initialState(str);
move_x(str, a, slot1); //move the slot one twice
move_x(str, a, slot1); //
str[1][8] = "Y";
initialState(str);
}
else
{
printf("\n DICE1= %d \n DICE2= %d\n", a, b);
printf("ENTER THE DESIRED SLOTS: CAPITAL LETTERS A1, B...\n");
scanf("%s%s", slot1, slot2);
bool result = moviment_allowed(mainStatus, a, b, slot1, slot2, player1);
while (result != 1)
{
printf("THE ENTERED SLOTS ARE NOT ALLOWED TO MOVED!\n");
printf("ENTER VALID SLOTS\n");
scanf("%s%s", slot1, slot2);
result = moviment_allowed(mainStatus, a, b, slot1, slot2, player1);
}
initialState(str);
move_x(str, a, slot1); //move the slot1
move_x(str, b, slot2); // move slot2
str[1][8] = "Y";
initialState(str);
}
}
}
}
}
else
{
printf("Something is wrong, conditions not satisfying \n");
}
}
//add the dice values to the grapth
void dice_in_grapth(string a[3][12], int dice1, int dice2)
{
string sdice1 = to_string(dice1);
string sdice2 = to_string(dice2);
a[1][5] = sdice1 + " ";
a[1][6] = sdice2 + " ";
}
//add the broken dices for x and for y
void broken_dices(string a[3][12], char c)
{
string broken = "";
if (c == 'X')
{
broken = a[1][4];
}
else
{
broken = a[1][7];
}
stringstream to_int(broken);
int x = 0;
to_int >> x;
x += 1;
char sx[2];
itoa(x, sx, 10);
if (c == 'X')
{
a[1][4] = sx;
}
else
{
a[1][7] = sx;
}
}
void move_x(string mainStatus[3][12], int dice, char slot[])
{
int res = char_to_int(slot[1]);
if (res == 1)
{
int col = letter_to_number(slot[0]);
int row = 0;
int next_col = dice + col;
string value1 = mainStatus[0][next_col];
if (value1 == "0")
{
mainStatus[0][next_col] = (int_to_char(1) + "X");
//decrement
string actualValue = mainStatus[0][col];
char decrement = actualValue.at(0);
int decrementInt = char_to_int(decrement);
decrementInt = decrementInt - 1;
mainStatus[0][col] = (int_to_char(decrementInt) + "X");
string after = mainStatus[0][col];
printf("SLOT MOVED SUCCESSFULLY\n");
}
else if (get_latter_from_grapth(mainStatus[0][next_col]) == "X")
{
string nextValue = mainStatus[0][next_col];
char onlyTheNumber = nextValue.at(0);
int onlyNumberInt = char_to_int(onlyTheNumber);
if (onlyNumberInt < 6)
{
onlyNumberInt = onlyNumberInt + 1;
mainStatus[0][next_col] = (int_to_char(onlyNumberInt) + "X");
string place = mainStatus[0][next_col];
//decrement
string actualValue = mainStatus[0][col];
char decrement = actualValue.at(0);
int decrementInt = char_to_int(decrement);
decrementInt = decrementInt - 1;
mainStatus[0][col] = (int_to_char(decrementInt) + "X");
string after = mainStatus[0][col];
}
else
{
printf("IMPOSSIBLE TO MOVE THIS SLOT, WE ALREADY HAVE 5 ELEMENTS IN THAT POSITION\n");
}
}
else if (get_latter_from_grapth(mainStatus[0][next_col]) != "X")
{
string nextPos = mainStatus[0][next_col];
char nextNumber = nextPos.at(0);
int nextInt = char_to_int(nextNumber);
if (nextInt == 1)
{
broken_dices(mainStatus, 'Y');
mainStatus[0][next_col] = (int_to_char(1) + "X");
//decrement
string actualValue = mainStatus[0][col];
char decrement = actualValue.at(0);
int decrementInt = char_to_int(decrement);
decrementInt = decrementInt - 1;
mainStatus[0][col] = (int_to_char(decrementInt) + "X");
string after = mainStatus[0][col];
}
else
{
printf("IMPOSSIBLE TO PLAY IN THIS SLOT");
}
}
}
else
{
//means the user entered something from line 5
int col = letter_to_number(slot[0]);
int reminder;
int next_col;
if (col > dice && col != dice)
{
next_col = col - dice;
}
else if (col == dice)
{
next_col = 0;
}
else
{
reminder = dice - col;
next_col = reminder;
}
if (reminder == 0)
{
string value1 = mainStatus[2][next_col];
if (value1 == "0")
{
mainStatus[2][next_col] = (int_to_char(1) + "X");
//decrement
string actualValue = mainStatus[2][col];
char decrement = actualValue.at(0);
int decrementInt = char_to_int(decrement);
decrementInt = decrementInt - 1;
mainStatus[2][col] = (int_to_char(decrementInt) + "X");
string after = mainStatus[2][col];
printf("SLOT MOVED SUCCESSFULLY\n");
}
else if (get_latter_from_grapth(mainStatus[2][next_col]) == "X")
{
string nextValue = mainStatus[2][next_col];
char onlyTheNumber = nextValue.at(0);
int onlyNumberInt = char_to_int(onlyTheNumber);
if (onlyNumberInt < 6)
{
onlyNumberInt = onlyNumberInt + 1;
mainStatus[2][next_col] = (int_to_char(onlyNumberInt) + "X");
string place = mainStatus[2][next_col];
//decrement
string actualValue = mainStatus[2][col];
char decrement = actualValue.at(0);
int decrementInt = char_to_int(decrement);
decrementInt = decrementInt - 1;
mainStatus[2][col] = (int_to_char(decrementInt) + "X");
string after = mainStatus[2][col];
printf("SLOT MOVED SUCCESSFULLY\n");
}
else
{
printf("IMPOSSIBLE TO MOVE THIS SLOT, WE ALREADY HAVE 5 ELEMENTS IN THAT POSITION\n");
}
}
else if (get_latter_from_grapth(mainStatus[2][next_col]) != "X")
{
string nextPos = mainStatus[2][next_col];
char nextNumber = nextPos.at(0);
int nextInt = char_to_int(nextNumber);
if (nextInt == 1)
{
broken_dices(mainStatus, 'Y');
mainStatus[2][next_col] = (int_to_char(1) + "X");
//decrement
string actualValue = mainStatus[2][col];
char decrement = actualValue.at(0);
int decrementInt = char_to_int(decrement);
decrementInt = decrementInt - 1;
mainStatus[2][col] = (int_to_char(decrementInt) + "X");
string after = mainStatus[2][col];
}
else
{
printf("IMPOSSIBLE TO PLAY IN THIS SLOT");
}
}