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
| #include <iostream>
#include <string>
using namespace std;
class TicTacToe
{
private:
char table[3][3];
public:
TicTacToe() { }
//insert numbers
void setTable()
{
int n = 1;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
table[i][j] = '0' + n;//cast n to char
n++;
}
}
}
//output table to screen
void printTable()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
if(j < 2) { cout << table[i][j] << "|"; }
else { cout << table[i][j] << endl; }
if(i < 2) { cout << "-+-+-\n"; }
}
}
//player move and set to X or O
void playerMove(char num, char player)
{
bool wrongMove = true;//wrong move
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{//mark square with X or O
if(table[i][j] == num) { table[i][j] = player; wrongMove = false; }
}
}
if(wrongMove == true) { cout << "Wrong move!\n"; }
}
//cheking for winers
bool checkWinner(char player, bool gameOver)
{
//winning conditions
for(int i = 0; i < 3; i++)//checking rows
if(table[i][0] == table[i][1] && table[i][1] == table[i][2]) gameOver = true;
for(int i = 0; i < 3; i++)//checking columns
if(table[0][i] == table[1][i] && table[1][i] == table[2][i]) gameOver = true;
//diagonals
if(table[0][0] == table[1][1] && table[1][1] == table[2][2]) gameOver = true;
if(table[0][2] == table[1][1] && table[1][1] == table[2][0]) gameOver = true;
if(gameOver == true)
{
cout << "Player " << player << " wins!\n\n";
}
return gameOver;
}
//checking for a draw
bool checkDraw(bool gameOver)
{
int n = 1, count = 0;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
//cheking if board is full
if(table[i][j] == '0'+n) { count++; }
n++;
}
}
if(count < 1) { cout << "It's a draw!\n\n"; gameOver = true; }
return gameOver;
}
};
int main()
{
bool done = false, gameOver = false;
char player = 'O', num;
TicTacToe myGame;
myGame.setTable();
do
{
if(player == 'X') { player = 'O'; }
else { player = 'X'; }
myGame.printTable();
cout << "Player \"" << player << "\" turn or (q) to quit> ";
cin >> num;
cout << "\n";
if(num == 'q') { cout << "Goodbye!...\n"; break; }
myGame.playerMove(num, player);
gameOver = myGame.checkWinner(player, gameOver);
gameOver = myGame.checkDraw(gameOver);
//if game over, starting over
if(gameOver == true)
{ myGame.setTable();
gameOver = false;
}
}while(!done);
system("pause");
return 0;
} |