C++: Tic Tac Toe
Here is the simple C++ implementation of Tic Tac Toe.
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
#include <iostream>
#include <vector>
void draw_board(const std::vector< std::vector<char> >& vec)
{
std::cout << " 0 1 2 \n";
std::cout << " +---+---+---+\n";
for (int i = 0; i < vec.size(); i++)
{
std::cout << i << " " ;
for (int j = 0; j < vec[i].size(); j++)
{
std::cout << "| " << vec[i][j] << " ";
}
std::cout << "|";
std::cout << '\n';
std::cout << " +---+---+---+\n";
}
}
void enter(unsigned int row, unsigned int col, char ch, std::vector< std::vector<char> >& vec)
{
vec[row][col] = ch;
}
bool check(const std::vector< std::vector<char> >& vec)
{
//to check diagonals
if ((vec[0][0] == 'X' && vec[1][1] == 'X' && vec[2][2] == 'X')
|| (vec[0][2] == 'X' && vec[1][1] == 'X' && vec[2][0] == 'X'))
{
std::cout << "Player X won this game\n";
return true;
}
else if ((vec[0][0] == 'O' && vec[1][1] == 'O' && vec[2][2] == 'O')
|| (vec[0][2] == 'O' && vec[1][1] == 'O' && vec[2][0] == 'O'))
{
std::cout << "Player O won this game\n";
return true;
}
//to check horizonatal and vertical
for (int i = 0; i < vec.size(); i++)
{
if ((vec[i][0] == 'X' && vec[i][1] == 'X' && vec[i][2] == 'X')
|| (vec[0][i] == 'X' && vec[1][i] == 'X' && vec[2][i] == 'X'))
{
std::cout << "Player X won this game\n";
return true;
}
else if((vec[i][0] == 'O' && vec[i][1] == 'O' && vec[i][2] == 'O')
|| (vec[0][i] == 'O' && vec[1][i] == 'O' && vec[2][i] == 'O'))
{
std::cout << "Player O won this game\n";
return true;
}
}
return false;
}
void start(std::vector< std::vector<char> >& vec)
{
unsigned int row, col;
int res = 0;
char ch;
for (int i = 0; i < 9;)
{
if (i == 0 || i%2 == 0)
{
ch = 'X';
std::cout << "Chance to enter X\n";
}
else
{
ch = 'O';
std::cout << "Chance to enter O\n";
}
std::cout << "Enter row number ";
std::cin >> row;
std::cout << "Enter column number ";
std::cin >> col;
if (row < 3 && col < 3)
{
if (vec[row][col] == ' ' || vec[row][col] == ' ')
{
enter(row, col, ch, vec);
draw_board(vec);
if ( i >= 2)
{
res = check(vec);
if (res == 1)
{
break;
}
}
i++;
}
else
{
std::cerr << "This position already contains a character\n";
}
}
else
{
std::cerr << "Enter row number and column number between 0 to 2\n";
}
}
if (res == 0)
{
std::cout << "This game draws\n";
}
}
int main()
{
std::vector< std::vector<char> > board(3, std::vector<char>(3, ' '));
draw_board(board);
start(board);
}
Output