forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game_tic-tak-toe.cpp
182 lines (175 loc) · 5.12 KB
/
Game_tic-tak-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
#include<iostream.h> //opening headerfile iostream for in-out
#include<conio.h> //opening headerfile conio for clrscr();
#include<stdlib.h> //opening headerfile stdlib for
random(int);
char tic[3][3]; //global matrix declerations
int d,e,f,a,t,i,j,x,y; //global variables declerations
void display(); //displays the matrix
void user(); //function for user's move
void newdisp(); //function for display of matrix after
every
move
void pc(); //function for pc's move
int check(); //function for finding out the winner
int horcheck(); //function for horizontal line check
int vercheck(); //function for vertical line check
int diagcheck(); //function for diagonal line check
main() //main function
{
clrscr(); //clears the previous output screen
randomize(); //initialize random function calling
int d=random(2); //random function call
for(i=0;i<3;i++)
for(j=0;j<3;j++)
tic[i][j]=' '; //assigning space ' ' to all elements of matrix
display(); //display function call
d==0?user():pc(); //random starting of the game depending
on d
getch(); //provides output by getting input
without
returning to program
return 0; //return int to main function
}
void display() //display function definition
{
for(t=0;t<3;t++)
{
cout<<" "<<tic[t][0]<<" | "<<tic[t][1]<<" | "<<tic[t][2]<<endl;
//figure formation
if(t!=2)
cout<<" --|---|--"<<endl;
}
}
void user() //user function definition
{
cout<<"
ENTER THE CO-ORDINATES WHERE YOU WANT TO PUT UR 'X' i.e
0,1,2
";
cin>>x>>y;
if((x<0)||(x>2)&&(y<0)||(y>2)) //check for valid co-ordinates
{
cout<<"
ENTER THE CORRECT CO-ORDINATES";
user(); //user function call
}
else
{
if(tic[x][y]==' ') //check for vacant space at entered co-ordinates
{
tic[x][y]='X'; //assigning user 'X' to the co-ordinates
newdisp(); //newdisp function call
}
else
{
cout<<"
THIS POSITION IS ALREADY FILLED. CHOOSE SOME OTHER
CO-ORDINATES";
user(); //user function call
}
}
d=check(); //check function call
if(d==0)
pc(); //pc function call
else
{
cout<<"
YOU ARE THE WINNER";
getche(); //requires enter to return to program. prevents return
without display
exit(1); //program termination
}
}
void newdisp() //newdisp function definition
{
for(t=0;t<3;t++)
{
cout<<" "<<tic[t][0]<<" | "<<tic[t][1]<<" | "<<tic[t][2]<<endl;
//displays new tictactoe after user or pc turn
if(t!=2)
cout<<" --|---|--"<<endl;
}
}
void pc() //pc function call
{
int f,z;
randomize(); //initialize random function calling
cout<<"
THIS IS THE COMPUTER'S MOVE
";
for(i=0;i<=20;i++)
{
f=random(3);
z=random(3);
if(tic[f][z]==' ') //check for vacant space at entered
co-ordinates
{
tic[f][z]='O'; //assigning pc 'O' to the co-ordinates
goto x; //exiting for loop to display new tictactoe
}
else
continue; //
}
x:newdisp(); //newdisp function call
d=check(); //check function call
if(d==0)
user(); //user function call
else
{
cout<<"
I'M THE WINNER";
getche(); //requires enter to return to program. prevents
return without display
exit(1); //program termination
}
}
check() //check function definition
{
horcheck(); //horcheck function call
vercheck(); //vercheck function call
diagcheck(); //diagcheck function call
return (d||e||f);
}
horcheck() //horcheck function definition
{
if(((tic[0][0]==tic[0][1])&&(tic[0][1]==tic[0][2])&&(tic[0][1]!='
'))||((tic[1][0]==tic[1][1])&&(tic[1][1]==tic[1][2])&&(tic[1][1]!='
'))||((tic[2][0]==tic[2][1])&&(tic[2][1]==tic[2][2])&&(tic[2][2]!='
')))
d=1; //checks each element of a horizontal line
to
be same
else //returns 1 if all 3 elements of any
horizontal line are same
d=0; //else returns 0
return d;
}
vercheck() //vercheck function definition
{
if(((tic[0][0]==tic[1][0])&&(tic[1][0]==tic[2][0])&&(tic[0][0]!='
'))||((tic[0][1]==tic[1][1])&&(tic[1][1]==tic[2][1])&&(tic[0][1]!='
'))||((tic[0][2]==tic[1][2])&&(tic[1][2]==tic[2][2])&&(tic[0][2]!='
')))
e=1; //checks each element of a vertical line to
be
same
else //returns 1 if all 3 elements of any
vertical
line are same
e=0; //else returns 0
return e;
}
diagcheck() //diagcheck function definition
{
if(((tic[0][0]==tic[1][1])&&(tic[1][1]==tic[2][2])&&(tic[0][0]!='
'))||((tic[0][2]==tic[1][1])&&(tic[1][1]==tic[2][0])&&(tic[1][1]!='
')))
f=1; //checks each element of a diagonal line to
be
same
else //returns 1 if all 3 elements of any
diagonal
line are same
f=0; //else returns 0
return f;
}