-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.cpp
266 lines (229 loc) · 5.45 KB
/
TicTacToe.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
#include<iostream>
#include "TicTacToe.h"
#include <random>
using namespace std;
ComputerLevel* ComputerLevel::Create(int level)
{
switch(level)
{
case 1:
return new Beginer();
case 2:
return new Smart();
default:
return new Smart();
}
}
Player* Player::Create(char player, char mark)
{
if (player == 'H')
return new Human(mark);
else if (player == 'C')
return new Computer(mark);
}
Computer::Computer(char mark) :Player("Computer", mark)
{
setupLevel();
}
void Computer::setupLevel()
{
int level = 2;
cout << "Comuter level : \n 1. Beginer \n 2. Smart: \n Your Choice: - ";
cin >> level;
if (level < 1 || level > 2)
cout<<"wrong input, taking default as Smart";
m_level = ComputerLevel::Create(level);
}
Computer::~Computer()
{
delete m_level;
}
int Computer::Play(Game* game)
{
int selectedBox = m_level->selectBox(game);
cout <<"\nComputer Selected "<<selectedBox<< " Box";
return selectedBox;
}
int Beginer::selectBox(Game* game)
{
//choose a random box.
int selectedBox = rand()%9 + 1;
while(!game->Validate(selectedBox))
{
selectedBox = rand()%9 + 1;
}
return selectedBox;
}
int Smart::selectBox(Game* game)
{
int i = 1;
char player1mark = game->getPlayer1Mark();
char player2mark = game->getPlayer2Mark();
//1. Check if Computer is winning in this move
for (i = 1; i < 10; ++i)
{
if(game->Validate(i))
{
game->setMark(i, player2mark);
if(game->IsGameover())
return i;
else
game->setMark(i, i + '0');
}
}
// 2. Check if other player is winning in next move, if so, stop him.
for (i = 1; i < 10; ++i)
{
if(game->Validate(i))
{
game->setMark(i, player1mark);
if(game->IsGameover())
return i;
else
game->setMark(i, i + '0');
}
}
// 3. check if a corner 1, 3, 7, 9 is free
if(game->Validate(1)) return 1;
if(game->Validate(3)) return 3;
if(game->Validate(7)) return 7;
if(game->Validate(9)) return 9;
// 4. check if center is free.
if(game->Validate(5)) return 5;
// 5. choose some other box
if(game->Validate(2)) return 2;
if(game->Validate(4)) return 4;
if(game->Validate(6)) return 6;
if(game->Validate(8)) return 8;
}
int Human::Play(Game* game)
{
int selectedBox;
cout << "Select a unfilled box from 1-9: ";
cin >> selectedBox;
while (!(game->Validate(selectedBox)))
{
cout << "Invalid Move, Enter new Option: ";
cin >> selectedBox;
}
return selectedBox;
}
Game::~Game()
{
delete player2;
}
bool Game::setupGame()
{
char OtherplayerType;
cout << "\nPlayer-1 : You";
cout << "\nPlayer-2 : Human(Enter 'H')/Computer(Enter 'C') - ";
cin >> OtherplayerType;
if (OtherplayerType != 'H' && OtherplayerType != 'C')
{
cout << "Wrong choice of player to play with.\n You need to enter 'H' for Human and 'C' for Computer\n";
return false;
}
player1 = Player::Create('H', PLAYER1_MARK);
player2 = Player::Create(OtherplayerType, PLAYER2_MARK);
int nextPlayer;
cout << "\nSelect Player for First Chance : \n 1. Player-1(You) \n 2. Other player: \n Your Choice : - ";
cin >> nextPlayer;
if(nextPlayer == 1)
{
m_nextPlayer = player1;
}
else if (nextPlayer == 2)
{
m_nextPlayer = player2;
}
else
{
cout << "\nWrong choice. Going with default. Your turn first.\n";
m_nextPlayer = player1;
}
for (int i = 0; i < 9; ++i)
gameBoard[i] = '1'+ i;
return true;
}
void Game::Display()
{
cout << endl;
int t = 0;
while (t < 9)
{
if (t == 3 || t == 6)
{
cout << "\n\n";
}
cout << " " << gameBoard[t];
t++;
}
cout << endl << endl;
}
void Game::NextMove()
{
int selectedBox;
cout<<(m_nextPlayer == player1 ? "Your" : "Player-2(" + m_nextPlayer->getPlayerType() + ")") <<" Turn :\n";
selectedBox = m_nextPlayer->Play(this);
gameBoard[selectedBox - 1] = m_nextPlayer->getMark();
}
void Game::Start()
{
Display();
while (!isBoardFull())
{
NextMove();
Display();
if (IsGameover())
{
cout<<(m_nextPlayer == player1 ? "You" : "Player-2(" + m_nextPlayer->getPlayerType() + ")") <<" won :\n";
cout << "\n###########################################\n";
break;
}
m_nextPlayer = m_nextPlayer == player1 ? player2 : player1;
}
}
bool Game::Validate(int position)
{
if ((gameBoard[position - 1] == '0') || (gameBoard[position - 1] == 'X'))
return false;
else
return true;
}
bool Game::isBoardFull()
{
for(int i = 1; i < 10; ++i)
if(Validate(i)) return false;
return true;
}
bool Game::IsGameover()
{
if (((gameBoard[0] == gameBoard[1]) && (gameBoard[1] == gameBoard[2]))
|| ((gameBoard[0] == gameBoard[3]) && (gameBoard[3] == gameBoard[6]))
|| ((gameBoard[0] == gameBoard[4]) && (gameBoard[4] == gameBoard[8]))
|| ((gameBoard[1] == gameBoard[4]) && (gameBoard[4] == gameBoard[7]))
|| ((gameBoard[2] == gameBoard[5]) && (gameBoard[5] == gameBoard[8]))
|| ((gameBoard[2] == gameBoard[4]) && (gameBoard[4] == gameBoard[6]))
|| ((gameBoard[3] == gameBoard[4]) && (gameBoard[4] == gameBoard[5]))
|| ((gameBoard[6] == gameBoard[7]) && (gameBoard[7] == gameBoard[8])))
{
return true;
}
else
return false;
}
int main()
{
char response;
do
{
Game newGame;
if(newGame.setupGame())
newGame.Start();
cout << "\n\nWanna play again(Y/y) : ";
cin >> response;
}
while (response == 'Y' || response == 'y');
getchar();
return 0;
}