-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
193 lines (165 loc) · 3.69 KB
/
main.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
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int index; //which is used for best move for AI
char board[9]= {'.','.','.','.','.','.','.','.','.'};
void draw_board() //display tic-tac-toe board
{
cout<<endl;
cout<<board[0]<<" | "<<board[1]<<" | "<<board[2]<<endl;
cout<<"---------"<<endl;
cout<<board[3]<<" | "<<board[4]<<" | "<<board[5]<<endl;
cout<<"---------"<<endl;
cout<<board[6]<<" | "<<board[7]<<" | "<<board[8]<<endl;
}
int equals3(char a, char b, char c) {
return a == b && b == c && a != '.';
}
char checkWinner() {
char winner = 'p';//any symbol
// horizontal
for (int i = 0; i < 9; i+=3) {
if (equals3(board[i], board[i+1], board[i+2])) {
winner = board[i];
}
}
// Vertical
for (int i = 0; i < 3; i++) {
if (equals3(board[i], board[i+3], board[i+6])) {
winner = board[i];
}
}
// Diagonal
if (equals3(board[0], board[4], board[8])) {
winner = board[0];
}
if (equals3(board[2], board[4], board[6])) {
winner = board[2];
}
int openSpots = 0;//check whether board empty
for (int j = 0; j < 9; j++) {
if (board[j] == '.') {
openSpots++;
}
}
if (winner == 'p' && openSpots == 0) {
return 't'; //which belong to tie
}
else
{
return winner;
}
}
int minimaxAlgo(int depth, bool isMaximizer)
{
char result = checkWinner();
if (result != 'p')
{
if(result == 'X') //win of AI
return 10;
else if(result == 'O') //loss
return -10;
else if(result == 't') //tie
return 0;
}
if(isMaximizer)
{
int bestScore = INT_MIN;
int score;
for(int i = 0; i < 9; i++)
{
if(board[i] == '.')
{
board[i] = 'X';
score = minimaxAlgo(depth + 1, false);
board[i] = '.';
bestScore = max(score,bestScore);
}
}
return bestScore;
}
else
{
int bestScore = INT_MAX;
int score;
for(int i = 0; i < 9; i++)
{
if(board[i] == '.')
{
board[i] = 'O';
score = minimaxAlgo(depth + 1, true);
board[i] = '.';
bestScore = min(score,bestScore);
}
}
return bestScore;
}
}
void BestMove()
{
int score;
int bestScore = INT_MIN;
for(int i = 0; i < 9; i++)
{
if(board[i] == '.')
{
board[i] = 'X';
score = minimaxAlgo(0,false);
board[i] = '.';
if(score > bestScore)
{
bestScore = score;
index = i;
}
}
}
board[index] = 'X';
}
int main()
{
int move;
//draw_board();
do{
system("CLS");
BestMove();
//board[index] = 'X';
draw_board();
char result = checkWinner();
if (result == 'X')
{
cout<<endl<<"CPU WON.....";
break;
}
if(result == 't')
{
cout<<endl<<"Draw....";
break;
}
again:
cout<<"\nEnter Your Move:";
cin>>move;
if(board[move-1] == '.')
{
board[move-1] = 'O';
}
else
{
cout<<"\nInvalid Move....,Enter Correct Move Again\n";
goto again;
}
system("CLS");
draw_board();
result = checkWinner();
if (result == 'O')
{
cout<<endl<<"You WON.....";
break;
}
if(result == 't')
{
cout<<endl<<"Draw....";
break;
}
}while(1);
return 0;
}