This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameEngine.cpp
1250 lines (1037 loc) · 44.1 KB
/
GameEngine.cpp
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 "GameEngine.h"
#include "TileCodes.h"
#include <array>
#include <bits/stdc++.h>
#include <iostream>
#include <random>
#include <sstream>
#include <stdexcept>
#include <iostream>
#define MAXIMUM_TILES_BAG 72
#define MAXIMUM_PLAYER_HAND 6
#define NUMBER_OF_COLOR 6
#define NUMBER_OF_SHAPE 6
#define COMMAND_SIZE_WHEN_PLACE_TILE 4
#define COMMAND_SIZE_WHEN_REPLACE_TILE 2
#define COMMAND_SIZE_WHEN_SAVING_GAME 2
#define ASCII_FOR_0 48
#define ID_OF_FIRST_PLAYER 1
#define ID_OF_SECOND_PLAYER 2
#define PLAY_GAME_SELECTION 1
#define LOAD_GAME_SELECTION 2
GameEngine::GameEngine() {
this->playerCount = 0;
tileBagStr = "";
}
GameEngine::~GameEngine() {
}
void GameEngine::getState(Player *p) {
std::cout << "\n" << p->getPlayerName() << ", it's your turn." << std::endl;
printScore();
std::cout << std::endl;
std::cout << board;
std::cout << "\nYour hand is " << std::endl;
p->displayTileInHand();
std::cout << "\n> " << std::flush;
}
int GameEngine::countToken(std::string input) {
int count = 0;
std::string s1 = "";
std::istringstream token(input);
while (token >> s1) {
count++;
}
return count;
}
void GameEngine::addPlayer(Player *p) {
playerList[playerCount] = new Player(p->getPlayerId(), p->getPlayerName(),
p->getPlayerHand());
playerCount++;
}
void GameEngine::printScore() {
for (int i = 0; i < this->playerCount; i++) {
std::cout << "Score for " << playerList[i]->getPlayerName() << ": "
<< playerList[i]->getPlayerScore() << std::endl;
}
}
Player* GameEngine::getHighScorePlayer(int i) {
return highScoreplayer[i];
}
//validate format only return true if the command input matches the following format:
//format for placing: place <tile> at <location>
//format for replacing: replace <tile>
//format for passing turn: pass
bool GameEngine::validateFormat(std::string input) {
bool result = false;
std::regex place("^place ([ROYGBP][1-6]) at ([A-Z]([0-9]|1[0-9]|2[0-5]))");
std::regex replace("^replace ([ROYGBP][1-6])");
std::regex pass("pass");
std::regex help("help");
if (std::regex_match(input, place) || std::regex_match(input, replace)
|| std::regex_match(input, pass) || std::regex_match(input, help)) {
result = true;
}
return result;
}
bool GameEngine::validateTileExistInHand(std::string tileInput,
Player *player) {
bool result = false;
int size = tileInput.size();
//tile is always a two-char representation
char char_array[size + 1];
strcpy(char_array, tileInput.c_str());
//ASCII to convert character into an integer
Tile *tileToBeCompare = new Tile(char_array[0], char_array[1] - ASCII_FOR_0);
if (player->getPlayerHand()->contains(tileToBeCompare)) {
result = true;
}
delete tileToBeCompare;
return result;
}
//tile in any command is at the second position
std::string GameEngine::getTileFromUserInput(std::string input) {
std::string tile = "";
std::istringstream token(input);
//tile in any command is at the second position
for (int i = 0; i < 1; i++) {
token >> tile;
}
token >> tile;
return tile;
}
//location in placing command is at the fourth position so we loop
//to tokenize three words to get to the fourth position
std::string GameEngine::getLocationFromUserInput(std::string input) {
std::string location = "";
std::istringstream token(input);
for (int i = 0; i < 3; i++) {
token >> location;
}
token >> location;
return location;
}
//this method will be called after every saving command input from user
void GameEngine::saveGame(std::string filename, Player* player,
LinkedList* tileBag) {
std::ofstream outFile;
outFile.open(filename);
for (int i = 0; i < this->playerCount; i++) {
outFile << playerList[i]->getPlayerName() << std::endl;
outFile << playerList[i]->getPlayerScore() << std::endl;
outFile << playerList[i]->getPlayerHand()->displayList() << std::endl;
}
outFile << tileBag->displayList() << std::endl;
outFile << getRecords();
outFile << player->getPlayerName() << std::endl;
outFile.close();
}
//name of file in saving command is at the second position
std::string GameEngine::getNameOfFileFromUserInput(std::string input) {
std::string fileName = "";
std::istringstream token(input);
//tile in any command is at the second position
for (int i = 0; i < 1; i++) {
token >> fileName;
}
token >> fileName;
return fileName;
}
void GameEngine::printMessageWhenSaveGame(std::string& inputFromUser,
Player* player, LinkedList* tileBag, bool& quitGame) {
while (inputFromUser.substr(0, 4) == "save"
&& validateFormat(inputFromUser) == false
&& countToken(inputFromUser) == COMMAND_SIZE_WHEN_SAVING_GAME) {
//call saveGame() here
saveGame(getNameOfFileFromUserInput(inputFromUser), player, tileBag);
std::cout << "\nGame successfully saved\n" << std::endl;
std::cout << "> " << std::flush;
//saveGame or quitGame(first time input) goes into here
std::getline(std::cin, inputFromUser);
if (std::cin.eof() == true) {
addHighestPlayer(getPlayerWithHighestScoreWhenEnd());
keepHighestRecords();
std::cout << "\n\nGoodbye" << std::endl;
quitGame = true;
}
}
}
void GameEngine::printMessageWhenInvalidFormat(std::string& inputFromUser,
Player* player, LinkedList* tileBag, bool& quitGame, bool& ableToAdd) {
while ((validateFormat(inputFromUser) == false
|| validateTileExistInHand(getTileFromUserInput(inputFromUser), player)
== false) && quitGame != true) {
if (inputFromUser.substr(0, 4)
!= "save"|| countToken(inputFromUser) != COMMAND_SIZE_WHEN_SAVING_GAME) {
if (validateFormat(inputFromUser) == false) {
std::cout << "\nInvalid Input, Please check your format again\n" << std::flush;
} else if(inputFromUser == "help"){
std::cout << "To place a tile, use the following syntax: 'place <tile> at <location'"
"\nTo replace a tile, use the following syntax: 'replace <tile>'" << std::endl;
}
else if (validateTileExistInHand(inputFromUser, player) == false) {
std::cout << "\nInvalid Input, tile input does not exist in your hand\n" << std::flush;
}
std::cout << "> " << std::flush;
ableToAdd = true;
std::getline(std::cin, inputFromUser);
if (std::cin.eof() == true) {
addHighestPlayer(getPlayerWithHighestScoreWhenEnd());
keepHighestRecords();
std::cout << "\n\nGoodbye" << std::endl;
quitGame = true;
}
} else {
if (countToken(inputFromUser) == 2) {
//call saveGame() here
saveGame(getNameOfFileFromUserInput(inputFromUser), player, tileBag);
std::cout << "\nGame successfully saved\n\n" << std::endl;
std::cout << "> " << std::flush;
ableToAdd = true;
std::getline(std::cin, inputFromUser);
if (std::cin.eof() == true) {
addHighestPlayer(getPlayerWithHighestScoreWhenEnd());
keepHighestRecords();
std::cout << "\n\nGoodbye" << std::endl;
quitGame = true;
}
}
}
}
}
void GameEngine::printInvalidWhenIllegalMove(bool& ableToAdd, bool& quitGame,
std::string& inputFromUser) {
if (ableToAdd == false) {
std::cout << "\nInvalid Input, Please check your move on the board" << std::endl;
std::cout << "> " << std::flush;
std::getline(std::cin, inputFromUser);
if (std::cin.eof() == true) {
addHighestPlayer(getPlayerWithHighestScoreWhenEnd());
keepHighestRecords();
std::cout << "\n\nGoodbye" << std::endl;
quitGame = true;
}
}
}
void GameEngine::keepHighestRecords() {
std::ofstream outFile;
outFile.open("hrecords.txt");
for (unsigned int i = 0; i < highScoreplayer.size(); i++) {
outFile << highScoreplayer[i]->getPlayerName() << ":"
<< highScoreplayer[i]->getPlayerScore() << std::endl;
delete highScoreplayer[i];
}
outFile.close();
}
//keeping all the moves in file "records.txt"
void GameEngine::keepRecords(std::string inputFromUser) {
std::ofstream outFile;
outFile.open("records.txt", std::ofstream::app);
outFile << inputFromUser << std::endl;
outFile.close();
}
//get records as a one string
std::string GameEngine::getRecords() {
std::string allMoves = "";
std::ifstream file("records.txt");
if (file.is_open()) {
std::string line = "";
while (std::getline(file, line)) {
allMoves.append(line);
allMoves.append("\n");
}
}
return allMoves;
}
//construct player state from saving file, focusing on three components:
//player name, player score and tiles in player hand
void GameEngine::constructPlayerState(std::string& firstPlayerName,
std::string& scoreFirstPlayer, std::string& firstPlayerHand,
std::string& secondPlayerName, std::string& scoreSecondPlayer,
std::string& secondPlayerHand) {
int score_1 = 0;
int score_2 = 0;
LinkedList* firstPlayerHandLL = new LinkedList();
LinkedList* secondPlayerHandLL = new LinkedList();
std::string tmp = "";
std::istringstream inputPlayerHand_1(firstPlayerHand);
Player* firstPlayer = new Player(ID_OF_FIRST_PLAYER, firstPlayerName, firstPlayerHandLL);
while (std::getline(inputPlayerHand_1, tmp, ',')) {
char cTileArray[3];
strcpy(cTileArray, tmp.c_str());
Tile* tileToBeAdded = new Tile(cTileArray[0], cTileArray[1] - ASCII_FOR_0);
firstPlayer->getPlayerHand()->addBack(tileToBeAdded);
delete tileToBeAdded;
}
std::istringstream inputPlayerHand_2(secondPlayerHand);
Player* secondPlayer = new Player(ID_OF_SECOND_PLAYER, secondPlayerName, secondPlayerHandLL);
while (std::getline(inputPlayerHand_2, tmp, ',')) {
char cTileArray[3];
strcpy(cTileArray, tmp.c_str());
Tile* tileToBeAdded = new Tile(cTileArray[0], cTileArray[1] - ASCII_FOR_0);
secondPlayer->getPlayerHand()->addBack(tileToBeAdded);
delete tileToBeAdded;
}
std::istringstream input_1(scoreFirstPlayer);
input_1 >> score_1;
std::istringstream input_2(scoreSecondPlayer);
input_2 >> score_2;
addPlayer(firstPlayer);
addPlayer(secondPlayer);
playerList[0]->setPlayerScore(score_1);
playerList[1]->setPlayerScore(score_2);
delete firstPlayer;
delete secondPlayer;
}
void GameEngine::forwardTileBag(std::string& tileBagStr) {
this->tileBagStr = tileBagStr;
}
//construct tile bag from saving file
LinkedList* GameEngine::constructTileBag(std::string& tileBag) {
LinkedList* tileBagList = new LinkedList();
std::istringstream inString(tileBag);
std::string tmp = "";
while (std::getline(inString, tmp, ',')) {
int size = 0;
size = tmp.size();
char cArray[size + 1];
strcpy(cArray, tmp.c_str());
Tile* tileToBeAdded = new Tile(cArray[0], cArray[1] - ASCII_FOR_0);
tileBagList->addBack(tileToBeAdded);
delete tileToBeAdded;
}
return tileBagList;
}
//construct board based on all the moves reading from saving file
//replay the game
void GameEngine::constructBoard(std::string& moves) {
std::string allMove = moves;
std::string oneMove = "";
std::istringstream input(allMove);
bool firstMove = true;
while (std::getline(input, oneMove)) {
if (oneMove == "pass") {
board.resetValidation();
} else {
std::string tileInput = getTileFromUserInput(oneMove);
std::string location = getLocationFromUserInput(oneMove);
char cTileArray[3];
strcpy(cTileArray, tileInput.c_str());
Tile* newTile = new Tile(cTileArray[0], cTileArray[1] - ASCII_FOR_0);
if (firstMove == true) {
int sizeLocation = location.size();
if (sizeLocation == 2) {
char cLocationArray[sizeLocation + 1];
strcpy(cLocationArray, location.c_str());
Coordinate c = Coordinate(cLocationArray[0],
cLocationArray[1] - ASCII_FOR_0, *newTile);
board.addTileAt(c);
} else {
char cLocationArray[sizeLocation + 1];
strcpy(cLocationArray, location.c_str());
int xCoordinate = (cLocationArray[1] - ASCII_FOR_0) * 10
+ (cLocationArray[2] - ASCII_FOR_0);
Coordinate c = Coordinate(cLocationArray[0], xCoordinate, *newTile);
board.addTileAt(c);
}
firstMove = false;
} else {
int sizeLocation = location.size();
if (sizeLocation == 2) {
char cLocationArray[sizeLocation + 1];
strcpy(cLocationArray, location.c_str());
Coordinate c = Coordinate(cLocationArray[0],
cLocationArray[1] - ASCII_FOR_0, *newTile);
if (!board.addTileAt(c)) {
throw std::runtime_error(
"ERROR: Your file name entered has wrong format, cannot load file\n");
}
} else {
char cLocationArray[sizeLocation + 1];
strcpy(cLocationArray, location.c_str());
int xCoordinate = (cLocationArray[1] - ASCII_FOR_0) * 10
+ (cLocationArray[2] - ASCII_FOR_0);
Coordinate c = Coordinate(cLocationArray[0], xCoordinate, *newTile);
if (!board.addTileAt(c)) {
throw std::runtime_error(
"ERROR: Your file name entered has wrong format, cannot load file\n");
}
}
}
delete newTile;
}
}
}
//after placing one tile players are asked if they want to continue to place tile on board
void GameEngine::askingForPlacingMultipleTiles(bool& ableToAddTile,
bool& firstPlayerTurn, bool& repromptPlayer, bool& quitGame, Player* player,
LinkedList* tileBag) {
bool passTurn = false;
bool printState = true;
std::string inputFromUser = "";
int countTilePlacing = MAXIMUM_PLAYER_HAND
- player->getPlayerHand()->getSize();
do {
if (printState == true) {
getState(player);
std::getline(std::cin, inputFromUser);
}
if (inputFromUser == "pass") {
ableToAddTile = true;
repromptPlayer = false;
if (player->getPlayerId() == ID_OF_FIRST_PLAYER) {
firstPlayerTurn = false;
} else {
firstPlayerTurn = true;
}
keepRecords(inputFromUser);
passTurn = true;
}
else if (std::cin.eof() == true) {
addHighestPlayer(getPlayerWithHighestScoreWhenEnd());
keepHighestRecords();
std::cout << "\n\nGoodbye" << std::endl;
passTurn = true;
quitGame = true;
}
else {
if (validateFormat(inputFromUser) == false
|| validateTileExistInHand(getTileFromUserInput(inputFromUser),
player) == false || ableToAddTile == false) {
if (validateFormat(inputFromUser) == false) {
if (countToken(inputFromUser) == COMMAND_SIZE_WHEN_SAVING_GAME
&& inputFromUser.substr(0, 4) == "save") {
std::cout << "\nGame successfully saved" << std::endl;
std::string filename = getNameOfFileFromUserInput(inputFromUser);
saveGame(filename, player, tileBag);
std::cout << "> " << std::flush;
std::getline(std::cin, inputFromUser);
}
else {
std::cout << "Invalid Input, please check your format" << std::endl;
std::cout << "> " << std::flush;
std::getline(std::cin, inputFromUser);
}
} else if (validateTileExistInHand(getTileFromUserInput(inputFromUser),
player) == false) {
if(inputFromUser == "help"){
std::cout << "To place a tile, use the following syntax: 'place <tile> at <location'"
"\nTo replace a tile, use the following syntax: 'replace <tile>'" << std::endl;
std::cout << "> " << std::flush;
std::getline(std::cin, inputFromUser);
}
else{
std::cout << "Invalid Input, tile input does not exist in hand" << std::endl;
std::cout << "> " << std::flush;
std::getline(std::cin, inputFromUser);
}
} else if (ableToAddTile == false) {
std::cout << "Invalid Input, please check your move on the board" << std::endl;
std::cout << "> " << std::flush;
std::getline(std::cin, inputFromUser);
ableToAddTile = true;
}
printState = false;
} else {
if (countToken(inputFromUser) == COMMAND_SIZE_WHEN_PLACE_TILE) {
std::string tileInput = getTileFromUserInput(inputFromUser);
std::string gridLocation = getLocationFromUserInput(inputFromUser);
if (validateFormat(inputFromUser) == true
&& validateTileExistInHand(tileInput, player) == true) {
int size = tileInput.size();
char cTileInput[size + 1];
strcpy(cTileInput, tileInput.c_str());
Tile *newTile = new Tile(cTileInput[0],
cTileInput[1] - ASCII_FOR_0);
if (gridLocation.size() == 2) {
int sizeLocation = gridLocation.size();
char cSizeInput[sizeLocation + 1];
strcpy(cSizeInput, gridLocation.c_str());
//cSizeInput[0] = y , cSizeInput[1] = x
Coordinate c = Coordinate(cSizeInput[0],
cSizeInput[1] - ASCII_FOR_0, *newTile);
if (!board.addTileAt(c)) {
ableToAddTile = false;
repromptPlayer = true;
if (player->getPlayerId() == 2) {
firstPlayerTurn = false;
}
printState = false;
} else {
countTilePlacing++;
player->getPlayerHand()->deleteTile(newTile);
repromptPlayer = false;
if (player->getPlayerId() == 2) {
firstPlayerTurn = true;
}
ableToAddTile = true;
printState = true;
keepRecords(inputFromUser);
}
}
//grid location is a three-char
else {
int sizeLocation = gridLocation.size();
char cSizeInput[sizeLocation + 1];
strcpy(cSizeInput, gridLocation.c_str());
int x = (cSizeInput[1] - ASCII_FOR_0) * 10
+ (cSizeInput[2] - ASCII_FOR_0);
//cSizeInput[0] = y, x = x coordinate
Coordinate c = Coordinate(cSizeInput[0], x, *newTile);
if (!board.addTileAt(c)) {
ableToAddTile = false;
if (player->getPlayerId() == 2) {
firstPlayerTurn = false;
}
repromptPlayer = true;
printState = false;
} else {
countTilePlacing++;
player->getPlayerHand()->deleteTile(newTile);
if (player->getPlayerId() == 2) {
firstPlayerTurn = true;
}
repromptPlayer = false;
ableToAddTile = true;
printState = true;
keepRecords(inputFromUser);
}
}
delete newTile;
}
} else {
printState = false;
std::cout
<< "\nInvalid Input, you can only continue placing tiles\n" << std::endl;
std::cout << ">" << std::flush;
std::getline(std::cin, inputFromUser);
}
}
}
} while (passTurn == false);
for (int i = 0; i < countTilePlacing; ++i) {
player->getPlayerHand()->addBack(tileBag->getFront());
tileBag->deleteFront();
}
//only calculate score in the end when players finish placing their tiles
player->setPlayerScore(board.totalPoint());
if (board.isQwirkle()) {
std::cout << "\nQWRIKLEEEEEEEE!!!\n" << std::endl;
}
board.resetValidation();
}
//Flow of the game occurs in this method
void GameEngine::playGame(std::string p1, std::string p2, int selection) {
//tileBag created inside scope of playGame
LinkedList* tileBag = nullptr;
LinkedList* firstPlayerHand = new LinkedList();
LinkedList* secondPlayerHand = new LinkedList();
bool firstPlayerTurn = true;
bool repromtFirstPlayer = true;
bool repromptSecondPlayer = true;
bool ableToAddTileForPlayer1 = true;
bool ableToAddTileForPlayer2 = true;
bool quitGame = false;
std::string inputFromUser = "";
std::string tileInput = "";
std::string gridLocation = "";
int turn = 0;
//if players play a new game we will construct the state of the game this way
if (selection == PLAY_GAME_SELECTION) {
tileBag = new LinkedList();
Player* firstPlayer = nullptr;
Player* secondPlayer = nullptr;
firstPlayer = new Player(ID_OF_FIRST_PLAYER, p1, firstPlayerHand);
secondPlayer = new Player(ID_OF_SECOND_PLAYER, p2, secondPlayerHand);
addPlayer(firstPlayer);
addPlayer(secondPlayer);
// Create the tile bag - comment this out when testing
// shuffleAndCreateTileBag(tileBag);
//tile bag for testing purpose - uncomment this when testing unit tests
tileBagForUnitTest(tileBag);
setUpTilesInitially(firstPlayer, tileBag);
setUpTilesInitially(secondPlayer, tileBag);
delete firstPlayer;
delete secondPlayer;
//if player choose to load game we will construct the state of the game this way
} else if (selection == LOAD_GAME_SELECTION) {
tileBag = this->constructTileBag(tileBagStr);
if (p1 == playerList[0]->getPlayerName()) {
firstPlayerTurn = true;
} else if (p1 == playerList[1]->getPlayerName()) {
firstPlayerTurn = false;
}
turn++;
}
//game starts here
do {
if (firstPlayerTurn == true
&& playerList[0]->getPlayerHand()->getSize() == 6) {
this->getState(playerList[0]);
std::getline(std::cin, inputFromUser);
if (std::cin.eof() == true) {
addHighestPlayer(getPlayerWithHighestScoreWhenEnd());
keepHighestRecords();
std::cout << "\n\nGoodbye" << std::endl;
quitGame = true;
}
}
//at the start of the game we do not have to check legal move on board
if (turn == 0 && firstPlayerTurn == true) {
//if player does not quit
if (quitGame != true) {
//start prompting first player and validating here
do {
//saveGame and invalid input (include invalidformat, invalidtileexistinhand and invalidmove ) goes into here
if (validateFormat(inputFromUser) == false
|| validateTileExistInHand(getTileFromUserInput(inputFromUser),
playerList[0]) == false) {
//suppose player saves game many times using while loop in this method
printMessageWhenSaveGame(inputFromUser, playerList[0], tileBag,
quitGame);
printMessageWhenInvalidFormat(inputFromUser, playerList[0], tileBag,
quitGame, ableToAddTileForPlayer1);
}
//after save and invalid input and do not quitGame goes into here for further validation
//or input correctly the first time
if (quitGame != true) {
if (countToken(inputFromUser) == COMMAND_SIZE_WHEN_PLACE_TILE) {
tileInput = getTileFromUserInput(inputFromUser);
gridLocation = getLocationFromUserInput(inputFromUser);
//passing the validateFormat and tileExistInHand, proceed to board validation
if (validateFormat(inputFromUser) == true
&& validateTileExistInHand(tileInput, playerList[0])
== true) {
firstPlayerTurn = false;
//turn to c-style string for comparison
int size = tileInput.size();
char cTileInput[size + 1];
strcpy(cTileInput, tileInput.c_str());
//using ASCII to convert char into integer
Tile *newTile = new Tile(cTileInput[0],
cTileInput[1] - ASCII_FOR_0);
if (gridLocation.size() == 2) {
int sizeLocation = gridLocation.size();
char cSizeInput[sizeLocation + 1];
strcpy(cSizeInput, gridLocation.c_str());
//using ASCII to convert char into integer
Coordinate c = Coordinate(cSizeInput[0],
cSizeInput[1] - ASCII_FOR_0, *newTile);
board.addTileAt(c);
ableToAddTileForPlayer1 = true;
playerList[0]->getPlayerHand()->deleteTile(newTile);
keepRecords(inputFromUser);
askingForPlacingMultipleTiles(ableToAddTileForPlayer1,
firstPlayerTurn, repromtFirstPlayer, quitGame,
playerList[0], tileBag);
} else {
int sizeLocation = gridLocation.size();
char cSizeInput[sizeLocation + 1];
strcpy(cSizeInput, gridLocation.c_str());
int x = (cSizeInput[1] - ASCII_FOR_0) * 10
+ (cSizeInput[2] - ASCII_FOR_0);
//cSizeInput[0] = y coordinate, x = x coordinate
Coordinate c = Coordinate(cSizeInput[0], x, *newTile);
board.addTileAt(c);
playerList[0]->getPlayerHand()->deleteTile(newTile);
ableToAddTileForPlayer1 = true;
keepRecords(inputFromUser);
askingForPlacingMultipleTiles(ableToAddTileForPlayer1,
firstPlayerTurn, repromtFirstPlayer, quitGame,
playerList[0], tileBag);
}
delete newTile;
turn++;
repromtFirstPlayer = false;
}
} else if (countToken(
inputFromUser) == COMMAND_SIZE_WHEN_REPLACE_TILE) {
tileInput = getTileFromUserInput(inputFromUser);
if (validateFormat(inputFromUser)
&& validateTileExistInHand(tileInput, playerList[0])) {
firstPlayerTurn = false;
int size = tileInput.size();
char cTileInput[size + 1];
strcpy(cTileInput, tileInput.c_str());
Tile *newTile = new Tile(cTileInput[0],
cTileInput[1] - ASCII_FOR_0);
tileBag->addBack(newTile);
playerList[0]->getPlayerHand()->deleteTile(newTile);
playerList[0]->getPlayerHand()->addBack(tileBag->getFront());
tileBag->deleteFront();
delete newTile;
ableToAddTileForPlayer1 = true;
turn++;
repromtFirstPlayer = false;
}
}
}
} while ((validateFormat(inputFromUser) == false
|| validateTileExistInHand(getTileFromUserInput(inputFromUser),
playerList[0]) == false) && quitGame != true
&& repromtFirstPlayer == true && ableToAddTileForPlayer1 == false);
}
//not the initial turn
} else if (turn != 0) {
//if player 1 does not want to quitGame start validating input
if (quitGame != true) {
//deciding whether it is the first turn of that player or player has already made one move and
//can pass or continue placing tiles
if (firstPlayerTurn
== true&& playerList[0]->getPlayerHand()->getSize() == MAXIMUM_PLAYER_HAND) {
//start prompting player 1 here
do {
//saveGame goes into this thread
if (validateFormat(inputFromUser) == false
|| validateTileExistInHand(getTileFromUserInput(inputFromUser),
playerList[0]) == false
|| ableToAddTileForPlayer1 == false) {
//suppose player saves game many times
printMessageWhenSaveGame(inputFromUser, playerList[0], tileBag,
quitGame);
printMessageWhenInvalidFormat(inputFromUser, playerList[0],
tileBag, quitGame, ableToAddTileForPlayer1);
printInvalidWhenIllegalMove(ableToAddTileForPlayer1, quitGame,
inputFromUser);
}
//player does not quit
if (quitGame != true) {
if (countToken(inputFromUser) == COMMAND_SIZE_WHEN_PLACE_TILE) {
tileInput = getTileFromUserInput(inputFromUser);
gridLocation = getLocationFromUserInput(inputFromUser);
if (validateFormat(inputFromUser) == true
&& validateTileExistInHand(tileInput, playerList[0])
== true) {
int size = tileInput.size();
char cTileInput[size + 1];
strcpy(cTileInput, tileInput.c_str());
Tile *newTile = new Tile(cTileInput[0],
cTileInput[1] - ASCII_FOR_0);
if (gridLocation.size() == 2) {
int sizeLocation = gridLocation.size();
char cSizeInput[sizeLocation + 1];
strcpy(cSizeInput, gridLocation.c_str());
//cSizeInput[0] = y , cSizeInput[1] = x
Coordinate c = Coordinate(cSizeInput[0],
cSizeInput[1] - ASCII_FOR_0, *newTile);
if (!board.addTileAt(c)) {
ableToAddTileForPlayer1 = false;
firstPlayerTurn = true;
repromtFirstPlayer = true;
} else {
playerList[0]->getPlayerHand()->deleteTile(newTile);
keepRecords(inputFromUser);
ableToAddTileForPlayer1 = true;
firstPlayerTurn = false;
repromtFirstPlayer = false;
askingForPlacingMultipleTiles(ableToAddTileForPlayer1,
firstPlayerTurn, repromtFirstPlayer, quitGame,
playerList[0], tileBag);
}
} else {
int sizeLocation = gridLocation.size();
char cSizeInput[sizeLocation + 1];
strcpy(cSizeInput, gridLocation.c_str());
int x = (cSizeInput[1] - ASCII_FOR_0) * 10
+ (cSizeInput[2] - ASCII_FOR_0);
Coordinate c = Coordinate(cSizeInput[0], x, *newTile);
if (!board.addTileAt(c)) {
ableToAddTileForPlayer1 = false;
firstPlayerTurn = true;
repromtFirstPlayer = true;
} else {
playerList[0]->getPlayerHand()->deleteTile(newTile);
ableToAddTileForPlayer1 = true;
firstPlayerTurn = false;
repromtFirstPlayer = false;
keepRecords(inputFromUser);
askingForPlacingMultipleTiles(ableToAddTileForPlayer1,
firstPlayerTurn, repromtFirstPlayer, quitGame,
playerList[0], tileBag);
}
}
delete newTile;
turn++;
}
} else if (countToken(
inputFromUser) == COMMAND_SIZE_WHEN_REPLACE_TILE) {
tileInput = getTileFromUserInput(inputFromUser);
if (validateFormat(inputFromUser)
&& validateTileExistInHand(tileInput, playerList[0])) {
firstPlayerTurn = false;
int size = tileInput.size();
char cTileInput[size + 1];
strcpy(cTileInput, tileInput.c_str());
Tile *newTile = new Tile(cTileInput[0],
cTileInput[1] - ASCII_FOR_0);
tileBag->addBack(newTile);
playerList[0]->getPlayerHand()->deleteTile(newTile);
playerList[0]->getPlayerHand()->addBack(tileBag->getFront());
tileBag->deleteFront();
delete newTile;
ableToAddTileForPlayer1 = true;
turn++;
repromtFirstPlayer = false;
}
}
}
} while ((validateFormat(inputFromUser) == false
|| validateTileExistInHand(getTileFromUserInput(inputFromUser),
playerList[0]) == false || ableToAddTileForPlayer1 == false)
&& quitGame != true && repromtFirstPlayer == true);
} else if (playerList[0]->getPlayerHand()->getSize() != MAXIMUM_PLAYER_HAND) {
askingForPlacingMultipleTiles(ableToAddTileForPlayer1,
firstPlayerTurn, repromtFirstPlayer, quitGame, playerList[0],
tileBag);
}
//player 2 turn- start prompting player 2 here
if (quitGame != true&& firstPlayerTurn == false
&& playerList[1]->getPlayerHand()->getSize() == MAXIMUM_PLAYER_HAND) {
this->getState(playerList[1]);
std::getline(std::cin, inputFromUser);
if (std::cin.eof()) {
addHighestPlayer(getPlayerWithHighestScoreWhenEnd());
keepHighestRecords();
std::cout << "\n\nGoodbye" << std::endl;
quitGame = true;
}
//if player 2 does not want to quitGame
if (quitGame != true) {
do {
if (validateFormat(inputFromUser) == false
|| validateTileExistInHand(
getTileFromUserInput(inputFromUser), playerList[1])
== false || ableToAddTileForPlayer2 == false) {
printMessageWhenSaveGame(inputFromUser, playerList[1], tileBag,
quitGame);
printMessageWhenInvalidFormat(inputFromUser, playerList[1],
tileBag, quitGame, ableToAddTileForPlayer2);
printInvalidWhenIllegalMove(ableToAddTileForPlayer2, quitGame,
inputFromUser);
}
if (quitGame != true) {
if (countToken(inputFromUser) == COMMAND_SIZE_WHEN_PLACE_TILE) {
tileInput = getTileFromUserInput(inputFromUser);
gridLocation = getLocationFromUserInput(inputFromUser);
if (validateFormat(inputFromUser) == true
&& validateTileExistInHand(tileInput, playerList[1])) {
int size = tileInput.size();
char cTileInput[size + 1];
strcpy(cTileInput, tileInput.c_str());
Tile *newTile = new Tile(cTileInput[0],
cTileInput[1] - ASCII_FOR_0);
//grid location is a two-char A2
if (gridLocation.size() == 2) {
int sizeLocation = gridLocation.size();
char cSizeInput[sizeLocation + 1];