-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrabble.cpp
More file actions
1325 lines (1257 loc) · 56.6 KB
/
Copy pathscrabble.cpp
File metadata and controls
1325 lines (1257 loc) · 56.6 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 <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <Windows.h>
using namespace std;
class Tile {
public:
char letter;
int value;
Tile(char x, int y) {
letter = x;
value = y;
}
Tile() {
letter = 'a';
value = 0;
}
};
class TileBag {
public:
vector<Tile>tiles;
int amount[26] = { 9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1 };
int vals[26] = { 1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 };
TileBag() {
for (int i = 0;i < 26;i++) {
for (int j = 0;j < amount[i];j++) {
tiles.push_back(Tile(i + 'A', vals[i]));
}
}
}
Tile takeTile() {
int gaming = rand() % tiles.size() + 0;
Tile newTile = tiles[gaming];
tiles.erase(tiles.begin() + gaming);
return newTile;
}
void returnTile(Tile oldTile) {
tiles.push_back(oldTile);
}
};
void menu();
void boardGen(char board[][15]);
void boardPrint(char board[][15]);
int numCheck(int ash);
void gameplay();
void rules();
void dictionary();
void turns(int turnCounter, char hand[], int handVals[], char p2Hand[], int p2HandVals[], string names[], char board[][15], TileBag& bag, bool boolHand[], bool boolHand2[], bool& isP2, int& score1, int& score2, bool& noWords);
void handGen(char hand[], int handVals[], char p2Hand[], int p2HandVals[], bool boolHand[], bool boolHand2[], TileBag& bag);
void playWord(bool& noWords, bool& isP2, char hand[], int handVals[], char p2Hand[], int p2HandVals[], char board[][15], bool boolHand[], bool boolHand2[], int& score1, int& score2);
bool wordCheck(string givenWord, bool& valid);
int assignChar(char newLetter, int& newVal);
void wordAdd(char board[][15], char hand[], int handVals[], char p2Hand[], int p2HandVals[], bool& noWords, string word, bool& isP2, int wordPoints[], int& score1, int& score2, int spaceVal, char newLetter);
void exchange(char hand[], int handVals[], char p2Hand[], int p2HandVals[], bool boolHand[], bool boolHand2[], bool& isP2, TileBag& bag);
void viewScore(int& score1, int& score2, string names[]);
void scoring(int wordPoints[], int& multiplier, int& score1, int& score2, bool& isP2, string word);
void lastTurns(char hand[], int handVals[], char p2Hand[], int p2HandVals[], string names[], char board[][15], TileBag& bag, bool boolHand[], bool boolHand2[], bool& isP2, int& score1, int& score2, bool& noWords);
// declaring functions
char normal[] = { 0x1b, '[', '0', ';', '3', '9', 'm', 0 };
char black[] = { 0x1b, '[', '0', ';', '3', '0', 'm', 0 };
char red[] = { 0x1b, '[', '0', ';', '3', '1', 'm', 0 };
char green[] = { 0x1b, '[', '0', ';', '3', '2', 'm', 0 };
char yellow[] = { 0x1b, '[', '0', ';', '3', '3', 'm', 0 };
char blue[] = { 0x1b, '[', '0', ';', '3', '4', 'm', 0 };
char purple[] = { 0x1b, '[', '0', ';', '3', '5', 'm', 0 };
char cyan[] = { 0x1b, '[', '0', ';', '3', '6', 'm', 0 };
char Lgray[] = { 0x1b, '[', '0', ';', '3', '7', 'm', 0 };
char Bred[] = { 0x1b, '[', '1', ';', '3', '1', 'm', 0 };
// colors
int main() {
int menuCase = 0;
while (menuCase != 3) {
menu();
menuCase = numCheck(menuCase);
system("cls");
switch (menuCase) {
case 1: {
// case to start the game
gameplay();
break;
}
case 2: {
// case to print out the instructions for the game
rules();
break;
}
case 3: {
// case to end the program
cout << "Goodbye!" << endl;
return 0;
}
}
}
}
void menu() {
cout << R"( _ __ __ __ ____________ ___ ___ ___ __ ______
| | /| / /__ / /______ __ _ ___ / /____ / __/ ___/ _ \/ _ | / _ )/ _ )/ / / __/ /
| |/ |/ / -_) / __/ _ \/ ' \/ -_) / __/ _ \ _\ \/ /__/ , _/ __ |/ _ / _ / /__/ _//_/
|__/|__/\__/_/\__/\___/_/_/_/\__/ \__/\___/ /___/\___/_/|_/_/ |_/____/____/____/___(_)
Would you like to:
[1] Start the game!
[2] View the rules!
[3] Exit the program
)";
}
void gameplay() {
TileBag bag;
string names[2];
int handVals[7];
char hand[7];
int p2HandVals[7];
char p2Hand[7];
char board[15][15];
int turnCounter = 1;
bool boolHand[7] = { false, false, false, false, false, false, false };
bool boolHand2[7] = { false, false, false, false, false, false, false };
bool isP2 = false;
int score1 = 0;
int score2 = 0;
bool noWords = true;
// initializing variables
cout << "So it begins. Player one, what is your name?" << endl;
cin.clear();
getline(cin >> ws, names[0]);
system("cls");
Sleep(1000);
cout << "Now, Player 2, please input your name." << endl;
cin.clear();
getline(cin >> ws, names[1]);
system("cls");
// inputting names
turns(turnCounter, hand, handVals, p2Hand, p2HandVals, names, board, bag, boolHand, boolHand2, isP2, score1, score2, noWords); // go play the game(it begins)
lastTurns(hand, handVals, p2Hand, p2HandVals, names, board, bag, boolHand, boolHand2, isP2, score1, score2, noWords); // game ending, happens either on forcequit or on no more tiles being in the bag
}
void turns(int turnCounter, char hand[], int handVals[], char p2Hand[], int p2HandVals[], string names[], char board[][15], TileBag& bag, bool boolHand[], bool boolHand2[], bool& isP2, int& score1, int& score2, bool& noWords) {
int turnOption = 0;
bool isFirstTurn = true;
// initializing variables
while (bag.tiles.size() == 0 || turnOption != 99) {
// loop that will run until there are either no more tiles in the tile bag or until someone force quits out of the game as a result of no more turns
if (isP2 == false) {
// player 1 menu
cout << R"(
___ __ ___ __ __
/ _ \/ /__ ___ _____ ____ < / / /___ _________ / /
/ ___/ / _ `/ // / -_) __/ / / / __/ // / __/ _ \/_/
/_/ /_/\_,_/\_, /\__/_/ /_/ \__/\_,_/_/ /_//_(_)
/___/
)";
cout << names[0] << ", it is your turn!" << " It is turn " << turnCounter << "." << endl;
}
else {
// player 2 menu
cout << R"(
___ __ ___ __ __
/ _ \/ /__ ___ _____ ____ |_ | / /___ _________ / /
/ ___/ / _ `/ // / -_) __/ / __/ / __/ // / __/ _ \/_/
/_/ /_/\_,_/\_, /\__/_/ /____/ \__/\_,_/_/ /_//_(_)
/___/
)";
cout << names[1] << ", it is your turn!" << " It is turn " << turnCounter << "." << endl;
}
if (noWords == true) {
// reminds the user that on the first turn a word can only be played in the middle of the board
cout << "Please remember that there are no words on the board yet, so whatever word you play must cross the middle." << endl;
}
else {
cout << "Remember that all words must intersect!" << endl;
}
cout << "\n\n";
handGen(hand, handVals, p2Hand, p2HandVals, boolHand, boolHand2, bag); // generating hands for both users
if (turnCounter == 1) {
boardGen(board); // generating the board on its first print
}
else {
boardPrint(board); // printing the board
}
cout << "\n\n";
cout << "Here is your hand:" << endl;
if (isP2 == false) {
// showing player 1's hand
for (int i = 0;i < 7;i++) {
cout << hand[i] << "(" << handVals[i] << ")" << " ";
}
}
else {
// showing player 2's hand
for (int j = 0;j < 7;j++) {
cout << p2Hand[j] << "(" << p2HandVals[j] << ")" << " ";
}
}
cout << endl;
// switch for the options on any given turn
cout << "\nYou can now: \n[1] Play a word\n[2] Exchange tiles\n[3] Check if a word is valid\n[4] Review the instructions\n[5] View scores\n[6] Pass your turn\n";
if (noWords == false && turnCounter > 2) {
cout << "[99] Force quit out of the game\n\n";
}
turnOption = numCheck(turnOption);
system("cls");
switch (turnOption) {
case 1: {
// case to play a word
playWord(noWords, isP2, hand, handVals, p2Hand, p2HandVals, board, boolHand, boolHand2, score1, score2);
// do the player swap inside of the function
turnCounter++;
break;
}
case 2: {
// case to exchange tiles
exchange(hand, handVals, p2Hand, p2HandVals, boolHand, boolHand2, isP2, bag);
turnCounter++;
break;
}
case 3: {
// case to check if a word is valid
dictionary();
break;
}
case 4: {
// rules again
rules();
break;
}
case 5: {
// scoring
viewScore(score1, score2, names);
break;
}
case 6: {
//pass
if (isP2 == false) {
isP2 = true;
}
else {
isP2 = false;
}
turnCounter++;
break;
}
case 99: {
// force quitting
if (noWords == true && turnCounter < 3) {
turnOption = 0;
cout << "You can't do that yet!" << endl; // there should be at least 1 word on the board and both players should have taken a turn for a game to be force quit
break;
}
else {
break;
}
}
default: {
// in the case of a wrong input
cout << "Invalid." << endl;
break;
}
}
}
}
void lastTurns(char hand[], int handVals[], char p2Hand[], int p2HandVals[], string names[], char board[][15], TileBag& bag, bool boolHand[], bool boolHand2[], bool& isP2, int& score1, int& score2, bool& noWords) {
char option;
int turnOption = 0;
system("cls");
cout << "We're in the endgame now! Each user gets 1 more turn and then the scores will be tallied to declare the winner." << endl;
cout << "Press a to continue" << endl;
cin >> option;
while (option != 'a') {
cout << "Please input a (lowercase)" << endl;
cin >> option;
}
system("cls");
for (int k = 0;k < 2;k++) {
if (isP2 == false) {
// player 1 menu
cout << R"(
___ __ ___ __ __
/ _ \/ /__ ___ _____ ____ < / / /___ _________ / /
/ ___/ / _ `/ // / -_) __/ / / / __/ // / __/ _ \/_/
/_/ /_/\_,_/\_, /\__/_/ /_/ \__/\_,_/_/ /_//_(_)
/___/
)";
cout << names[0] << ", it is your turn!" << endl;
}
else {
// player 2 menu
cout << R"(
___ __ ___ __ __
/ _ \/ /__ ___ _____ ____ |_ | / /___ _________ / /
/ ___/ / _ `/ // / -_) __/ / __/ / __/ // / __/ _ \/_/
/_/ /_/\_,_/\_, /\__/_/ /____/ \__/\_,_/_/ /_//_(_)
/___/
)";
cout << names[1] << ", it is your turn!" << " It is turn " << endl;
}
if (noWords == true) {
// reminds the user that on the first turn a word can only be played in the middle of the board
cout << "Please remember that there are no words on the board yet, so whatever word you play must cross the middle." << endl;
}
else {
cout << "Remember that all words must intersect!" << endl;
}
handGen(hand, handVals, p2Hand, p2HandVals, boolHand, boolHand2, bag);
boardPrint(board);
cout << "\n\n";
cout << "Here is your hand:" << endl;
if (isP2 == false) {
// showing player 1's hand
for (int i = 0;i < 7;i++) {
cout << hand[i] << "(" << handVals[i] << ")" << " ";
}
}
else {
// showing player 2's hand
for (int j = 0;j < 7;j++) {
cout << p2Hand[j] << "(" << p2HandVals[j] << ")" << " ";
}
}
cout << "\nYou can now: \n[1] Play a word\n[2] Check if a word is valid\n[3] Pass your turn\n\n";
turnOption = numCheck(turnOption);
system("cls");
switch (turnOption) {
case 1: {
// case to play a word
playWord(noWords, isP2, hand, handVals, p2Hand, p2HandVals, board, boolHand, boolHand2, score1, score2);
// do the player swap inside of the function
break;
}
case 2: {
// case to check if a word is valid
dictionary();
break;
}
case 3: {
//pass
if (isP2 == false) {
isP2 = true;
}
else {
isP2 = false;
}
break;
}
default: {
// in the case of a wrong input
cout << "Invalid." << endl;
break;
}
}
}
system("cls");
// assigning the winner of the match
if (score1 == score2) {
cout << R"( ______ _ __ _ __
/ _/ /( )___ ___ _ / /_(_)__ / /
_/ // __//(_-< / _ `/ / __/ / -_)_/
/___/\__/ /___/ \_,_/ \__/_/\__(_)
)";
}
else if (score1 > score2) {
cout << R"( ___ __ ___ _ __
/ _ \/ /__ ___ _____ ____ < / _ __(_)__ ___ / /
/ ___/ / _ `/ // / -_) __/ / / | |/|/ / / _ \(_-</_/
/_/ /_/\_,_/\_, /\__/_/ /_/ |__,__/_/_//_/___(_)
/___/
)";
}
else {
cout << R"( ___ __ ___ _ __
/ _ \/ /__ ___ _____ ____ |_ | _ __(_)__ ___ / /
/ ___/ / _ `/ // / -_) __/ / __/ | |/|/ / / _ \(_-</_/
/_/ /_/\_,_/\_, /\__/_/ /____/ |__,__/_/_//_/___(_)
/___/ )";
}
cout << "\n\n";
cout << "The score was " << score1 << " - " << score2 << "!" << endl;
cout << "Press a to go back to the main menu" << endl; // go back to the main menu
cin >> option;
while (option != 'a') {
cout << "Please input a (lowercase)" << endl;
cin >> option;
}
system("cls");
}
void playWord(bool& noWords, bool& isP2, char hand[], int handVals[], char p2Hand[], int p2HandVals[], char board[][15], bool boolHand[], bool boolHand2[], int& score1, int& score2) {
string word;
int badCounter = 0;
int wordPoints[7] = { 0, 0, 0, 0, 0, 0, 0 };
bool valid = false;
bool valid2 = false;
bool valid3 = false;
int spaceVal = 0;
char newLetter = ' ';
int newVal = 0;
int spaceCount = 0;
// initializing variables
cout << "Below is the board and your hand. What word would you like to play." << endl;
if (noWords == true) {
// reminding the user once again that if there are no words on the board the word must cross the middle
cout << "There are no words on the board, so your word must cross the middle" << endl;
valid3 = true;
}
else {
// reminding the user about intersection rules
cout << "Please put a 0 where you would like the word to intersect" << endl;
}
cout << "Remember to always input the word in UPPERCASE!" << endl;
boardPrint(board); // prints the board
cout << "\n\n";
if (isP2 == false) {
for (int i = 0;i < 7;i++) {
cout << hand[i] << "(" << handVals[i] << ")" << " ";
}
}
else {
for (int j = 0;j < 7;j++) {
cout << p2Hand[j] << "(" << p2HandVals[j] << ")" << " ";
}
}
// showing hands
cout << "\n\n";
while (valid == false) {
cout << "Please input your word here: " << endl;
cin.clear();
getline(cin >> ws, word);
// input a word
while (word.length() > 7 || word.length() < 2) {
// in case if the word has the wrong length
cout << "You cannot make a word that long!" << endl;
cin.clear();
getline(cin >> ws, word);
}
if (isP2 == false) {
// checks through all of the first player's tiles to see if they have all the tiles required
for (int k = 0;k < word.length();k++) {
badCounter = 0;
for (int l = 0;l < 7;l++) {
if (noWords == false) {
if (word[k] == '0') {
// assgin a value to the intersecting placeholder
cout << "What character do you intend to use in this space?";
cin.clear();
cin >> newLetter;
spaceVal = k;
word[k] = newLetter;
assignChar(newLetter, newVal);
wordPoints[k] = newVal;
valid3 = true;
spaceCount++;
continue;
}
}
if (word[k] != hand[l] || (word[k] == hand[l] && boolHand[l] == false)) {
badCounter++;
}
else {
// flags all the correct tiles to be overwritten and stores the int value of the character
boolHand[l] = false;
wordPoints[l] = handVals[l];
break;
}
}
if (badCounter == 7) {
// in case if the correct tile is not present
cout << "Invalid word at index " << k << endl;
for (int m = 0;m < 7;m++) {
boolHand[m] = true;
}
break;
}
}
}
else {
// checks through all of the second player's tiles to see if they have all the tiles required
for (int k = 0;k < word.length();k++) {
badCounter = 0;
for (int l = 0;l < 7;l++) {
if (noWords == false) {
if (word[k] == '0') {
// assign a value to the intersecting placeholder
cout << "What character do you intend to use in this space?";
cin.clear();
cin >> newLetter;
spaceVal = k;
word[k] = newLetter;
assignChar(newLetter, newVal);
wordPoints[k] = newVal;
valid3 = true;
spaceCount++;
continue;
}
}
if (word[k] != p2Hand[l] || (word[k] == p2Hand[l] && boolHand2[l] == false)) {
badCounter++;
}
else {
// flags all the correct tiles to be overwritten and stores the int value of the character
boolHand2[l] = false;
wordPoints[l] = p2HandVals[l];
break;
}
}
if (badCounter == 7) {
// in case if the correct tile is not present
cout << "Invalid word at index " << k << endl;
for (int m = 0;m < 7;m++) {
boolHand2[m] = true;
}
break;
}
}
}
if (badCounter == 7) {
// restarts the loop if invalid
continue;
}
if (valid3 == false) {
// restarts if there are no spaces
cout << "You can't intersect without the space! Go input again." << endl;
continue;
}
if (spaceCount > 1) {
// restarts if there is more than 1 space
cout << "In this version you unfortunately cannot intersect more than once. Please input again." << endl;
spaceCount = 0;
continue;
}
wordCheck(word, valid); // checks if the word is valid in the dictionary
if (valid == false) {
cout << "Invalid" << endl;
}
else {
break;
}
}
wordAdd(board, hand, handVals, p2Hand, p2HandVals, noWords, word, isP2, wordPoints, score1, score2, spaceVal, newLetter); // now add the word to the board
}
int assignChar(char newLetter, int& newVal) {
// assigning the intersect character value again, this could have been prevented if I used classes better. Happens.
if (newLetter == 'E' || newLetter == 'A' || newLetter == 'I' || newLetter == 'U' || newLetter == 'O' || newLetter == 'N' || newLetter == 'R' || newLetter == 'T' || newLetter == 'S' || newLetter == 'L') {
newVal = 1;
}
else if (newLetter == 'D' || newLetter == 'G') {
newVal = 2;
}
else if (newLetter == 'B' || newLetter == 'C' || newLetter == 'M' || newLetter == 'P') {
newVal = 3;
}
else if (newLetter == 'F' || newLetter == 'H' || newLetter == 'V' || newLetter == 'W' || newLetter == 'Y') {
newVal = 4;
}
else if (newLetter == 'K') {
newVal = 5;
}
else if (newLetter == 'X' || newLetter == 'J') {
newVal = 8;
}
else {
newVal = 10;
}
return newVal;
}
void wordAdd(char board[][15], char hand[], int handVals[], char p2Hand[], int p2HandVals[], bool& noWords, string word, bool& isP2, int wordPoints[], int& score1, int& score2, int spaceVal, char newLetter) {
int dirOp = 0;
bool validPlace = false;
int startPosX = 0;
int startPosY = 0;
int counter = 0;
int multiplier = 1;
char option;
// initializing variables
while (validPlace == false) {
cout << "Now input a coordinate to place the first letter of your word (input x axis and then y axis, goes from 0-14)" << endl;
startPosX = numCheck(startPosX);
startPosY = numCheck(startPosY);
// inputting coordinates for the word
cout << "Now, which direction will you put the piece?" << endl;
cout << "[1] Left\n[2] Right\n[3] Up\n[4] Down\n" << endl;
dirOp = numCheck(dirOp);
// swtich case for the direction of the string
switch (dirOp) {
case 1: {
// left
if (noWords == true) {
if (startPosX < 7 || startPosX >= 7 + word.length() || startPosY != 7) {
// if there are no words they must cross the middle
cout << "Please choose another direction or input another coordinate." << endl;
break;
}
}
else {
if (board[startPosY][startPosX - spaceVal] != newLetter) {
// in the case that the intersecting letters are not equal
cout << "Please choose another direction or input another coordinate" << endl;
break;
}
}
if (startPosX - word.length() < 0) {
// in case if the coordinate cannot be uesd
cout << "Please choose another direction or input another coordinate." << endl;
break;
}
else {
for (int l = startPosX;l > startPosX - word.length();l--) {
board[startPosY][l] = word[counter]; // overwrite the index of the board with your word
// checking for extra point tiles
if (noWords == false) {
if (l == startPosX - spaceVal) {
counter++; // if an intersecting tile is on a multiplier it cannot be used again
}
else {
if ((startPosY == 0 && (l == 0 || l == 7 || l == 14)) || (startPosY == 7 && (l == 0 || l == 14)) || (startPosY == 14 && (l == 0 || l == 7 || l == 14))) {
multiplier = 3;
}
else if (((startPosY == 0 || startPosY == 14) && (l == 3 || l == 11)) || ((startPosY == 2 || startPosY == 12) && (l == 6 || l == 8)) || ((startPosY == 3 || startPosY == 11) && (l == 0 || l == 7 || l == 14)) || ((startPosY == 6 || startPosY == 8) && (l == 2 || l == 6 || l == 8 || l == 12)) || (startPosY == 7 && (l == 3 || l == 11))) {
wordPoints[counter] *= 2;
}
else if ((startPosY == 1 || startPosY == 13) && (l == 5 || l == 9) || ((startPosY == 5 || startPosY == 9) && (l == 1 || l == 5 || l == 9 || l == 13))) {
wordPoints[counter] *= 3;
}
else if (startPosY == l || 14 - startPosY == l) {
multiplier = 2;
}
counter++; // output the next letter
}
}
else {
// I am so so sorry but it had to be done
// I could have made this a function but there are too many specifics and other more pressing things to worry about at the moment I am typing this comment (7:50PM)
if ((startPosY == 0 && (l == 0 || l == 7 || l == 14)) || (startPosY == 7 && (l == 0 || l == 14)) || (startPosY == 14 && (l == 0 || l == 7 || l == 14))) {
multiplier = 3;
}
else if (((startPosY == 0 || startPosY == 14) && (l == 3 || l == 11)) || ((startPosY == 2 || startPosY == 12) && (l == 6 || l == 8)) || ((startPosY == 3 || startPosY == 11) && (l == 0 || l == 7 || l == 14)) || ((startPosY == 6 || startPosY == 8) && (l == 2 || l == 6 || l == 8 || l == 12)) || (startPosY == 7 && (l == 3 || l == 11))) {
wordPoints[counter] *= 2;
}
else if ((startPosY == 1 || startPosY == 13) && (l == 5 || l == 9) || ((startPosY == 5 || startPosY == 9) && (l == 1 || l == 5 || l == 9 || l == 13))) {
wordPoints[counter] *= 3;
}
else if (startPosY == l || 14 - startPosY == l) {
multiplier = 2;
}
counter++; // output the next letter
}
}
validPlace = true; // that word was correct
noWords = false;
}
break;
}
case 2: {
// right
if (noWords == true) {
if (startPosX > 7 || startPosX <= 7 - word.length() || startPosY != 7) {
// in the case of a wrong coordinate
cout << "Please choose another direction or input another coordinate." << endl;
break;
}
}
else {
if (board[startPosY][startPosX + spaceVal] != newLetter) {
// in case the intersecting letter does not match
cout << "Please choose another direction or input another coordinate" << endl;
break;
}
}
if (startPosX + word.length() > 14) {
// we don't want to send this outside of the board
cout << "Please choose another direction or input another coordinate." << endl;
break;
}
else {
for (int l = startPosX;l < startPosX + word.length();l++) {
board[startPosY][l] = word[counter]; // overwrites the board
// checking for multilpliers
if (noWords == false) {
if (l == startPosX + spaceVal) {
counter++; // if an intersecting tile is on a multiplier it cannot be used again
}
else {
if ((startPosY == 0 && (l == 0 || l == 7 || l == 14)) || (startPosY == 7 && (l == 0 || l == 14)) || (startPosY == 14 && (l == 0 || l == 7 || l == 14))) {
multiplier = 3;
}
else if (((startPosY == 0 || startPosY == 14) && (l == 3 || l == 11)) || ((startPosY == 2 || startPosY == 12) && (l == 6 || l == 8)) || ((startPosY == 3 || startPosY == 11) && (l == 0 || l == 7 || l == 14)) || ((startPosY == 6 || startPosY == 8) && (l == 2 || l == 6 || l == 8 || l == 12)) || (startPosY == 7 && (l == 3 || l == 11))) {
wordPoints[counter] *= 2;
}
else if ((startPosY == 1 || startPosY == 13) && (l == 5 || l == 9) || ((startPosY == 5 || startPosY == 9) && (l == 1 || l == 5 || l == 9 || l == 13))) {
wordPoints[counter] *= 3;
}
else if (startPosY == l || 14 - startPosY == l) {
multiplier = 2;
}
counter++; // output the next letter
}
}
else {
if ((startPosY == 0 && (l == 0 || l == 7 || l == 14)) || (startPosY == 7 && (l == 0 || l == 14)) || (startPosY == 14 && (l == 0 || l == 7 || l == 14))) {
multiplier = 3;
}
else if (((startPosY == 0 || startPosY == 14) && (l == 3 || l == 11)) || ((startPosY == 2 || startPosY == 12) && (l == 6 || l == 8)) || ((startPosY == 3 || startPosY == 11) && (l == 0 || l == 7 || l == 14)) || ((startPosY == 6 || startPosY == 8) && (l == 2 || l == 6 || l == 8 || l == 12)) || (startPosY == 7 && (l == 3 || l == 11))) {
wordPoints[counter] *= 2;
}
else if ((startPosY == 1 || startPosY == 13) && (l == 5 || l == 9) || ((startPosY == 5 || startPosY == 9) && (l == 1 || l == 5 || l == 9 || l == 13))) {
wordPoints[counter] *= 3;
}
else if (startPosY == l || 14 - startPosY == l) {
multiplier = 2;
}
counter++; // output the next letter
}
}
validPlace = true; // the word is valid
noWords = false;
}
break;
}
case 3: {
// up
if (noWords == true) {
if (startPosY < 7 || startPosY >= 7 + word.length() || startPosX != 7) {
// in case of a wrong coordinate
cout << "Please choose another direction or input another coordinate." << endl;
break;
}
}
else {
if (board[startPosY - spaceVal][startPosX] != newLetter) {
// in the case of a wrong coordinate
cout << "Please choose another direction or input another coordinate" << endl;
break;
}
}
if (startPosY - word.length() < 0) {
// so that it does not go outside of the boundary
cout << "Please choose another direction or input another coordinate." << endl;
break;
}
else {
for (int k = startPosY;k > startPosY - word.length();k--) {
board[k][startPosX] = word[counter]; // overwrites the board with the chosen word
// checking for multiplier tiles
if (noWords == false) {
if (k == startPosY - spaceVal) {
counter++; // if an intersecting tile is on a multiplier it cannot be used again
}
else {
if ((k == 0 && (startPosX == 0 || startPosX == 7 || startPosX == 14)) || (k == 7 && (startPosX == 0 || startPosX == 14)) || (k == 14 && (startPosX == 0 || startPosX == 7 || startPosX == 14))) {
multiplier = 3;
}
else if (((k == 0 || k == 14) && (startPosX == 3 || startPosX == 11)) || ((k == 2 || k == 12) && (startPosX == 6 || startPosX == 8)) || ((k == 3 || k == 11) && (startPosX == 0 || startPosX == 7 || startPosX == 14)) || ((k == 6 || k == 8) && (startPosX == 2 || startPosX == 6 || startPosX == 8 || startPosX == 12)) || (k == 7 && (startPosX == 3 || startPosX == 11))) {
wordPoints[counter] *= 2;
}
else if ((k == 1 || k == 13) && (startPosX == 5 || startPosX == 9) || ((k == 5 || k == 9) && (startPosX == 1 || startPosX == 5 || startPosX == 9 || startPosX == 13))) {
wordPoints[counter] *= 3;
}
else if (k == startPosX || 14 - k == startPosX) {
multiplier = 2;
}
counter++;
}
}
else {
if ((k == 0 && (startPosX == 0 || startPosX == 7 || startPosX == 14)) || (k == 7 && (startPosX == 0 || startPosX == 14)) || (k == 14 && (startPosX == 0 || startPosX == 7 || startPosX == 14))) {
multiplier = 3;
}
else if (((k == 0 || k == 14) && (startPosX == 3 || startPosX == 11)) || ((k == 2 || k == 12) && (startPosX == 6 || startPosX == 8)) || ((k == 3 || k == 11) && (startPosX == 0 || startPosX == 7 || startPosX == 14)) || ((k == 6 || k == 8) && (startPosX == 2 || startPosX == 6 || startPosX == 8 || startPosX == 12)) || (k == 7 && (startPosX == 3 || startPosX == 11))) {
wordPoints[counter] *= 2;
}
else if ((k == 1 || k == 13) && (startPosX == 5 || startPosX == 9) || ((k == 5 || k == 9) && (startPosX == 1 || startPosX == 5 || startPosX == 9 || startPosX == 13))) {
wordPoints[counter] *= 3;
}
else if (k == startPosX || 14 - k == startPosX) {
multiplier = 2;
}
counter++;
}
}
validPlace = true; // word is valid
noWords = false;
}
break;
}
case 4: {
// down
if (noWords == true) {
if (startPosY > 7 || startPosY <= 7 - word.length() || startPosX != 7) {
// in case of a wrong coordinate
cout << "Please choose another direction or input another coordinate." << endl;
break;
}
}
else {
if (board[startPosY][startPosX + spaceVal] != newLetter) {
// in case 2 intersecting letters don't match
cout << "Please choose another direction or input another coordinate" << endl;
break;
}
}
if (startPosY + word.length() > 14) {
// do not go outside the board
cout << "Please choose another direction or input another coordinate." << endl;
break;
}
else {
for (int k = startPosY;k < startPosY + word.length();k++) {
board[k][startPosX] = word[counter]; // overwrites the board
// checking for multiplier tiles
if (noWords == false) {
if (k == startPosY + spaceVal) {
counter++; // if an intersecting tile is on a multiplier it cannot be used again
}
else {
if ((k == 0 && (startPosX == 0 || startPosX == 7 || startPosX == 14)) || (k == 7 && (startPosX == 0 || startPosX == 14)) || (k == 14 && (startPosX == 0 || startPosX == 7 || startPosX == 14))) {
multiplier = 3;
}
else if (((k == 0 || k == 14) && (startPosX == 3 || startPosX == 11)) || ((k == 2 || k == 12) && (startPosX == 6 || startPosX == 8)) || ((k == 3 || k == 11) && (startPosX == 0 || startPosX == 7 || startPosX == 14)) || ((k == 6 || k == 8) && (startPosX == 2 || startPosX == 6 || startPosX == 8 || startPosX == 12)) || (k == 7 && (startPosX == 3 || startPosX == 11))) {
wordPoints[counter] *= 2;
}
else if ((k == 1 || k == 13) && (startPosX == 5 || startPosX == 9) || ((k == 5 || k == 9) && (startPosX == 1 || startPosX == 5 || startPosX == 9 || startPosX == 13))) {
wordPoints[counter] *= 3;
}
else if (k == startPosX || 14 - k == startPosX) {
multiplier = 2;
}
counter++;
}
}
else {
if ((k == 0 && (startPosX == 0 || startPosX == 7 || startPosX == 14)) || (k == 7 && (startPosX == 0 || startPosX == 14)) || (k == 14 && (startPosX == 0 || startPosX == 7 || startPosX == 14))) {
multiplier = 3;
}
else if (((k == 0 || k == 14) && (startPosX == 3 || startPosX == 11)) || ((k == 2 || k == 12) && (startPosX == 6 || startPosX == 8)) || ((k == 3 || k == 11) && (startPosX == 0 || startPosX == 7 || startPosX == 14)) || ((k == 6 || k == 8) && (startPosX == 2 || startPosX == 6 || startPosX == 8 || startPosX == 12)) || (k == 7 && (startPosX == 3 || startPosX == 11))) {
wordPoints[counter] *= 2;
}
else if ((k == 1 || k == 13) && (startPosX == 5 || startPosX == 9) || ((k == 5 || k == 9) && (startPosX == 1 || startPosX == 5 || startPosX == 9 || startPosX == 13))) {
wordPoints[counter] *= 3;
}
else if (k == startPosX || 14 - k == startPosX) {
multiplier = 2;
}
counter++;
}
}
validPlace = true; // word is valid
noWords = false;
}
break;
}
default: {
// in case of a wrong input
cout << "Invalid" << endl;
break;
}
}
}
scoring(wordPoints, multiplier, score1, score2, isP2, word); // scores the word
// next turn
cout << "Press a to go back to the menu" << endl;
cin >> option;
while (option != 'a') {
cout << "Please input a (lowercase)" << endl;
cin >> option;
}
system("cls");
if (isP2 == false) {
isP2 = true;
}
else {
isP2 = false;
}
}
void scoring(int wordPoints[], int& multiplier, int& score1, int& score2, bool& isP2, string word) {
int points = 0;
for (int i = 0;i < 7;i++) {
points += wordPoints[i]; // adds on the points based on each character's value
}
points *= multiplier; // if there was a multiplier multiply the points
if (word.length() == 7) {
// in case if the user gets a bingo
cout << "You got a bingo! That's 50 more points!" << endl;
points += 50;
}
cout << "You scored " << points << " this turn!" << endl;
if (isP2 == false) {
score1 += points; // stores the score and shows the total
cout << "Your total score is now " << score1 << endl;
}
else {
score2 += points; // stores the score and shows the total
cout << "Your total score is now " << score2 << endl;
}
}
void viewScore(int& score1, int& score2, string names[]) {
char option;
cout << names[0] << ": " << score1 << "\n\n" << names[1] << ": " << score2 << endl; // shows player 1 and 2 scores
cout << "Press a to go back to the menu" << endl;
cin >> option;
while (option != 'a') {
cout << "Please input a (lowercase)" << endl;
cin >> option;
}
system("cls");
}
void handGen(char hand[], int handVals[], char p2Hand[], int p2HandVals[], bool boolHand[], bool boolHand2[], TileBag& bag) {
srand(time(NULL));
Tile tempTile;
for (int i = 0;i < 7;i++) {
if (boolHand[i] == false) {
tempTile = bag.takeTile(); // take a tile out of the bag
hand[i] = tempTile.letter;
handVals[i] = tempTile.value;
boolHand[i] = true;
// assign the int and char value of the tile then flag the index
}
else {
// if the index is flagged just skip it
continue;
}
}
for (int j = 0;j < 7;j++) {
if (boolHand2[j] == false) {
tempTile = bag.takeTile(); // take a tile out of the bag
p2Hand[j] = tempTile.letter;
p2HandVals[j] = tempTile.value;
boolHand2[j] = true;
// assign the letter and char value of the tile then flag the index
}
else {
// if the index is flagged just skip it
continue;
}
}
}
void exchange(char hand[], int handVals[], char p2Hand[], int p2HandVals[], bool boolHand[], bool boolHand2[], bool& isP2, TileBag& bag) {
int exArr[7];
Tile goneTile;
char option;
cout << "Welcome to exchange menu. Which pieces would you like to put back in the bag for some new ones?" << endl;
cout << "You will input 1 if you want to remove the piece and 0 if you want to keep it" << endl;
if (isP2 == false) {
for (int i = 0;i < 7;i++) {
cout << hand[i] << "(" << handVals[i] << ")" << " "; // showing hand
}
cout << endl;
for (int j = 0;j < 7;j++) {
exArr[j] = numCheck(exArr[j]);
while (exArr[j] > 1 || exArr[j] < 0) {
// if the input is not 1 or 0
cout << "Invalid." << endl;
exArr[j] = numCheck(exArr[j]);
}
if (exArr[j] == 1) {
boolHand[j] = false; // if 1 is inputted exchange tile and flag the index
}
else {
continue; // is 0 is inputted do nothing
}
}
for (int k = 0;k < 7;k++) {
if (boolHand[k] == false) {
// replace the tile of each flagged index by talking the tile and putting it back in the bag
goneTile.letter = hand[k];
goneTile.value = handVals[k];
bag.returnTile(goneTile);
}
}
handGen(hand, handVals, p2Hand, p2HandVals, boolHand, boolHand2, bag); // give user their now missing tiles
cout << "Here is your new hand:" << endl;
for (int l = 0;l < 7;l++) {
cout << hand[l] << "(" << handVals[l] << ")" << " "; // show the user's hand
}
cout << endl;
isP2 = true; // change the player