-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
259 lines (203 loc) · 5.05 KB
/
Copy pathmain.cpp
File metadata and controls
259 lines (203 loc) · 5.05 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
#include <iostream>
#include <vector>
using namespace std;
char pieces[3] = {'X', 'O', ' '};
int board[9] = {
2, 2, 2,
2, 2, 2,
2, 2, 2
};
int players_wins[2] = { 0, 0 };
int current_player = 0;
bool finished = false;
void endGame();
void startGame();
// Main
int main()
{
system("CLS");
startGame();
return 0;
}
// My Infos:
void printHeader()
{
cout << string(30, '*') << endl;
cout << "** C++ TicTacToe by: AnsjDev\n";
cout << string(30, '*') << endl
<< endl;
}
void printGitHub()
{
cout << "\n\n** Like this repository! \n** AnsjDev GitHub: https://github.com/ansjdev\n\n\n";
}
// Infos
void printPlayersScoreboard()
{
cout << "** Score: \n";
for (int i = 0; i < 2; i++)
{
cout << "- Total PLAYER " << i+1 << " WINS: " << players_wins[i] << endl;
}
}
void printStartInfo()
{
system("CLS");
printHeader();
cout << "Player 1: " << pieces[0] << endl;
cout << "Player 2: " << pieces[1] << endl << endl;
cout << "** Starting game....\n\n";
printPlayersScoreboard();
}
// Win condition
vector<int> getWin()
{
// Check Horizontal Win
for (int i = 0; i < 9; i += 3)
{
if (board[i] == board[i + 1] && board[i + 1] == board[i + 2] && board[i] != 2)
{
return {i, i + 1, i + 2};
}
}
// Check Vertical Win
for (int i = 0; i < 3; i++)
{
if (board[i] == board[i + 3] && board[i + 3] == board[i + 6] && board[i] != 2)
{
return {i, i + 3, i + 6};
}
}
// Check Diagonal win
if (board[0] == board[4] && board[4] == board[8] && board[0] != 2)
{
return {0, 4, 8};
}
if (board[2] == board[4] && board[4] == board[6] && board[2] != 2)
{
return {2, 4, 6};
}
return {};
}
int check_win()
{
return getWin().empty() ? 0 : 1;
}
// Board
void drawBoard(int fill = 1)
{
char new_board[9];
for (int i = 0; i < 9; i++)
{
if(board[i] == 2) {
new_board[i] = fill == 1 ? i+49 : ' ';
} else {
new_board[i] = pieces[board[i]];
}
}
cout << "\n | | \n";
cout << " " << new_board[0] << " | " << new_board[1] << " | " << new_board[2] << endl;
cout << "_____|_____|_____\n";
cout << " | | \n";
cout << " " << new_board[3] << " | " << new_board[4] << " | " << new_board[5] << endl;
cout << "_____|_____|_____\n";
cout << " | | \n";
cout << " " << new_board[6] << " | " << new_board[7] << " | " << new_board[8] << endl;
cout << " | | \n\n";
}
void drawWinBoard()
{
vector<int> win = getWin();
if(win.empty()) return;
for (int &i : board) i = 2;
for (int i = 0; i < 3; i++)
{
board[win[i]] = current_player;
}
drawBoard(0);
}
// Checks
int checkValidInput(int input)
{
if (input < 1 || input > 9 || cin.fail())
{
while (true) {
if (cin.fail() || input < 1 || input > 9) {
cin.clear();
cin.ignore(1000, '\n');
cout << "Your input isn't valid." << endl;
cout << "Select number between 1 and 9: ";
cin >> input;
} else {
return input;
}
}
}
return input;
}
void checkValidSlot(int slot)
{
if (board[slot - 1] != 2)
{
while (true) {
if (board[slot - 1] != 2) {
cin.clear();
cin.ignore(1000, '\n');
cout << "That slot isn't empty.\n";
cout << "Select an empty slot: ";
cin >> slot;
} else {
break;
}
}
}
}
// Game
void runGame()
{
cout << "\nCurrent player turn: PLAYER " << current_player + 1 << " [ " << pieces[current_player] << " ]" << endl
<< endl;
int input;
cout << "Select number between 1 and 9: ";
cin >> input;
int slot = checkValidInput(input);
checkValidSlot(slot);
board[slot-1] = current_player;
drawBoard();
if (check_win())
{
players_wins[current_player]+=1;
cout << "\n\n** PLAYER " << current_player + 1 << " WIN!\n";
drawWinBoard();
printPlayersScoreboard();
finished = true;
return;
}
current_player = current_player == 0 ? 1 : 0;
}
void startGame()
{
finished = false;
printHeader();
printStartInfo();
drawBoard();
while (!finished)
{
runGame();
}
endGame();
}
void endGame()
{
string input;
cout << "\n\n** Continue the game? (yes) or (no): ";
cin >> input;
if (input == "yes")
{
for (int &i : board) i = 2;
startGame();
} else {
cout << "\n\n** Thanks for playing :)";
printGitHub();
}
}