-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
732 lines (505 loc) · 18.9 KB
/
main.cpp
File metadata and controls
732 lines (505 loc) · 18.9 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
#include <iostream>
#include <iomanip>
#include <string>
#include "Board.h"
/*////////////////////////////////////////////////////////////////////
Name: Leoul Tilahun
MId: M00082868
Date: 02/11/2019
To: Dr Ng
//////////////////////////////////////////////////////////////////////*/
using namespace std;
/*////////////////////////////////////////////////////////////
CONSTANTS
///////////////////////////////////////////////////////////*/
const int MAX_ROW_SIZE = 11;
const int MAX_COL_SIZE = 14;
const int MIN_ROW_SIZE = 3;
const int MIN_COL_SIZE = 3;
const int MAX_NUMBER_PLAYERS = 5;
const int MIN_NUMBER_PLAYERS = 2;
/*////////////////////////////////////////////////////////////
PLAYER STRUCT
///////////////////////////////////////////////////////////*/
struct Player{
string firstName;
string fullName;
int gamesWon;
int gamesLoss;
int gamesDrawn;
char piece;
};
/*////////////////////////////////////////////////////////////
Prototypes
///////////////////////////////////////////////////////////*/
int getNumberOfPlayer();
void openningStatement();
char toUpper(char letter);
char toLower(char letter);
string getName(int player_number);
string getFirstName(string userName);
bool checkNoLastName(string userName);
string changeToCapital(string userName);
bool check_Characters(string user_Name);
string remove_spaces(string Player_name);
string toLowerCaseString(string user_Name);
Player* create_Players(int numberOfPlayers);
int getSize(const int MAX_SIZE, char rowOrCol);
void printStat(Player* gamePlayers, int numberOfPlayers);
int* checkUserMoves(string userInput, Board& playingBoard);
void get_UserMove(Player currentPlayer, Board& playingBoard);
void update_Status(Player* gamePlayers, int turn, int numberOfPlayers);
int* checkUserPosition(int* playerMove, Board& playingBoard, Player currentPlayer);
int main(){
Player* gamePlayers;
int numberOfPlayers, totalGames;
int currentPlayer, turn, numberOfMoves;
int row, column;
bool gameWon;
string quit = "0";
openningStatement();
numberOfPlayers = getNumberOfPlayer();
gamePlayers = create_Players(numberOfPlayers);
for (int i = 0; i < numberOfPlayers; i++){
gamePlayers[i].fullName = getName(i);
gamePlayers[i].firstName = getFirstName(gamePlayers[i].fullName);
}
currentPlayer = 0;
gameWon = false;
totalGames = 0;
do{
cout<<endl<< " Please enter the dimension of the board: "<< endl;
turn = currentPlayer;
row = getSize(MAX_ROW_SIZE, 'r');
column = getSize(MAX_COL_SIZE, 'c');
Board playingBoard(row, column);
numberOfMoves = 0;
gameWon = false;
do{
playingBoard.printBoard();
get_UserMove(gamePlayers[turn], playingBoard);
numberOfMoves++;
if (playingBoard.checkWin(gamePlayers[turn].piece)){
gameWon = true;
}
else{
if (numberOfMoves < row * column){
turn = (turn + 1) % numberOfPlayers;
}
}
}while ((!gameWon) && (numberOfMoves < row * column));
totalGames++;
if (gameWon){
update_Status(gamePlayers, turn, numberOfPlayers);
playingBoard.printBoard();
cout<< " Congratulations "<< gamePlayers[turn].fullName<< " You won the game!!! "<< endl;
currentPlayer = (turn + 1) % numberOfPlayers;
}
if (!gameWon && numberOfMoves == row * column){
update_Status(gamePlayers, numberOfPlayers, numberOfPlayers);
playingBoard.printBoard();
cout<< endl<<"The Game has ended in a Draw!!!"<< endl;
currentPlayer = (currentPlayer + 1) % numberOfPlayers;
}
cout<<endl<< "Total game(s) played = "<< totalGames<< "\n\n";
printStat(gamePlayers, numberOfPlayers);
cout<<endl<< " If you want to quit hit only \"0\" (zero) else hit any character"<< endl;
cout<< " Do you want to quit? ";
getline(cin, quit);
}while(quit != "0");
}
/*////////////////////////////////////////////////////////////
- gets a character and returns the smaller version
of letter
///////////////////////////////////////////////////////////*/
char toLower(char letter){
char lowerCase;
if (letter >= 'A' && letter <= 'Z'){
lowerCase = letter + 32;
return lowerCase;
}
else{
return letter;
}
}
/*////////////////////////////////////////////////////////////
- gets a character and returns the Capitalized version
of letter
///////////////////////////////////////////////////////////*/
char toUpper(char letter){
char upperCase;
if (letter >= 'a' && letter <= 'z'){
upperCase = letter - 32;
return upperCase;
}
else{
return letter;
}
}
/*////////////////////////////////////////////////////////////////
Get the player and the Board
- get the position Input form the user
- Verify the input
- Call the method to make the move
//////////////////////////////////////////////////////////////////*/
void get_UserMove(Player currentPlayer, Board& playingBoard){
string userInput;
int* playerMove;
int row_Move;
int column_Move;
cout<< " "<<currentPlayer.firstName<< "'s move("<< currentPlayer.piece<< ") Please choose a Move ---> ";
getline(cin, userInput);
playerMove = checkUserMoves(userInput, playingBoard);
playerMove = checkUserPosition(playerMove, playingBoard,currentPlayer);
row_Move = playerMove[0];
column_Move = playerMove[1];
playingBoard.make_Move(row_Move,column_Move, currentPlayer.piece);
}
/*///////////////////////////////////////////////////////////////////////
Get the user Move and the Board
- check whether if the input is in a correct form
- check if it has length b/n 2-3
- check the letter input
- check the number
- change the final input to LETTER NUMBER FORM
Return an array which contains the {row position, column position}
///////////////////////////////////////////////////////////////////////*/
int* checkUserMoves(string userInput, Board& playingBoard){
int* userMoves = nullptr;
char rowMove;
char maxRow;
bool incorrectInput;
userMoves = new int[2];
maxRow = 'a' + (playingBoard.getRow() - 1);
incorrectInput = false;
while (!incorrectInput){
if (userInput.length() < 2 || userInput.length() > 3){
incorrectInput = true;
}
rowMove = toLower(userInput[0]);
if (rowMove < 'a' || rowMove > maxRow){
incorrectInput = true;
}
if (userInput[1] < '1' || userInput[1] > '9'){
incorrectInput = true;
}
if (userInput.length() == 3){
if (userInput[2] < '0' || userInput[2] > '9'){
incorrectInput = true;
}
}
if (incorrectInput){
cout<< " Incorrect Move!!! Please enter a correct move ---> ";
getline(cin, userInput);
incorrectInput = false;
}
else{
incorrectInput = true;
}
}
rowMove = toLower(userInput[0]);
userMoves[0] = rowMove - 'a';
if (userInput.length() == 2){
userMoves[1] = (userInput[1] - '1');
}
else{
userMoves[1] = ((userInput[1] - '0') * 10) + (userInput[2]-'1');
}
return userMoves;
}
/*////////////////////////////////////////////////////////////////
Get the player, the Board and the move
- check if the position is occupied
- check if it a right column move
Return an array which contains the {row position, column position}
//////////////////////////////////////////////////////////////////*/
int* checkUserPosition(int* playerMove, Board& playingBoard, Player currentPlayer){
string userMove;
char** gameBoard = playingBoard.getBoard();
int row = playerMove[0];
int column = playerMove[1];
while(column >= playingBoard.getColumn() || gameBoard[row][column] != ' '){
cout<< " Incorrect Move!!! Please enter a correct move ---> ";
getline(cin, userMove);
playerMove = checkUserMoves(userMove, playingBoard);
row = playerMove[0];
column = playerMove[1];
}
return playerMove;
}
/*////////////////////////////////////////////////////////////////
Get the number of players from the user
- verify the input is correct and it is between MIN and MAX
values given
Return the number of players
//////////////////////////////////////////////////////////////////*/
int getNumberOfPlayer(){
string numberOfPlayers;
int numberOfPlayers_int;
cout<<" Please enter the number of players. choose from 2 to 5 players ---> ";
getline(cin, numberOfPlayers);
numberOfPlayers_int = numberOfPlayers[0] - '0';
while (numberOfPlayers.length()!= 1 ||
numberOfPlayers_int < MIN_NUMBER_PLAYERS ||
numberOfPlayers_int > MAX_NUMBER_PLAYERS)
{
cout<<" Incorrect input! Please enter the number of players ---> ";
getline(cin, numberOfPlayers);
numberOfPlayers_int = numberOfPlayers[0] - '0';
}
numberOfPlayers_int = numberOfPlayers[0] - '0';
return numberOfPlayers_int;
}
/*////////////////////////////////////////////////////////////////
Get the number of players
- Create an array of players by using the number of players
- Initialize the the player piece and stat
Return the number of players
//////////////////////////////////////////////////////////////////*/
Player* create_Players(int numberOfPlayers){
Player* gamePlayers = nullptr;
gamePlayers = new Player[numberOfPlayers];
for (int i = 0; i < numberOfPlayers; i++){
gamePlayers[i].piece = 'a' + i;
gamePlayers[i].gamesWon = 0;
gamePlayers[i].gamesLoss = 0;
gamePlayers[i].gamesDrawn = 0;
}
return gamePlayers;
}
/*////////////////////////////////////////////////////////////////
Get the number of players
- Ask the user for their name
- check for if there is any mistake or error
- capitalize the first letter of the every part of the name
Return the final full name
//////////////////////////////////////////////////////////////////*/
string getName(int player_number){
string user_Name;
cout<< endl<<" Please Enter Name for Player "<< player_number + 1 << " ---> ";
getline(cin, user_Name);
user_Name = remove_spaces(user_Name);
user_Name = toLowerCaseString(user_Name);
while(check_Characters(user_Name) || !checkNoLastName(user_Name)){
cout<< " Incorrect Input!!! Please Enter correct name for Player"<< player_number +1 << " ---> ";
getline(cin, user_Name);
user_Name = remove_spaces(user_Name);
user_Name = toLowerCaseString(user_Name);
}
user_Name = changeToCapital(user_Name);
return user_Name;
}
/*////////////////////////////////////////////////////////////////
Get the user name
- verify if the input has only characters
Return a boolean after checking
//////////////////////////////////////////////////////////////////*/
bool check_Characters(string user_Name){
bool isNotCharacter;
int numOfSpace = 0;
isNotCharacter = false;
for (int i = 0; i < user_Name.length(); i++){
if ((user_Name[i] < 'a' || user_Name[i] >'z') && user_Name[i] != ' '){
isNotCharacter = true;
}
if (user_Name[i] == ' '){
numOfSpace++;
}
}
if (user_Name.length() < 1){
string* multiplyNumbers(string number1, string number2);
isNotCharacter = true;
}
if (numOfSpace == 0){
isNotCharacter = true;
}
return isNotCharacter;
}
/*////////////////////////////////////////////////////////////////
Get the user name
- verify if the input has a last name
Return a boolean after checking
//////////////////////////////////////////////////////////////////*/
bool checkNoLastName(string userName){
bool hasLastName = false;
int indexOfString = 0;
userName = toLowerCaseString(userName);
while ((indexOfString < userName.length()) && !hasLastName){
if (userName[indexOfString] == ' '){
if (userName[indexOfString + 1] >= 'a' && userName[indexOfString + 1] <= 'z'){
hasLastName = true;
}
}
indexOfString++;
}
return hasLastName;
}
/*////////////////////////////////////////////////////////////////
Get the user name
- change every letter to small letter
Return a boolean after checking
//////////////////////////////////////////////////////////////////*/
string toLowerCaseString(string user_Name){
string lowerCase;
for (int i = 0; i < user_Name.length(); i++){
lowerCase += toLower(user_Name[i]);
}
return lowerCase;
}
/*////////////////////////////////////////////////////////////////
Get the user name
- remove any extra spaces in the name
Return the name
//////////////////////////////////////////////////////////////////*/
string remove_spaces(string Player_name){
string full_Name="";
bool is_Firstletter = false;
int Space_count = 0;
for (int i = 0; i < Player_name.length(); i++){
if (Player_name[i] != ' ' || is_Firstletter){
full_Name += Player_name[i];
is_Firstletter = true;
}
}
Player_name = full_Name;
full_Name = "";
for (int i = 0; i < Player_name.length(); i++){
if (Player_name[i] == ' ' && Space_count == 0){
full_Name += " ";
Space_count++;
}
if (Player_name[i] != ' '){
full_Name += Player_name[i];
Space_count=0;
}
}
return full_Name;
}
/*////////////////////////////////////////////////////////////////
Get the user name
- capitalize the first letter
Return final letter
//////////////////////////////////////////////////////////////////*/
string changeToCapital(string userName){
string name;
name += toUpper(userName[0]);
for (int i = 1; i < userName.length(); i++){
if (userName[i-1] == ' '){
name += toUpper(userName[i]);
}
else{
name += toLower(userName[i]);
}
}
return name;
}
/*////////////////////////////////////////////////////////////////
Get the user name
- get the first name from the name
- move over the string until you get a space
Return the final string as the first name
//////////////////////////////////////////////////////////////////*/
string getFirstName(string userName){
string firstName;
int indexofString;
indexofString = 0;
while (userName[indexofString] != ' '){
firstName += userName[indexofString];
if(indexofString == userName.length()){
break;
}
indexofString++;
}
return firstName;
}
/*////////////////////////////////////////////////////////////////
Get the Max size, trigger if it is row or column
- get the row or column size
- verify if the input is correct
Return the final size
//////////////////////////////////////////////////////////////////*/
int getSize(const int MAX_SIZE, char rowOrCol){
int final_size;
string userSizeInput;
if(rowOrCol == 'r'){
cout << " Enter the number of rows -> ";
}
else{
cout << " Enter the number of columns -> ";
}
getline(cin, userSizeInput);
if (userSizeInput.length() == 1){
final_size = userSizeInput[0] - '0';
}
if (userSizeInput.length() == 2){
final_size = (userSizeInput[0] - '0') * 10 + (userSizeInput[1] - '0');
}
while (final_size < MIN_ROW_SIZE || final_size > MAX_SIZE ||
userSizeInput.length() < 1 || userSizeInput.length() > 2){
cout<< " Incorrect Input!!! Please enter the correct size ---> ";
getline(cin, userSizeInput);
if (userSizeInput.length() == 1){
final_size = userSizeInput[0] - '0';
}
if (userSizeInput.length() == 2){
final_size = (userSizeInput[0] - '0') * 10 + (userSizeInput[1] - '0');
}
}
return final_size;
}
/*////////////////////////////////////////////////////////////////
Gets the players who won and number of players
- updates each player's draw if it is a draw
- updates win and losses if there is a win
//////////////////////////////////////////////////////////////////*/
void update_Status(Player* gamePlayers, int turn, int numberOfPlayers){
for (int i = 0; i < numberOfPlayers; i++){
if (turn == numberOfPlayers){
gamePlayers[i].gamesDrawn++;
}
if (i == turn){
gamePlayers[i].gamesWon++;
}
if ((turn != numberOfPlayers) && (i != turn)){
gamePlayers[i].gamesLoss++;
}
}
}
/*////////////////////////////////////////////////////////////////
Gets the game players and number of players
Displays final stat
//////////////////////////////////////////////////////////////////*/
void printStat(Player* gamePlayers, int numberOfPlayers){
string longest_name = "";
for (int i = 0 ; i < numberOfPlayers; i++){
if( (gamePlayers[i].fullName).length() > longest_name.length()){
longest_name = gamePlayers[i].fullName;
}
}
cout<<" "<< setw(longest_name.length() + 1)<< " " << " ------ ------ ------"<< endl;
cout<<" "<< setw(longest_name.length() + 1)<< " " << "| WIN | Loss | Draw |"<< endl;
cout<<" "<< setw(longest_name.length() + 1)<< " " << " ------ ------ ------"<< endl;
for (int i = 0; i < numberOfPlayers; i++){
cout<<" "<< setw(longest_name.length())<< gamePlayers[i].fullName<< " | ";
cout<<setw(4) << gamePlayers[i].gamesWon<< " | "<< setw(4) << gamePlayers[i].gamesLoss;
cout<< " | "<< setw(4) << gamePlayers[i].gamesDrawn <<" | "<< endl;
cout<< " "<<setw(longest_name.length())<< " " << " ------ ------ ------"<< endl;
}
}
/*////////////////////////////////////////////////////////////////
Prints the Opening guide
//////////////////////////////////////////////////////////////////*/
void openningStatement(){
cout<< endl;
cout<<" Welcome Players to Tic Tac Toe!!!"<< endl;
cout<<" Here you can play with 2 to 5 people in this game."<< endl;
cout<<" The size of the board can be between 3 x 3 to 11 x 14. " << endl;
cout<< endl<<" You can enter players names before the game however the names " << endl;
cout<<" has to be in characters (NO NUMBERS OR SYMBOLS!!!) and must have ";
cout<<"first and last name." << endl;
cout<<endl<<" To make a move enter the row letter then the column number Example: A10"<< endl;
cout<<" You can play this game Multiple times with changing the board size "<< endl;
cout<<" after every game. Know that you cannot increase or decrease the number";
cout<<" every game."<< endl;
cout<<endl<< endl<<" Enjoy the game!!!" << endl;
}