-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtictac.h
134 lines (119 loc) · 2.07 KB
/
tictac.h
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
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
char mat[3][3], p1, p2;
int init();
void display();
int input(char, int, int);
char check();
int init()
{
int i, j;
for (i=0; i<3; i++)
for (j=0; j<3; j++)
mat[i][j] = '-';
}
void display()
{
int i, j;
system("clear");
cout<<"\nWelcome to the Tic-Tac-Toe Game!\n\nGrid Co-ordinates : "<<endl<<endl;
for (i=0; i<3; i++)
{
cout<<"\t";
for (j=0; j<3; j++)
cout<<i<<" "<<j<<" ";
cout<<endl;
}
cout<<"\nThe Tic-Tac-Toe board : "<<endl<<endl;
for (i=0; i<3; i++)
{
cout<<"\t";
for (j=0; j<3; j++)
cout<<" "<<mat[i][j]<<" ";
cout<<endl;
}
}
int input(char choice, int x, int y)
{
if (x >= 0 && x <= 2 && y >= 0 && y<= 2)
if (mat[x][y] == '-')
{
mat[x][y] = choice;
return 0;
}
else
{
cout<<endl<<"Co-ordinates "<<x<<" "<<y<<" are already used!"<<endl<<"Press ENTER to continue..."<<endl;
do{}
while(cin.get() != '\n');
return 1;
}
else
{
cout<<"\nInvalid Co-ordinates!"<<endl<<"Press ENTER to continue..."<<endl;
do{}
while(cin.get() != '\n');
return 1;
}
}
char check()
{
int i, j;
char temp;
for (j=0; j<3; j++)
{
if (mat[0][j] != '-')
{
temp = mat[0][j];
if (j == 0)
{
if (mat[0][1] == temp)
if (mat[0][2] == temp)
return temp;
if (mat[1][0] == temp)
if (mat[2][0] == temp)
return temp;
if (mat[1][1] == temp)
if (mat[2][2] == temp)
return temp;
}
else if (j == 1)
{
if (mat[1][1] == temp)
if (mat[2][1] == temp)
return temp;
}
else
{
if (mat[1][2] == temp)
if (mat[2][2] == temp)
return temp;
}
}
}
for (i=1; i<3; i++)
{
if (mat[i][0] != '-')
{
temp = mat[i][0];
if (i == 1)
{
if (mat[1][1] == temp)
if (mat[1][2] == temp)
return temp;
}
else
{
if (mat[2][1] == temp)
if (mat[2][2] == temp)
return temp;
if (mat[1][1] == temp)
if (mat[0][2] == temp)
return temp;
}
}
}
return 'f';
}