forked from AnuragAnalog/games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic_Tac_Toe.cpp
188 lines (150 loc) · 3.89 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
#include<iostream>
#include<conio.h>
using namespace std;
char arr[3][3] = {{'1','2','3'}, {'4','5','6'},{'7','8','9'}};
char turn = 'X';
bool draw = false;
void display_board(){
system("cls");
cout<<"\t | | "<<endl;
cout<<"\t "<<arr[0][0]<<" | "<<arr[0][1]<<" | "<<arr[0][2]<<" "<<endl;
cout<<"\t______|_____|_____"<<endl;
cout<<"\t | | "<<endl;
cout<<"\t "<<arr[1][0]<<" | "<<arr[1][1]<<" | "<<arr[1][2]<<" "<<endl;
cout<<"\t______|_____|_____"<<endl;
cout<<"\t | | "<<endl;
cout<<"\t "<<arr[2][0]<<" | "<<arr[2][1]<<" | "<<arr[2][2]<<" "<<endl;
cout<<"\t | | "<<endl;
}
void player_turn(){
int choice;
int row;
int column;
if(turn=='X'){
cout<<"\n\t player1 [X] turn: ";
}
if(turn=='O'){
cout<<"\n\t player2 [O] turn: ";
}
cin>>choice;
switch (choice)
{
case 1:
row = 0;
column = 0;
break;
case 2:
row = 0;
column = 1;
break;
case 3:
row = 0;
column = 2;
break;
case 4:
row = 1;
column = 0;
break;
case 5:
row = 1;
column = 1;
break;
case 6:
row = 1;
column = 2;
break;
case 7:
row = 2;
column = 0;
break;
case 8:
row = 2;
column = 1;
break;
case 9:
row = 2;
column = 2;
break;
default:
cout<<"Invalid choice"<<endl;
break;
}
if(turn=='X' && arr[row][column]!='X' && arr[row][column]!='O'){
arr[row][column]='X';
turn='O';
}
else if(turn=='O' && arr[row][column]!='X' && arr[row][column]!='O'){
arr[row][column]='O';
turn='X';
}
else{
cout<<"\n enter another number"<<endl;
player_turn();
}
display_board();
}
bool game_over(){
// check win
// horizontal win
for(int i=0; i<3; i++){
if(arr[i][0]==arr[i][1] && arr[i][0]==arr[i][2] && arr[i][0]=='X'){
cout<<"\n\tPLAYER 1 WON !!"<<endl;
return false;
}
else if(arr[i][0]==arr[i][1] && arr[i][0]==arr[i][2] && arr[i][0]=='O'){
cout<<"\n\tPLAYER 2 WON !!"<<endl;
return false;
}
}
// vertical win
for(int i=0; i<3; i++){
if(arr[0][i]==arr[1][i] && arr[0][i]==arr[2][i] && arr[0][i]=='X'){
cout<<"\n\tPLAYER 1 WON !!"<<endl;
return false;
}
else if(arr[0][i]==arr[1][i] && arr[0][i]==arr[2][i] && arr[0][i]=='O'){
cout<<"\n\tPLAYER 2 WON !!"<<endl;
return false;
}
}
//diagnol win
for(int i=0; i<3; i++){
if(arr[0][0]==arr[1][1] && arr[0][0]==arr[2][2] && arr[0][0]=='X'){
cout<<"\n\tPLAYER 1 WON !!"<<endl;
return false;
}
else if(arr[0][2]==arr[1][1] && arr[0][2]==arr[2][0] && arr[0][2]=='X'){
cout<<"\n\tPLAYER 1 WON !!"<<endl;
return false;
}
else if(arr[0][0]==arr[1][1] && arr[0][0]==arr[2][2] && arr[0][0]=='O'){
cout<<"\n\tPLAYER 2 WON !!"<<endl;
return false;
}
else if(arr[0][2]==arr[1][1] && arr[0][2]==arr[2][0] && arr[0][2]=='O'){
cout<<"\n\tPLAYER 2 WON !!"<<endl;
return false;
}
}
//playing
for(int i=0; i<3;i++){
for(int j=0; j<3; j++){
if(arr[i][j]!='X' && arr[i][j]!='O'){
return true;
}
}
}
//draw
draw = true;
cout<<"\n\t GAME DRAW :( "<<endl;
return false;
}
int main(){
display_board();
while(game_over()){
player_turn();
}
cout<<"\n\n *** Hope you enjoyed playing it :) *** \n\n";
getch();
return 0;
}