-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic-Tac-Toe.cpp
219 lines (205 loc) · 4.07 KB
/
Tic-Tac-Toe.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
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
class Game
{
private:
enum class Player
{
Human = 'X',
Computer = 'O',
None = '\0'
};
struct Move
{
unsigned int x = 0;
unsigned int y = 0;
};
Player Board[3][3];
public:
//Constructor
Game()
{
for (unsigned int i = 0; i < 3; i++)
{
for (unsigned int j = 0; j < 3; j++)
{
Board[i][j] = Player::None;
}
}
}
void printBoard() const
{
system("cls");
cout << " ----Tic Tac Toe----\n\n Human : X\n AI : O\n" << endl;
cout << " 0 1 2" << endl;
cout << " -------------------";
for (unsigned int i = 0; i < 3; i++)
{
cout << "\n "<<i<<" "<< "|";
for (unsigned int j = 0; j < 3; j++)
{
cout << setw(3) << static_cast<char>(Board[i][j]) << setw(3) << " |";
}
cout << "\n -------------------";
}
cout << "\n\n";
}
bool isTie() const
{
for (unsigned int i = 0; i < 3; i++)
{
if (Board[i][0] == Player::None || Board[i][1] == Player::None || Board[i][2] == Player::None)
return false;
}
return true;
}
bool checkWin(Player _player) const
{
for (unsigned int i = 0; i < 3; i++)
{
if (Board[i][0] == _player && Board[i][1] == _player && Board[i][2] == _player)
return true;
if (Board[0][i] == _player && Board[1][i] == _player && Board[2][i] == _player)
return true;
}
if (Board[0][0] == _player && Board[1][1] == _player && Board[2][2] == _player)
return true;
if (Board[0][2] == _player && Board[1][1] == _player && Board[2][0] == _player)
return true;
return false;
}
Move minimax()
{
int score = numeric_limits<int>::max();
Move move;
for (unsigned int i = 0; i < 3; i++)
{
for (unsigned int j = 0; j < 3; j++)
{
if (Board[i][j] == Player::None)
{
Board[i][j] = Player::Computer;
int temp = maxSearch();
if (temp < score)
{
score = temp;
move.x = i;
move.y = j;
}
Board[i][j] = Player::None;
}
}
}
return move;
}
int maxSearch()
{
if (checkWin(Player::Human)) { return 10; }
else if (checkWin(Player::Computer)) { return -10; }
else if (isTie()) { return 0; }
int score = numeric_limits<int>::min();
for (unsigned int i = 0; i < 3; i++)
{
for (unsigned int j = 0; j < 3; j++)
{
if (Board[i][j] == Player::None)
{
Board[i][j] = Player::Human;
score = max(score, minSearch());
Board[i][j] = Player::None;
}
}
}
return score;
}
int minSearch()
{
if (checkWin(Player::Human)) { return 10; }
else if (checkWin(Player::Computer)) { return -10; }
else if (isTie()) { return 0; }
int score = numeric_limits<int>::max();
for (unsigned int i = 0; i < 3; i++)
{
for (unsigned int j = 0; j < 3; j++)
{
if (Board[i][j] == Player::None)
{
Board[i][j] = Player::Computer;
score = min(score, maxSearch());
Board[i][j] = Player::None;
}
}
}
return score;
}
void humanTurn()
{
bool Status = true;
unsigned int x = -1, y = -1;
char c;
do
{ //Getting the move coordinates from the user
cout << " Your Move: ";
cin >> c;
x = c - '0';
cin >> c;
y = c - '0';
Status = Board[x][y] != Player::None;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
} while (Status);
Board[x][y] = Player::Human;
}
void MainFunc()
{
unsigned int Turn = 0;
bool Status = false;
printBoard();
do
{
if (Turn == 0)
{
humanTurn();
if (checkWin(Player::Human))
{
printBoard();
cout << " Human Wins\n";
Status = true;
}
else printBoard();
}
else
{
Move aimove = minimax();
Board[aimove.x][aimove.y] = Player::Computer;
if (checkWin(Player::Computer))
{
printBoard();
cout << " Computer Wins\n";
Status = true;
}
else printBoard();
}
if (isTie())
{
printBoard();
cout << " Tie\n";
Status = true;
}
Turn ^= 1;
} while (!Status);
}
};
int main()
{
char again;
do
{
Game g;
g.MainFunc();
cout << "\n PLAY AGAIN ? [Y/N] : ";
cin >> again;
} while (again=='Y'||again=='y');
}