-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
178 lines (170 loc) · 5.44 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
#include <iostream>
using namespace std;
void printer(string s[3][3]) ;
void comp_move(string s[3][3], string comp) ;
void cheker(string s[3][3],string &cond) ;
int main() {
// starting
string s[3][3] = {
{ " " , " " , " " } ,
{ " " , " " , " " } ,
{ " " , " " , " " }
};
cout << "Welcome to CLI tic tac toe game " << endl ;
cout << "For playing this game you need some knoledge of coordinate geometry " << endl ;
cout << "please select your sign : " << endl;
cout << "1. O" << endl ;
cout << "2. X" << endl ;
cout << "Choose [1\\2] : " ;
int a ;
cin >> a ;
string pl , comp;
string win = " ";
if (a == 1 ) {
pl = "O";
comp = "X";
} else if ( a ==2 ) {
pl = "X";
comp = "O";
}else { /// just a small check
cout << "Invalide values , terminating..." << endl ;
return 0 ;
}
// main process
printer(s) ;
while (true) {
cout << "Choose the coordinates" << endl;
cout << "x : " ;
int x ;
cin >> x ;
cout << "y : " ;
int y ;
cin >> y ;
if ( x > 3 || y > 3 || x < 0 || y < 0 ) {
cout << "Invalid values " << endl;
continue;
} else if ( s[y-1][x-1] != " ") {
cout << "The the location provided is already in use " << endl;
continue;
}
s[y-1][x-1] = pl ;
printer(s) ;
cheker(s,win);
if ( win == " " ) {
cout << "computer's move " << endl;
} else if ( win == "Tie") {
cout << "All places are filled, match is a tie!!" << endl;
printer(s) ;
return 0 ;
} else if ( win == "X" || win == "O") {
if ( win == pl ) {
cout << "You won !!" ;
return 0 ;
} else {
cout << "Computer won!!";
return 0 ;
}
}
comp_move(s,comp);
printer(s) ;
cheker(s,win);
// was't sure if this can be put into a function or not
if ( win == " " ) {
cout << "your move " << endl;
} else if ( win == "Tie") {
cout << "All places are filled, match is a tie!!" << endl;
printer(s) ;
return 0 ;
} else if ( win == "X" || win == "O") {
if ( win == pl ) {
cout << "You won !!" ;
return 0;
} else {
cout << "Computer won!!";
return 0;
}
}
}
cout << rand() % 100 ;
}
void printer(string s[3][3]) {
cout << endl << endl ;
string blank = " ";
cout << s[1][1] << endl;
cout << "Y *---*---*---*" << endl;
cout << "3 | "<< s[2][0] << " | "<< s[2][1] << " | "<< s[2][2] << " |" << endl;
cout << "| *---*---*---*" << endl ;
cout << "2 | "<< s[1][0] << " | "<< s[1][1] << " | "<< s[1][2] << " |" << endl;
cout << "| *---*---*---*" << endl ;
cout << "1 | "<< s[0][0] << " | "<< s[0][1] << " | "<< s[0][2] << " |" << endl;
cout << "| *---*---*---*" << endl ;
cout << "0-----1---2---3-X" << endl ;
cout << endl << endl;
}
void comp_move(string s[3][3], string comp) {
while (true) {
srand(time(0)) ; // to generate random number everytime
int x = rand() % 4 ;
srand(time(0)) ; // to generate random number everytime
int y = rand() % 4 ;
if (s[y][x] == " " ) {
s[y][x] = comp ;
break;
}
}
}
// next thing is write checker
void cheker(string s[3][3],string &cond) {
// horizontal check
if (s[0][0] == "O" && s[0][1] == "O" && s[0][2] == "O" ) {
cond = "O";
} else if (s[1][0] == "O" && s[1][1] == "O" && s[1][2] == "O" ) {
cond = "O" ;
} else if (s[2][0] == "O" && s[2][1] == "O" && s[2][2] == "O" ) {
cond = "O";
// vertical check
} else if (s[0][0] == "O" && s[1][0] == "O" && s[2][0] == "O" ) {
cond = "O";
} else if (s[0][1] == "O" && s[1][1] == "O" && s[2][1] == "O" ) {
cond = "O";
} else if (s[0][2] == "O" && s[1][2] == "O" && s[2][2] == "O" ) {
cond = "O";
// diagonal check
} else if (s[0][0] == "O" && s[1][1] == "O" && s[2][2] == "O" ) {
cond = "O";
} else if (s[2][0] == "O" && s[1][1] == "O" && s[0][2] == "O" ) {
cond = "O";
// check for x
}else if (s[0][0] == "X" && s[0][1] == "X" && s[0][2] == "X" ) {
cond = "X";
} else if (s[1][0] == "X" && s[1][1] == "X" && s[1][2] == "X" ) {
cond = "X" ;
} else if (s[2][0] == "X" && s[2][1] == "X" && s[2][2] == "X" ) {
cond = "X";
// vertical check
} else if (s[0][0] == "X" && s[1][0] == "X" && s[2][0] == "X" ) {
cond = "X";
} else if (s[0][1] == "X" && s[1][1] == "X" && s[2][1] == "X" ) {
cond = "X";
} else if (s[0][2] == "X" && s[1][2] == "X" && s[2][2] == "X" ) {
cond = "X";
// diagonal check
} else if (s[0][0] == "X" && s[1][1] == "X" && s[2][2] == "X" ) {
cond = "X";
} else if (s[2][0] == "X" && s[1][1] == "X" && s[0][2] == "X" ) {
cond = "X";
} else {
cond = " " ;
}
bool fil = true ;
for (int i = 0 ; i < 3 ; ++i ) {
for (int j = 0 ; j < 3 ; ++j) {
if ( s[i][j] == " ") {
fil = false ;
}
}
}
if (fil == true && cond == " ") {
cond = "Tie" ;
}
}