-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
451 lines (418 loc) · 13.1 KB
/
main.cpp
File metadata and controls
451 lines (418 loc) · 13.1 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
//
// main.cpp
// Tic Tac Toe
//
// Created by Muriel Dulieu on 8/15/14.
// Copyright (c) 2014 Muriel Dulieu. All rights reserved.
//
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <cctype>
class TicTacToe{
int table[9]; // Table in which the game is played. 0 means the position is empty, 1 means 0, 4 means X
int ComputerGetsXorO; // 0 means it has not been initialized, 1 means the computer plays O and plays second, 4 means the computer plays X and plays first
int turns; // the number of turns left
int WhoWins; // 0 means the game is still going or nobody won, 1 means the computer wins, 2 means the guest wins
public:
TicTacToe(); // initialize all entries of table[9] to 0
void Show(); // Show the table
void ShowPositions(); // Show the numbers corresponding to the positions in the table
void Play(); // play one game
void Reset(); // reinitialize the game
void WhoPlayFirst(); // decide randomly who starts playing
inline int WhoWinsTheGame() {return WhoWins;}
};
int main(int argc, const char * argv[])
{
using std::cout;
using std::endl;
using std::cin;
int NumberWinsComputer = 0;
int NumberWinsGuest = 0;
TicTacToe Game;
char PlayAgain;
cout << "Do you want to play a game of Tic Tac Toe?" << endl << "If yes type \'y\' else type \'n\': ";
cin >> PlayAgain;
cout << endl;
while (tolower(PlayAgain) == 'y')
{
Game.Reset();
Game.Play();
if (Game.WhoWinsTheGame() == 1)
++ NumberWinsComputer;
else if (Game.WhoWinsTheGame() == 2)
++ NumberWinsGuest;
cout << "Do you want to play another game? If yes type \'y\' else type \'n\': ";
cin >> PlayAgain;
cout << endl;
}
cout << "I won " << NumberWinsComputer << " game(s) and you won " << NumberWinsGuest << " game(s)." << endl;
cout << "Thank you for playing!" << endl;
return 0;
}
TicTacToe::TicTacToe()
{
Reset();
}
void TicTacToe::Show()
{
using std::cout;
using std::endl;
for (int i = 0; i < 3; ++i)
{
cout << " ------------- " << endl;
for (int j = 0; j < 3; ++j)
{
cout << " | ";
if (table[(i * 3) + j] == 0)
cout << " ";
else if (table[(i * 3) + j] == 1)
cout << "O";
else if (table[(i * 3) + j] == 4)
cout << "X";
}
cout << " | " << endl;
}
cout << " ------------- " << endl;
}
void TicTacToe::ShowPositions()
{
using std::cout;
using std::endl;
cout << endl;
for (int i = 0; i < 3; ++i)
{
cout << " ------------- " << endl;
for (int j = 0; j < 3; ++j)
{
cout << " | ";
cout << (i * 3) + j + 1;
}
cout << " | " << endl;
}
cout << " ------------- " << endl << endl;
}
void TicTacToe::Reset()
{
for (int i = 0; i < 9; ++i)
table[i] = 0;
ComputerGetsXorO = 0;
turns = 9;
WhoWins = 0;
}
void TicTacToe::Play()
{
using std::cout;
using std::endl;
using std::cin;
cout << "Below is your tic tac toe table with the numbers corresponding to the positions in the table: " << endl;
ShowPositions();
WhoPlayFirst();
srand (time(NULL));
int ComputerPosition = rand() % turns; // to choose randomly where the computer is going to play
if(ComputerGetsXorO == 4)
{
table[ComputerPosition] = ComputerGetsXorO;
-- turns;
}
Show();
int GuestPosition = 0;
bool tryAgain = true;
bool GuestPlays = true;
int GuestGetsXorO = (ComputerGetsXorO + 3) % 6;
while (turns > 0)
{
cout << endl;
if (GuestPlays)
{
do // guest plays
{
cout << "Your turn! \nType the number corresponding to the position in which you want to play: ";
std::cin >> GuestPosition;
if (GuestPosition > 0 && GuestPosition <= 9 && table[GuestPosition - 1] == 0)
{
tryAgain = false;
}
else if (GuestPosition > 0 && GuestPosition <= 9 && table[GuestPosition - 1] != 0)
{
cout << "The position you chose is already occupied, try again!" << endl;
cin.get();
}
else
{
cout << "You did not type a number between 1 and 9, try again!" << endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}while(tryAgain);
tryAgain = true;
table[GuestPosition - 1] = (ComputerGetsXorO + 3) % 6;
}
else
{
cout << "My turn: " << endl;
// check if computer has two signs in one row/column/diagonal with a third position empty so he wins in this turn
if (table[0] + table[1] + table[2] == 2 * ComputerGetsXorO)
{
for (int i = 0; i < 3; ++i)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
-- turns;
Show();
WhoWins = 1;
break;
}
else if (table[3] + table[4] + table[5] == 2 * ComputerGetsXorO)
{
for (int i = 3; i < 6; ++i)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
-- turns;
Show();
WhoWins = 1;
break;
}
else if (table[6] + table[7] + table[8] == 2 * ComputerGetsXorO)
{
for (int i = 6; i < 9; ++i)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
-- turns;
Show();
WhoWins = 1;
break;
}
else if (table[0] + table[3] + table[6] == 2 * ComputerGetsXorO)
{
for (int i = 0; i < 9; i += 3)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
-- turns;
Show();
WhoWins = 1;
break;
}
else if (table[1] + table[4] + table[7] == 2 * ComputerGetsXorO)
{
for (int i = 1; i < 9; i += 3)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
-- turns;
Show();
WhoWins = 1;
break;
}
else if(table[2] + table[5] + table[8] == 2 * ComputerGetsXorO)
{
for (int i = 2; i < 9; i += 3)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
-- turns;
Show();
WhoWins = 1;
break;
}
else if (table[0] + table[4] + table[8] == 2 * ComputerGetsXorO)
{
for (int i = 0; i < 9; i += 4)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
-- turns;
Show();
WhoWins = 1;
break;
}
else if (table[2] + table[4] + table[6] == 2 * ComputerGetsXorO)
{
for (int i = 2; i < 9; i += 4)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
-- turns;
Show();
WhoWins = 1;
break;
}
// check if guest has two signs in one row/column/diagonal to block it
else if (table[0] + table[1] + table[2] == 2 * GuestGetsXorO)
{
for (int i = 0; i < 3; ++i)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
}
else if (table[3] + table[4] + table[5] == 2 * GuestGetsXorO)
{
for (int i = 3; i < 6; ++i)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
}
else if (table[6] + table[7] + table[8] == 2 * GuestGetsXorO)
{
for (int i = 6; i < 9; ++i)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
}
else if (table[0] + table[3] + table[6] == 2 * GuestGetsXorO)
{
for (int i = 0; i < 9; i += 3)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
}
else if (table[1] + table[4] + table[7] == 2 * GuestGetsXorO)
{
for (int i = 1; i < 9; i += 3)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
}
else if(table[2] + table[5] + table[8] == 2 * GuestGetsXorO)
{
for (int i = 2; i < 9; i += 3)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
}
else if (table[0] + table[4] + table[8] == 2 * GuestGetsXorO)
{
for (int i = 0; i < 9; i += 4)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
}
else if (table[2] + table[4] + table[6] == 2 * GuestGetsXorO)
{
for (int i = 2; i < 9; i += 4)
{
if (table[i] == 0)
{
table[i] = ComputerGetsXorO;
break;
}
}
}
else
{
// Guest does not have two signs in one row/column/diagonal that need to be blocked
ComputerPosition = rand() % turns;
int i;
for (i = 0; i < 9; ++i)
{
if (table[i] == 0)
{
if (ComputerPosition == 0)
break;
-- ComputerPosition;
}
}
table[i] = ComputerGetsXorO;
}
}
-- turns;
Show();
GuestPlays = !GuestPlays;
// check if the guest wins
if (table[0] + table[1] + table[2] == 3 * GuestGetsXorO || table[3] + table[4] + table[5] == 3 * GuestGetsXorO || table[6] + table[7] + table[8] == 3 * GuestGetsXorO || table[0] + table[3] + table[6] == 3 * GuestGetsXorO || table[1] + table[4] + table[7] == 3 * GuestGetsXorO || table[2] + table[5] + table[8] == 3 * GuestGetsXorO || table[0] + table[4] + table[8] == 3 * GuestGetsXorO || table[2] + table[4] + table[6] == 3 * GuestGetsXorO)
{
WhoWins = 2;
break;
}
}
if (WhoWins == 1)
cout << "I win!";
else if (WhoWins == 2)
cout << "You win!";
else
cout << "Nobody wins!";
cout << endl;
}
void TicTacToe::WhoPlayFirst()
{
using std::cout;
using std::endl;
cout << "Let's see who plays first: ";
srand (time(NULL));
int guestFirst = rand() % 2;
if (guestFirst == 0)
{
ComputerGetsXorO = 4;
cout << "I play first!";
}
else
{
ComputerGetsXorO = 1;
cout << "You play first!";
}
cout << endl;
}