-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
294 lines (271 loc) · 9.45 KB
/
main.cpp
File metadata and controls
294 lines (271 loc) · 9.45 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
#include<iostream>
#include<windows.h> //For using system methods
#include<conio.h> //For using _getch()
#include<ctime> //For setting time for random function
using namespace std;
//Shown at top of every screen
void welcome()
{
system("CLS");
cout << "Welcome to Tic-Tac-Toe by Jay" << endl;
cout << "-----------------------------" << endl << endl;
}
//Instruction function. Shown only first time
void instruction()
{
welcome();
cout << "This is board: " << endl << endl;
cout << " | | " << endl;
cout << " 1 | 2 | 3 " << endl;
cout << " | | " << endl;
cout << "-----------------------" << endl;
cout << " | | " << endl;
cout << " 4 | 5 | 6 " << endl;
cout << " | | " << endl;
cout << "-----------------------" << endl;
cout << " | | " << endl;
cout << " 7 | 8 | 9 " << endl;
cout << " | | " << endl << endl;
cout << "To put your mark press key representing the position from above figure" << endl;
system("pause");
}
//Show playing board with marks
void displayBoard(char posArr[9])
{
cout << endl;
cout << " | | " << endl;
cout << " " << posArr[0] << " | " << posArr[1] << " | " << posArr[2] << " " << endl;
cout << " | | " << endl;
cout << "-----------------------" << endl;
cout << " | | " << endl;
cout << " " << posArr[3] << " | " << posArr[4] << " | " << posArr[5] << " " << endl;
cout << " | | " << endl;
cout << "-----------------------" << endl;
cout << " | | " << endl;
cout << " " << posArr[6] << " | " << posArr[7] << " | " << posArr[8] << " " << endl;
cout << " | | " << endl << endl;
}
//Function to get Humans choice of mark
char getHumanMark()
{
char mark;
welcome();
cout << "Enter your mark -> ";
mark = _getch(); // Used getch() instead of cin so that user need not press enter after input
cout << mark;
return mark;
}
//function to get computers marked
char getCompMark(char hm) // hm = HUman Mark
{
if (hm == 'X' || hm == 'x')
return 'O';
else
return 'X';
}
//One time called function to display marks after selection
void displayMarks(char hm, char cm)
{
welcome();
cout << "Your mark is '" << hm << "' and Computer's mark is '" << cm << "'" << endl;
system("pause");
}
//function to print score of the game
void printScore(int score[3])
{
cout << "Score: Human = " << score[0] << ", Computer = " << score[1] << ", Draw = " << score[2] << endl;
}
//This function clears screen,
//Display welcome message,
//print current score and then display the game board
void displayGame(int score[3], char posArr[9])
{
welcome();
printScore(score);
displayBoard(posArr);
}
//A function to check if anybody is a winner
//Returns mark of the winner or null
char winner(char posArr[9])
{
for(int i = 0; i < 7; i+=3) //Check all rows for same mark
{
if(posArr[i]!=0 && posArr[i+1]!=0 && posArr[i+2]!=0)
if(posArr[i] == posArr[i+1] && posArr[i+1] == posArr[i+2])
return posArr[i];
}
for(int i = 0; i < 3; i++) //Check all columns for same mark
{
if(posArr[i]!=0 && posArr[i+3]!=0 && posArr[i+6]!=0)
if(posArr[i] == posArr[i+3] && posArr[i+3] == posArr[i+6])
return posArr[i];
}
//Check both diagonals
if(posArr[0]!=0 && posArr[4]!=0 && posArr[8]!=0)
if(posArr[0] == posArr[4] && posArr[4] == posArr[8])
return posArr[0];
if(posArr[2]!=0 && posArr[4]!=0 && posArr[6]!=0)
if(posArr[2] == posArr[4] && posArr[4] == posArr[6])
return posArr[2];
//Check for not draw (some empty position)
for(int i = 0; i < 9; i++)
if(posArr[i] == '\0')
return '\0'; //returns null if no one is winner
return 'd'; //Draw
}
//Apply minimax algorith
//Maximise the score for computers turn
//Minimise the score for humans turn
int getScore(char posArr[9], char humanMark, char compMark, int turn, int l)
{
char posArr2[9];
int score, score1;
char win = winner(posArr);
if(win == compMark)
return 100-l;
else if(win == humanMark)
return -(100-l);
else if(win == 'd')
return 0;
if(turn == 0) //Human's turn
{
score1 = 1000;
for (int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
posArr2[j] = posArr[j];
if(posArr2[i] == '\0') //Find an empty space
{
posArr2[i] = humanMark; //Check the score if move is made on that empty space
score = getScore(posArr2, humanMark, compMark, 1, l+1);
if(score < score1)
score1 = score; //Return the minimum score
}
}
}
else //Computers turn
{
score1 = -1000;
for (int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
posArr2[j] = posArr[j];
if(posArr2[i] == '\0') //Find empty space
{
posArr2[i] = compMark; //Check the score if move is made on that empty space
score = getScore(posArr2, humanMark, compMark, 0, l+1);
if(score > score1)
score1 = score; //Return the maximum score
}
}
}
return score1;
}
//Compute the computers move
int compMove(char posArr[9], char humanMark, char compMark)
{
char posArr2[9];
int score[9];
for(int i = 0; i < 9; i++)
{
score[i] = -1000;
}
int maxScore;
for (int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
posArr2[j] = posArr[j];
if(posArr2[i] == '\0') //Find emppty space
{
posArr2[i] = compMark; //Check score for this position
score[i] = getScore(posArr2, humanMark, compMark, 0, 0);
}
}
//Return maximize score's index (its computer score)
maxScore = 0;
for(int i = 1; i < 9; i++)
{
if(score[maxScore] < score[i])
{
maxScore = i;
}
}
return maxScore;
}
//Function that control game
//Return 'h' if human wins, 'c' if comp wins and 'd' if draw
char startGame(int score[3], char humanMark, char compMark)
{
int turn; //Turn 0 = human, 1 = comp
int position; //position in between 0 to 8
char temp;
char posArr[9] = {'\0'}; //Array that saves mark of human and comp
char win = '\0'; //char to save winners mark
srand(time(0)); //Set random timer
turn = rand() % 2; //Randomize the turn
for(int i = 0; i < 9; i++)
{
displayGame(score, posArr);
if(turn == 0)
{
do
{
cout << "Human's turn: ";
temp = _getch(); //Input integer position as char (because of getch())
position = (int)temp - 48; //Converting char to int
position--; //Human's position start from 1, comps start from 0
cout << "\r"; //In case of invalid input we want to ask for input again on same line so use '\r'
} while(posArr[position] != '\0' || position < 0 || position > 9);
posArr[position] = humanMark; //Put human mark
turn = 1; //Change the turn
displayGame(score, posArr);
cout << "Human's turn: " << position+1 << endl;
}
else
{
position = compMove(posArr, humanMark, compMark); //Get computers move
posArr[position] = compMark; //Put computers mark
turn = 0; //Change the turn
displayGame(score, posArr);
cout << "Computer's turn: " << position+1 << endl;
}
win = winner(posArr); //Check if game is over
if(win == humanMark)
return 'h';
else if(win == compMark)
return 'c';
Sleep(1000);
}
return 'd';
}
//Main driver program
int main()
{
int score[3] = {0}; //Array to keep score
char humanMark, compMark, playAgain, winner;
instruction(); //Show instructions
humanMark = getHumanMark(); //Get humans mark
compMark = getCompMark(humanMark); //Get computers mark
displayMarks(humanMark, compMark); //Display both marks
do
{
winner = startGame(score, humanMark, compMark); //Start game
if (winner == 'h')
{
score[0]++;
cout << "You won!!!" << endl;
}
else if (winner == 'c')
{
score[1]++;
cout << "Computer won!!!" << endl;
}
else
{
score[2]++;
cout << "It's a draw" << endl;
}
cout << "Play again? Y or N? : ";
playAgain = _getch();
} while (playAgain != 'N' && playAgain != 'n');
}