forked from spandey1296/CODEMONK-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.cpp
166 lines (160 loc) Β· 4.18 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
#include<bits/stdc++.h>
using namespace std;
void print(int ttt[])
{
// system("cls");
for(int i=1; i<=9;i++){
cout<<ttt[i-1]<<" ";
if(i%3==0){
cout<<endl;
}
}
}
int check(int ttt[], int id1, int id2, int id3){
if(ttt[id1]==2){
return id1;
}else if(ttt[id2]==2){
return id2;
}else{
return id3;
}
}
int whereToPlace(int ttt[])
{
if(ttt[0] * ttt[1] * ttt[2] == 50){
return check(ttt,0,1,2);
}else if(ttt[3] * ttt[4] * ttt[5] == 50){
return check(ttt,3,4,5);
}else if(ttt[6] * ttt[7] * ttt[8] == 50){
return check(ttt,6,7,8);
}else if(ttt[0] * ttt[3] * ttt[6] == 50){
return check(ttt,0,3,6);
}else if(ttt[1] * ttt[4] * ttt[7] == 50){
return check(ttt,1,4,7);
}else if(ttt[2] * ttt[5] * ttt[8] == 50){
return check(ttt,2,5,8);
}else if(ttt[0] * ttt[4] * ttt[8] == 50){
return check(ttt,0,4,8);
}else if(ttt[2] * ttt[4] * ttt[6] == 50){
return check(ttt,2,4,6);
}
if(ttt[0] * ttt[1] * ttt[2] == 18){
return check(ttt,0,1,2);
}else if(ttt[3] * ttt[4] * ttt[5] == 18){
return check(ttt,3,4,5);
}else if(ttt[6] * ttt[7] * ttt[8] == 18){
return check(ttt,6,7,8);
}else if(ttt[0] * ttt[3] * ttt[6] == 18){
return check(ttt,0,3,6);
}else if(ttt[1] * ttt[4] * ttt[7] == 18){
return check(ttt,1,4,7);
}else if(ttt[2] * ttt[5] * ttt[8] == 18){
return check(ttt,2,5,8);
}else if(ttt[0] * ttt[4] * ttt[8] == 18){
return check(ttt,0,4,8);
}else if(ttt[2] * ttt[4] * ttt[6] == 18){
return check(ttt,2,4,6);
}else
return 99;
}
// function for winning condition
int whoWins(int ttt[]){
if(((ttt[0] * ttt[1] * ttt[2] )== 125)
or
((ttt[3] * ttt[4] * ttt[5]) == 125)
or
((ttt[6] * ttt[7] * ttt[8]) == 125)
or
((ttt[0] * ttt[3] * ttt[6]) == 125)
or
((ttt[1] * ttt[4] * ttt[7]) == 125)
or
((ttt[2] * ttt[5] * ttt[8]) == 125)
or
((ttt[0] * ttt[4] * ttt[8]) == 125)
or
((ttt[2] * ttt[4] * ttt[6]) == 125)
){
return 5;
}else if(((ttt[0] * ttt[1] * ttt[2] )== 27)
or
((ttt[3] * ttt[4] * ttt[5]) == 27)
or
((ttt[6] * ttt[7] * ttt[8]) == 27)
or
((ttt[0] * ttt[3] * ttt[6]) == 27)
or
((ttt[1] * ttt[4] * ttt[7]) == 27)
or
((ttt[2] * ttt[5] * ttt[8] )== 27)
or
((ttt[0] * ttt[4] * ttt[8]) == 27)
or
((ttt[2] * ttt[4] * ttt[6]) == 27)
){
return 3;
}else
return 2;
}
// for changing the grid board
void makeChanges(int ttt[], int index){
ttt[index-1]=3;
}
int main(){
int ttt[9];
ttt[0]=5;
for(int i=1; i<9;i++){
ttt[i]=2;
}
cout<<"---- The game is between player and computer ----"<<endl;
cout<<"5 for computer"<<endl;
cout<<"3 for player"<<endl;
cout<<"-------LET'S START-------"<<endl;
print(ttt);
bool player =1;
int temp =0;
int index ;
for(int i=0; i<=7;i++){
temp++;
if(player){
cout<<"It's your turn"<<endl;
cin>>index;
cout<<"-------***********-------"<<endl;
makeChanges(ttt,index);
print(ttt);
player = !player;
cout<<"-------***********-------"<<endl;
}else{
int idx = whereToPlace(ttt);
if(idx==99){
for(int i=0;i<9;i=i+2){
if(ttt[i]==2){
ttt[i]=5;
break;
}
}
}else{
ttt[idx]=5;
}
player = !player;
print(ttt);
}
if(i>=3){
if(whoWins(ttt)==5){
cout<<"Computer wins"<<endl;
temp =0;
break;
}else if(whoWins(ttt)==3){
cout<<"Player wins"<<endl;
temp=0;
break;
}else{
continue;
}
}
if(temp==7 or temp ==8){
cout<<"Match Drawn"<<endl;
}
}
return 0;
}