-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
146 lines (125 loc) · 3.34 KB
/
game.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
#pragma once
#include "table.hpp"
/**
* @brief The game is processed here.
*
* @param none
*
* @return nothing
*/
void Table::game()
{
if (SIZE % 2 != 0 || SIZE <= 2)
{
cout << "The size of the board is not appropriate";
sleep(2000);
return;
}
if (BLACK == WHITE ||
BLACK == EMPTY ||
BLACK == LEGAL ||
WHITE == EMPTY ||
WHITE == LEGAL ||
EMPTY == LEGAL)
{
cout << "The characters are not appropriate";
sleep(2000);
return;
}
// offering some game modes to the user
settings();
cout << " ";
// the game starts
coor c;
while (true)
{
printBoard();
if (!hasTileToFlip())
break;
// print the side to indicate which side is playing
if (getTurn() == BLACK)
cout << "\nBlack\n";
else
cout << "\nWhite\n";
// getting coordinates from the players
// if the choice is 1, user plays against computer
if (getGameMode() == HUMAN_VS_CPU)
{
if (getTurn() == getUserSide())
{
c = userPlays();
}
else
{
c = cpuPlays(500);
}
}
// if the choice is 2, user will play multiplayer
if (getGameMode() == HUMAN_VS_HUMAN)
{
c = userPlays();
}
// if the choice is 3, only computer will play
if (getGameMode() == CPU_VS_CPU)
{
c = cpuPlays(500);
}
// if the choice is 4, the game will load from a file
if (getGameMode() == LOAD_GAME)
{
c = filePlays(500);
}
// legal tiles are flipped here
flipTiles(c);
// switching the side when a move is done
switchTurn();
// in case there is no legal move for the next user the side is switched
if (!hasTileToFlip())
{
switchTurn();
}
}
// to count how many black and white squares there are
int blacks = 0;
int whites = 0;
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
if (getBoard({row, col}) == BLACK)
blacks++;
if (getBoard({row, col}) == WHITE)
whites++;
}
}
cout << "\n\nG A M E O V E R\n\n";
// print the final result
if (blacks == whites)
cout << "DRAW";
else
{
if (blacks > whites)
cout << "Player black won." << endl;
else
cout << "Player white won." << endl;
cout << "SCORE" << endl
<< "Black: " << blacks << endl
<< "White: " << whites;
}
// ask the user if he wants to save it to a file
if (getGameMode() != LOAD_GAME)
{
cout << "\n\nDo you want to save the game to a file? (y/n) ";
char choice;
choice = getche();
if (choice == 'y' || choice == 'Y')
{
cout << "\nEnter the file name: ";
string fileName;
cin >> fileName;
saveMoves(fileName);
}
}
cout << "\nPress any key to quit";
getch();
}