-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask8.cpp
306 lines (266 loc) · 6.94 KB
/
Task8.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
//PvP TicTacToe Game
#include <iostream>
#include <exception>
#include <vector>
#include<Windows.h>
#include <algorithm>
#include <time.h>
/*************************************Defines for program*************************************************/
#define BAD_INPUT -1
#define EMPTY_FIELD ' '
#define FIELD_SIZE 9
#define PAD_DIVIDER "##"
#define FIELD_DIVIDER "||"
#define SLEEP_PERIOD 400
#define ROWSANDCOLUMNS 3
#define WIN_STREAK 3
#define DIVIDER_BY_THREE 3
#define DIVIDER_BY_FOUR 4
#define DIVIDER_BY_FIVE 5
#define DIVIDER_BY_SIX 6
#define X_TURN 1
#define O_TURN 2
#define X_NAME 'X'
#define O_NAME '0'
/*************************************Namespaces requierd for work****************************************/
using namespace std;
/*************************************Function prototypes*************************************************/
/*
*@[brief]: Function prints gameboard with current Xand0 positions
*@[in]: vector with elements
*/
void Print_Board(vector<char>);
/*
*@[brief]: Function prints pad for choosing positions
*/
void Print_Pad();
/*
*@[brief]: Function announce winner using char parameter which indicates current turn
*@[in]: current turn in char format
*/
void Announce_Winner(char);
/*
*@[brief]: Function checks for correct input position and check for win/draw
*@[in]: refernce to vector that handles all positions, it's needed to add marks on positions, also int position and char turn;
*@[out]: boolean value that indicates if something happend, win, lose .. etc
*/
bool Rules_Check(vector<char>&, int, char);
/*
*@[brief]: Function simply runs all needed functions to start game;
*/
void Start_Game();
/*
*@[brief]: Function check if istream object state is set to failbit, shows error and fix input stream;
*@[in]: istream object needed for fail check;
*@[out]: bool statement that shows function result, true if failbit enabled and false otherwise;
*/
bool isbadinput(istream& in) {
if (in.fail()) {
cout << "Bad input\n";
in.clear();
in.ignore();
return true;
}
return false;
}
/**********************************************************************************************/
void Print_Board(vector<char> board) {
int elem_counter = 0;
cout << FIELD_DIVIDER;
for (auto x : board) {
cout << x << FIELD_DIVIDER;
++elem_counter;
if ((elem_counter % DIVIDER_BY_THREE) == 0) {
cout << "\n";
if (elem_counter != FIELD_SIZE) {
cout << FIELD_DIVIDER;
}
}
}
}
/**********************************************************************************************/
void Print_Pad() {
int elem_counter = 0;
cout << "\n\n\n";
cout << PAD_DIVIDER;
static vector<int> pad{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (auto x : pad) {
cout << x << PAD_DIVIDER;
++elem_counter;
if ((elem_counter % DIVIDER_BY_THREE) == 0) {
cout << "\n";
if (elem_counter != FIELD_SIZE) {
cout << PAD_DIVIDER;
}
}
}
}
/**********************************************************************************************/
void Announce_Winner(char turn) {
cout << "Winner user who played with " << turn << " !!!Congratulations\n";
}
/**********************************************************************************************/
bool Rules_Check(vector<char>& board, int position, char turn) {
int elem_counter = 0;
int row_sum = 0;
vector<int> elem_sums;
int U_val = WIN_STREAK * turn;
if (position < 0) {
cout << "Bad position setting to 0\n";
position = 0;
}
try {
elem_sums.push_back(board.at(0));
elem_sums.push_back(board.at(1));
elem_sums.push_back(board.at(2));
if (board.at(position) == EMPTY_FIELD) {
board.at(position) = turn;
}
else {
bool ret;
bool stop = true;
Sleep(SLEEP_PERIOD);
system("cls");
while (stop) {
cout << "Whoaa sorry but user who play with " << board.at(position) << " already placed there!!\n";
Print_Board(board);
Print_Pad();
cin >> position;
stop = isbadinput(cin);
}
ret = Rules_Check(board, --position, turn);
return ret;
}
}
catch(exception& ex){
cout << "Error: " << ex.what() << " can't handle exception closing program\n";
exit(true);
}
for (auto x : board) {
++elem_counter;
row_sum += x;
if ((row_sum / DIVIDER_BY_THREE) == turn) {
Sleep(SLEEP_PERIOD);
system("cls");
Print_Board(board);
Announce_Winner(turn);
return true;
}
if ((elem_counter % DIVIDER_BY_THREE) == 0) {
row_sum = 0;
}
}
elem_counter = 0;
for (auto x : board) {
++elem_counter;
try {
if ((elem_counter % DIVIDER_BY_FOUR) == 0) {
elem_sums.at(0) += x;
}
if ((elem_counter % DIVIDER_BY_FIVE) == 0) {
elem_sums.at(1) += x;
}
if ((elem_counter % DIVIDER_BY_SIX) == 0) {
elem_sums.at(2) += x;
elem_counter = ROWSANDCOLUMNS;
}
}
catch (exception& ex) {
cout << "Error: " << ex.what() << " can't handle exception closing program\n";
exit(true);
}
}
vector<int>::iterator winner = find(elem_sums.begin(), elem_sums.end(), U_val);
if (winner != elem_sums.end()) {
Sleep(SLEEP_PERIOD);
system("cls");
Print_Board(board);
Announce_Winner(turn);
return true;
}
try {
elem_counter = 0;
elem_sums.at(0) = 0;
elem_sums.at(1) = 0;
for (int k = 0, j = 2; elem_counter < ROWSANDCOLUMNS; k += 4, j += 2, elem_counter++) {
elem_sums.at(0) += board.at(k);
elem_sums.at(1) += board.at(j);
}
}
catch (exception& ex) {
cout << "Error: " << ex.what() << " can't handle exception closing program\n";
exit(true);
}
winner = find(elem_sums.begin(), elem_sums.end(), U_val);
if (winner != elem_sums.end()) {
Sleep(SLEEP_PERIOD);
system("cls");
Print_Board(board);
Announce_Winner(turn);
return true;
}
vector<char> ::iterator draw = find(board.begin(), board.end(), EMPTY_FIELD);
if (draw == board.end()) {
cout << "Draw!!!\n";
return true;
}
return false;
}
/**********************************************************************************************/
void Start_Game() {
vector<char> board(FIELD_SIZE, EMPTY_FIELD);
int position;
int swapper = 0;
char turn;
bool returned = false;
while (!swapper) {
Sleep(SLEEP_PERIOD);
system("cls");
cout << "Who is playing first: X or 0?: ";
cin >> turn;
switch (toupper(turn)) {
case 'X':
swapper = X_TURN;
break;
case '0':
swapper = O_TURN;
break;
default:
cout << "Bad input!!\n";
break;
}
}
while (!returned) {
Sleep(SLEEP_PERIOD);
system("cls");
Print_Board(board);
Print_Pad();
cin >> position;
if (isbadinput(cin)) {
position = BAD_INPUT;
}
switch (--position) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
turn = (swapper == X_TURN ? X_NAME : O_NAME);
swapper = (swapper == X_TURN ? O_TURN : X_TURN);
returned = Rules_Check(board, position, turn);
break;
default:
cout << "Bad input\n";
break;
}
}
}
/**********************************************************************************************/
void main() {
Start_Game();
}
/**********************************************************************************************/