-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTictactoe_direction_control.c
293 lines (281 loc) · 7.7 KB
/
Tictactoe_direction_control.c
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <conio.h>
#include <windows.h>
void print_draw(int (*pc)[3]);
void gotoxy(int xpos, int ypos);
void showCursor(int visible);
int pox,poy;
void up(void);
void down(void);
void left(void);
void right(void);
void circle(int (*pi)[3]);
void forkk(int (*pf)[3]);
int win_check_circle(int (*pc)[3]);
int win_check_fork(int (*pc)[3]);
int main(int argc,char **argv)
{
char position;
int picture[3][3]={0};
for (int i=0;i<3;i++){
for (int j=0;j<3;j++){
picture[i][j]='-';
}
}
pox=5;
poy=2;
print_draw(picture);
gotoxy(5,2);
while (1){
position=getch();
if (position==111){
circle(picture);
if (win_check_circle(picture)){
system("CLS");
gotoxy(3,2);
printf(" O Circle is win the game !!!\n\n");
gotoxy(3,5);
system("pause");
exit(0);
}
}else if (position==120){
forkk(picture);
if (win_check_fork(picture)){
system("CLS");
gotoxy(3,2);
printf(" X Fork is win the game !!!\n\n");
gotoxy(3,5);
system("pause");
exit(0);
}
}else if (position==-32){
position=getch();
if (position!=0){
switch (position){
case 72:
up();
break;
case 80:
down();
break;
case 75:
left();
break;
case 77:
right();
break;
}
}
}
}
return 0;
}
void print_draw(int (*pc)[3]){
system("CLS");
printf(" | | \n");
printf(" %c | %c | %c \n", *(*(pc+0)+0), *(*(pc+0)+1), *(*(pc+0)+2));
printf(" _____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", *(*(pc+1)+0), *(*(pc+1)+1), *(*(pc+1)+2));
printf(" _____|_____|_____\n");
printf(" | | \n");
printf(" %c | %c | %c \n", *(*(pc+2)+0), *(*(pc+2)+1), *(*(pc+2)+2));
printf(" | | \n\n");
}
void gotoxy(int xpos, int ypos){
COORD scrn;
HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
scrn.X = xpos; scrn.Y = ypos;
SetConsoleCursorPosition(hOuput,scrn);
}
void showCursor(int visible)
{
CONSOLE_CURSOR_INFO ConCurInf;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleCursorInfo(hStdOut, &ConCurInf);
ConCurInf.bVisible = visible;
SetConsoleCursorInfo(hStdOut, &ConCurInf);
}
void up(void){
if (poy>2)
gotoxy(pox,poy-=3);
else
gotoxy(pox,poy);
}
void down(void){
if (poy<6)
gotoxy(pox,poy+=3);
else
gotoxy(pox,poy);
}
void left(void){
if (pox>5)
gotoxy(pox-=6,poy);
else
gotoxy(pox,poy);
}
void right(void){
if (pox<17)
gotoxy(pox+=6,poy);
else
gotoxy(pox,poy);
}
void circle(int (*pi)[3]){
if (pox==5 && poy==2){
if (*(*(pi+0)+0)=='-'){
*(*(pi+0)+0)='O';
print_draw(pi);
gotoxy(pox,poy);
}
}else if (pox==5 && poy==5){
if (*(*(pi+1)+0)=='-'){
*(*(pi+1)+0)='O';
print_draw(pi);
gotoxy(pox,poy);
}
}else if (pox==5 && poy==8){
if (*(*(pi+2)+0)=='-'){
*(*(pi+2)+0)='O';
print_draw(pi);
gotoxy(pox,poy);
}
}else if (pox==11 && poy==2){
if (*(*(pi+0)+1)=='-'){
*(*(pi+0)+1)='O';
print_draw(pi);
gotoxy(pox,poy);
}
}else if (pox==11 && poy==5){
if (*(*(pi+1)+1)=='-'){
*(*(pi+1)+1)='O';
print_draw(pi);
gotoxy(pox,poy);
}
}else if (pox==11 && poy==8){
if (*(*(pi+2)+1)=='-'){
*(*(pi+2)+1)='O';
print_draw(pi);
gotoxy(pox,poy);
}
}else if (pox==17 && poy==2){
if (*(*(pi+0)+2)=='-'){
*(*(pi+0)+2)='O';
print_draw(pi);
gotoxy(pox,poy);
}
}else if (pox==17 && poy==5){
if (*(*(pi+1)+2)=='-'){
*(*(pi+1)+2)='O';
print_draw(pi);
gotoxy(pox,poy);
}
}else if (pox==17 && poy==8){
if (*(*(pi+2)+2)=='-'){
*(*(pi+2)+2)='O';
print_draw(pi);
gotoxy(pox,poy);
}
}
}
void forkk(int (*pf)[3]){
if (pox==5 && poy==2){
if (*(*(pf+0)+0)=='-'){
*(*(pf+0)+0)='X';
print_draw(pf);
gotoxy(pox,poy);
}
}else if (pox==5 && poy==5){
if (*(*(pf+1)+0)=='-'){
*(*(pf+1)+0)='X';
print_draw(pf);
gotoxy(pox,poy);
}
}else if (pox==5 && poy==8){
if (*(*(pf+2)+0)=='-'){
*(*(pf+2)+0)='X';
print_draw(pf);
gotoxy(pox,poy);
}
}else if (pox==11 && poy==2){
if (*(*(pf+0)+1)=='-'){
*(*(pf+0)+1)='X';
print_draw(pf);
gotoxy(pox,poy);
}
}else if (pox==11 && poy==5){
if (*(*(pf+1)+1)=='-'){
*(*(pf+1)+1)='X';
print_draw(pf);
gotoxy(pox,poy);
}
}else if (pox==11 && poy==8){
if (*(*(pf+2)+1)=='-'){
*(*(pf+2)+1)='X';
print_draw(pf);
gotoxy(pox,poy);
}
}else if (pox==17 && poy==2){
if (*(*(pf+0)+2)=='-'){
*(*(pf+0)+2)='X';
print_draw(pf);
gotoxy(pox,poy);
}
}else if (pox==17 && poy==5){
if (*(*(pf+1)+2)=='-'){
*(*(pf+1)+2)='X';
print_draw(pf);
gotoxy(pox,poy);
}
}else if (pox==17 && poy==8){
if (*(*(pf+2)+2)=='-'){
*(*(pf+2)+2)='X';
print_draw(pf);
gotoxy(pox,poy);
}
}
}
int win_check_circle(int (*pc)[3]){
if ((*(*(pc+0)+0)=='O'&&*(*(pc+0)+1)=='O'&&*(*(pc+0)+2)=='O')){
return 1;
}else if ((*(*(pc+1)+0)=='O'&&*(*(pc+1)+1)=='O'&&*(*(pc+1)+2)=='O')){
return 1;
}else if ((*(*(pc+2)+0)=='O'&&*(*(pc+2)+1)=='O'&&*(*(pc+2)+2)=='O')){
return 1;
}else if ((*(*(pc+0)+0)=='O'&&*(*(pc+1)+0)=='O'&&*(*(pc+2)+0)=='O')){
return 1;
}else if ((*(*(pc+0)+1)=='O'&&*(*(pc+1)+1)=='O'&&*(*(pc+2)+1)=='O')){
return 1;
}else if ((*(*(pc+0)+2)=='O'&&*(*(pc+1)+2)=='O'&&*(*(pc+2)+2)=='O')){
return 1;
}else if ((*(*(pc+0)+0)=='O'&&*(*(pc+1)+1)=='O'&&*(*(pc+2)+2)=='O')){
return 1;
}else if ((*(*(pc+0)+2)=='O'&&*(*(pc+1)+1)=='O'&&*(*(pc+2)+0)=='O')){
return 1;
}else{
return 0;
}
}
int win_check_fork(int (*pc)[3]){
if (*(*(pc+0)+0)=='X'&&*(*(pc+0)+1)=='X'&&*(*(pc+0)+2)=='X'){
return 1;
}else if ((*(*(pc+1)+0)=='X'&&*(*(pc+1)+1)=='X'&&*(*(pc+1)+2)=='X')){
return 1;
}else if ((*(*(pc+2)+0)=='X'&&*(*(pc+2)+1)=='X'&&*(*(pc+2)+2)=='X')){
return 1;
}else if ((*(*(pc+0)+0)=='X'&&*(*(pc+1)+0)=='X'&&*(*(pc+2)+0)=='X')){
return 1;
}else if ((*(*(pc+0)+1)=='X'&&*(*(pc+1)+1)=='X'&&*(*(pc+2)+1)=='X')){
return 1;
}else if ((*(*(pc+0)+2)=='X'&&*(*(pc+1)+2)=='X'&&*(*(pc+2)+2)=='X')){
return 1;
}else if ((*(*(pc+0)+0)=='X'&&*(*(pc+1)+1)=='X'&&*(*(pc+2)+2)=='X')){
return 1;
}else if ((*(*(pc+0)+2)=='X'&&*(*(pc+1)+1)=='X'&&*(*(pc+2)+0)=='X')){
return 1;
}else{
return 0;
}
}