· 7 years ago · Jan 08, 2019, 09:12 PM
1#include <stdio.h>
2#include <stdlib.h>
3#include <conio.h>
4#include <string.h>
5int main(void);
6char end(int *table)
7{
8 char i;
9 for(i=0;i<3;i++)
10 {
11 ///checking for vertical win
12 if((*(table+i))%3!=0)
13 if((*(table+i))%3==(*(table+i+3))%3&&(*(table+i))%3==(*(table+i+6))%3)
14 {
15 if((*(table+i))%3==1)
16 return 1;
17 else
18 return 2;
19 }
20 ///checking for horizontal win
21 if((*(table+i*3))%3!=0)
22 if((*(table+i*3))%3==(*(table+i*3+1))%3&&(*(table+i*3))%3==(*(table+i*3+2))%3)
23 {
24 if((*(table+i*3))%3==1)
25 return 1;
26 else
27 return 2;
28 }
29 }
30 ///checking for diagonal win
31 if(((*(table+4))%3!=0 &&
32 (
33 ((*(table))%3==(*(table+4))%3 && (*(table))%3==(*(table+8))%3 ) ||
34 ((*(table+2))%3==(*(table+4))%3 && (*(table+2))%3==(*(table+6))%3 )
35 )
36 ))
37 {
38 if((*(table+4))%3==1)
39 return 1;
40 else
41 return 2;
42 }
43 for(i=0;i<9;i++)
44 if((*(table+i))%3==0)
45 i=11;
46 if(i==12)
47 return 0;
48 return 3;
49}
50void cpu_update(int pic[20][26],int *table,char playing_position)
51{
52 ///building x
53 if((*(table+playing_position))%3==1)
54 {
55 pic[playing_position/3*(6+1)+1][playing_position%3*(8+1)+2]='\\';
56 pic[playing_position/3*(6+1)+2][playing_position%3*(8+1)+3]='\\';
57 pic[playing_position/3*(6+1)+3][playing_position%3*(8+1)+4]='\\';
58 pic[playing_position/3*(6+1)+4][playing_position%3*(8+1)+5]='\\';
59 pic[playing_position/3*(6+1)+1][playing_position%3*(8+1)+8-3]='/';
60 pic[playing_position/3*(6+1)+2][playing_position%3*(8+1)+8-4]='/';
61 pic[playing_position/3*(6+1)+3][playing_position%3*(8+1)+8-5]='/';
62 pic[playing_position/3*(6+1)+4][playing_position%3*(8+1)+8-6]='/';
63 }
64 ///building o
65 else if((*(table+playing_position))%3==2)
66 {
67 pic[playing_position/3*(6+1)+1][playing_position%3*(8+1)+2]='/';
68 pic[playing_position/3*(6+1)+1][playing_position%3*(8+1)+3]=238;
69 pic[playing_position/3*(6+1)+1][playing_position%3*(8+1)+4]=238;
70 pic[playing_position/3*(6+1)+1][playing_position%3*(8+1)+5]='\\';
71 pic[playing_position/3*(6+1)+2][playing_position%3*(8+1)+1]=179;
72 pic[playing_position/3*(6+1)+2][playing_position%3*(8+1)+6]=179;
73 pic[playing_position/3*(6+1)+3][playing_position%3*(8+1)+1]=179;
74 pic[playing_position/3*(6+1)+3][playing_position%3*(8+1)+6]=179;
75 pic[playing_position/3*(6+1)+4][playing_position%3*(8+1)+2]='\\';
76 pic[playing_position/3*(6+1)+4][playing_position%3*(8+1)+3]='_';
77 pic[playing_position/3*(6+1)+4][playing_position%3*(8+1)+4]='_';
78 pic[playing_position/3*(6+1)+4][playing_position%3*(8+1)+5]='/';
79 }
80
81}
82void player_update(int pic[20][26],int *table,char last_square_position)
83{
84 char i,j,square_position=-1;
85 ///finding square position
86 for(i=0;i<9&&!(square_position+1);i++)
87 if((*(table+i))>2)
88 square_position=i;
89 ///deleting square from previous position
90 {
91 for(j=0;j<8;j++)
92 pic[last_square_position/3*(6+1)][(last_square_position%3)*(8+1)+j]=' ';
93 for(j=1;j<6-1;j++)
94 {
95 pic[last_square_position/3*(6+1)+j][(last_square_position%3)*(8+1)]=' ';
96 pic[last_square_position/3*(6+1)+j][(last_square_position%3)*(8+1)+8-1]=' ';
97 }
98 for(j=0;j<8;j++)
99 pic[last_square_position/3*(6+1)+6-1][(last_square_position%3)*(8+1)+j]=' ';
100 }
101 ///building x
102 if((*(table+square_position))%3==1)
103 {
104 pic[square_position/3*(6+1)+1][square_position%3*(8+1)+2]='\\';
105 pic[square_position/3*(6+1)+2][square_position%3*(8+1)+3]='\\';
106 pic[square_position/3*(6+1)+3][square_position%3*(8+1)+4]='\\';
107 pic[square_position/3*(6+1)+4][square_position%3*(8+1)+5]='\\';
108 pic[square_position/3*(6+1)+1][square_position%3*(8+1)+8-3]='/';
109 pic[square_position/3*(6+1)+2][square_position%3*(8+1)+8-4]='/';
110 pic[square_position/3*(6+1)+3][square_position%3*(8+1)+8-5]='/';
111 pic[square_position/3*(6+1)+4][square_position%3*(8+1)+8-6]='/';
112 }
113 ///building o
114 else if((*(table+square_position))%3==2)
115 {
116 pic[square_position/3*(6+1)+1][square_position%3*(8+1)+2]='/';
117 pic[square_position/3*(6+1)+1][square_position%3*(8+1)+3]=238;
118 pic[square_position/3*(6+1)+1][square_position%3*(8+1)+4]=238;
119 pic[square_position/3*(6+1)+1][square_position%3*(8+1)+5]='\\';
120 pic[square_position/3*(6+1)+2][square_position%3*(8+1)+1]=179;
121 pic[square_position/3*(6+1)+2][square_position%3*(8+1)+6]=179;
122 pic[square_position/3*(6+1)+3][square_position%3*(8+1)+1]=179;
123 pic[square_position/3*(6+1)+3][square_position%3*(8+1)+6]=179;
124 pic[square_position/3*(6+1)+4][square_position%3*(8+1)+2]='\\';
125 pic[square_position/3*(6+1)+4][square_position%3*(8+1)+3]='_';
126 pic[square_position/3*(6+1)+4][square_position%3*(8+1)+4]='_';
127 pic[square_position/3*(6+1)+4][square_position%3*(8+1)+5]='/';
128 }
129 return;
130}
131void print(int pic[20][26])
132{
133 unsigned char i,j;
134 printf("USE THE WASD KEYS TO MOVE AND SPACE TO PLAY\n\n");
135 ///printing board
136 for(i=0;i<20;i++)
137 {
138 for(j=0;j<26;j++)
139 printf("%c",pic[i][j]);
140 printf("\n");
141 }
142 return;
143}
144void start(int pic[20][26])
145{
146 unsigned char i,j;
147 ///filling the spaces and fixing the vertical lines
148 for(i=0;i<20;i++)
149 {
150 for(j=0;j<8;j++)
151 pic[i][j]=' ';
152 pic[i][8]=179;
153 for(j=9;j<17;j++)
154 pic[i][j]=' ';
155 pic[i][17]=179;
156 for(j=18;j<26;j++)
157 pic[i][j]=' ';
158 }
159 ///fixing the horizontal lines(6
160 {
161 for(i=0;i<26;i++)
162 pic[6][i]=196;
163 for(i=0;i<26;i++)
164 pic[13][i]=196;
165 }
166 ///fixing the 4 points where the horizontal and vertical lines meet
167 pic[6][8]=pic[6][17]=pic[13][8]=pic[13][17]=197;
168 return;
169}
170int check(int *table,int s1,int s2,int s3)
171{
172 ///checking if s1 position is equal to s2 and if s3 is free to play
173 if(((*(table+s1))%3==(*(table+s2))%3)&&(*(table+s1))%3!=0&&(*(table+s3))%3==0)
174 return s3;
175 ///same for s1 and s3
176 else if(((*(table+s1))%3==(*(table+s3))%3)&&(*(table+s1))%3!=0&&(*(table+s2))%3==0)
177 return s2;
178 ///and for s2 and s3
179 else if(((*(table+s3))%3==(*(table+s2))%3)&&(*(table+s2))%3!=0&&(*(table+s1))%3==0)
180 return s1;
181 ///returns 0 if nothing is true
182 return 0;
183}
184void move(int pic,int *table,char first)
185{
186 char i,j,temp_playing_position,playing_position=-1;
187 ///checking if there are two same shapes in a row so that cpu to win or to block(first to win then to block)
188 for(j=2;j>=1;j--)
189 {
190 if((temp_playing_position=check(table,0,1,2))&&((*table)%3==j||(*(table+1))%3==j))
191 {
192 playing_position=temp_playing_position;
193 (*(table+playing_position))+=2;
194 cpu_update(pic,table,playing_position);
195 return;
196 }
197 else if((temp_playing_position=check(table,3,4,5))&&((*(table+3))%3==j||(*(table+4))%3==j))
198 {
199 playing_position=temp_playing_position;
200 (*(table+playing_position))+=2;
201 cpu_update(pic,table,playing_position);
202 return;
203 }
204 else if((temp_playing_position=check(table,6,7,8))&&((*(table+6))%3==j||(*(table+7))%3==j))
205 {
206 playing_position=temp_playing_position;
207 (*(table+playing_position))+=2;
208 cpu_update(pic,table,playing_position);
209 return;
210 }
211 else if((temp_playing_position=check(table,0,3,6))&&((*table)%3==j||(*(table+3))%3==j))
212 {
213 playing_position=temp_playing_position;
214 (*(table+playing_position))+=2;
215 cpu_update(pic,table,playing_position);
216 return;
217 }
218 else if((temp_playing_position=check(table,1,4,7))&&((*(table+1))%3==j||(*(table+4))%3==j))
219 {
220 playing_position=temp_playing_position;
221 (*(table+playing_position))+=2;
222 cpu_update(pic,table,playing_position);
223 return;
224 }
225 else if((temp_playing_position=check(table,2,5,8))&&((*(table+2))%3==j||(*(table+5))%3==j))
226 {
227 playing_position=temp_playing_position;
228 (*(table+playing_position))+=2;
229 cpu_update(pic,table,playing_position);
230 return;
231 }
232 else if((temp_playing_position=check(table,0,4,8))&&((*table)%3==j||(*(table+4))%3==j))
233 {
234 playing_position=temp_playing_position;
235 (*(table+playing_position))+=2;
236 cpu_update(pic,table,playing_position);
237 return;
238 }
239 else if((temp_playing_position=check(table,2,4,6))&&((*(table+2))%3==j||(*(table+4))%3==j))
240 {
241 playing_position=temp_playing_position;
242 (*(table+playing_position))+=2;
243 cpu_update(pic,table,playing_position);
244 return;
245 }
246 }
247 ///counts the number of times that player has played at a corner and at an edge
248 char corner_counter=0;
249 char edge_counter=0;
250 for(i=0;i<9;i++)
251 if(i%2==1)
252 if((*(table+i))%3==1)
253 edge_counter+=1;
254 for(i=0;i<9;i++)
255 if(i%2==0&&i!=4)
256 if((*(table+i))%3==1)
257 corner_counter+=1;
258 if(first=='y'||first=='Y')
259 {
260 /**
261 checking for this position or anything symmetrical to it:
262 x | |
263 ---|---|---
264 | 0 |
265 ---|---|---
266 | x |
267 so that it won't play at any losing position
268 */
269 if(playing_position==-1&&corner_counter==1&&edge_counter==1)
270 {
271 if(((*(table))%3==1&&(*(table+7))%3==1)||((*(table+8))%3==1&&(*(table+3))%3==1))
272 {
273 (*(table+6))+=2;
274 playing_position=6;
275 cpu_update(pic,table,playing_position);
276 return;
277 }
278 else if(((*(table))%3==1&&(*(table+5))%3==1)||((*(table+8))%3==1&&(*(table+1))%3==1))
279 {
280 (*(table+2))+=2;
281 playing_position=2;
282 cpu_update(pic,table,playing_position);
283 return;
284 }
285 else if(((*(table+2))%3==1&&(*(table+7))%3==1)||((*(table+6))%3==1&&(*(table+5))%3==1))
286 {
287 (*(table+8))+=2;
288 playing_position=8;
289 cpu_update(pic,table,playing_position);
290 return;
291 }
292 else if(((*(table+2))%3==1&&(*(table+3))%3==1)||((*(table+6))%3==1&&(*(table+1))%3==1))
293 {
294 (*(table))+=2;
295 playing_position=0;
296 cpu_update(pic,table,playing_position);
297 return;
298 }
299 }
300 /**
301 checking for this position or anything symmetrical to it:
302 x | |
303 ---|---|---
304 | 0 |
305 ---|---|---
306 | | x
307 so that it won't play at any losing position
308 */
309 if(playing_position==-1&&corner_counter==2&&edge_counter==0)
310 {
311 (*(table+1))+=2;
312 playing_position=1;
313 cpu_update(pic,table,playing_position);
314 return;
315 }
316 /**
317 checking for this position:
318 | |
319 ---|---|---
320 | 0 | x
321 ---|---|---
322 | x |
323 so that it won't play at any losing position
324 */
325 if(playing_position==-1&&edge_counter==2&&corner_counter==0&&(*(table+5))%3==1)
326 {
327 (*(table+8))+=2;
328 playing_position=8;
329 cpu_update(pic,table,playing_position);
330 return;
331 }
332 }
333 else
334 {
335 /**checks this position
336 0 | X |
337 ---|---|---
338 | 0 |
339 ---|---|---
340 | | X
341 */
342 if(edge_counter==1&&corner_counter==1&&(*(table+1))%3==1&&(*(table+8))%3==1&&(*(table+4))%3==2)
343 {
344 (*(table+6))+=2;
345 playing_position=6;
346 cpu_update(pic,table,playing_position);
347 return;
348 }
349 /**checks this position
350 0 | X | 0
351 ---|---|---
352 | |
353 ---|---|---
354 | | X
355 */
356 if(edge_counter==1&&corner_counter==1&&(*(table+1))%3==1&&(*(table+8))%3==1&&(*(table+2))%3==2)
357 {
358 (*(table+6))+=2;
359 playing_position=6;
360 cpu_update(pic,table,playing_position);
361 return;
362 }
363 ///plays the first move
364 if((*(table+4))%3==1&&edge_counter==0&&corner_counter==0)
365 {
366 (*(table+8))+=2;
367 playing_position=8;
368 cpu_update(pic,table,playing_position);
369 return;
370 }
371 else if((*(table+4))%3==0&&edge_counter+corner_counter==1)
372 {
373 if(corner_counter==1)
374 {
375 if((*(table+8))%3==0)
376 {
377 (*(table+8))+=2;
378 playing_position=8;
379 cpu_update(pic,table,playing_position);
380 return;
381 }
382 else
383 {
384 (*(table+2))+=2;
385 playing_position=2;
386 cpu_update(pic,table,playing_position);
387 return;
388 }
389 }
390 else
391 {
392 (*(table+4))+=2;
393 playing_position=4;
394 cpu_update(pic,table,playing_position);
395 return;
396 }
397 }
398 }
399 ///if it didn't yet play:
400 if(playing_position==-1)
401 {
402 ///plays firstly in middle if free, else in first free corner then edge
403 if((*(table+4))%3==0)
404 {
405 (*(table+4))+=2;
406 cpu_update(pic,table,4);
407 return;
408 }
409 else if((*(table))%3==0)
410 {
411 (*(table))+=2;
412 cpu_update(pic,table,0);
413 return;
414 }
415 else if((*(table+2))%3==0)
416 {
417 (*(table+2))+=2;
418 cpu_update(pic,table,2);
419 return;
420 }
421 else if((*(table+6))%3==0)
422 {
423 (*(table+6))+=2;
424 cpu_update(pic,table,6);
425 return;
426 }
427 else if((*(table+8))%3==0)
428 {
429 (*(table+8))+=2;
430 cpu_update(pic,table,8);
431 return;
432 }
433 else if((*(table+1))%3==0)
434 {
435 (*(table+1))+=2;
436 cpu_update(pic,table,1);
437 return;
438 }
439 else if((*(table+3))%3==0)
440 {
441 (*(table+3))+=2;
442 cpu_update(pic,table,3);
443 return;
444 }
445 else if((*(table+5))%3==0)
446 {
447 (*(table+5))+=2;
448 cpu_update(pic,table,5);
449 return;
450 }
451 else if((*(table+7))%3==0)
452 {
453 (*(table+7))+=2;
454 cpu_update(pic,table,7);
455 return;
456 }
457 else
458 {
459 system("cls");
460 printf("ERROR");
461 }
462 }
463 return;
464}
465void square(int pic[20][26],int *table,char last_square_position)
466{
467 player_update(pic,table,last_square_position);
468 int i,square_position;
469 ///finds square position
470 for(i=0;i<9;i++)
471 if((*(table+i))>2)
472 square_position=i;
473 ///fixes first row of the square
474 {
475 pic[square_position/3*(6+1)][(square_position%3)*(8+1)]=218;
476 for(i=1;i<=8-2;i++)
477 pic[square_position/3*(6+1)][(square_position%3)*(8+1)+i]=196;
478 pic[square_position/3*(6+1)][(square_position%3)*(8+1)+8-1]=191;
479 }
480 ///fixes last row of the square
481 {
482 pic[square_position/3*(6+1)+6-1][(square_position%3)*(8+1)]=192;
483 for(i=1;i<=8-2;i++)
484 pic[square_position/3*(6+1)+6-1][(square_position%3)*(8+1)+i]=196;
485 pic[square_position/3*(6+1)+6-1][(square_position%3)*(8+1)+8-1]=217;
486 }
487 ///fixes everything in between
488 {
489 for(i=1;i<=6-2;i++)
490 {
491 pic[square_position/3*(6+1)+i][(square_position%3)*(8+1)]=179;
492 pic[square_position/3*(6+1)+i][(square_position%3)*(8+1)+8-1]=179;
493 }
494 }
495 ///prints the new tic tac toe board
496 print(pic);
497 return;
498}
499void wasd(int pic[20][26],int *table,char players,char first,char username1[17],char username2[17])
500{
501 char square_position=-1,last_square_position,user_file[17];
502 int win,draw,loss;
503 unsigned char i=0,a,winner,counter=1;
504 FILE *somefile;
505 ///finds square position before it changes (because the last square position must be erased)
506 for(i=0;i<9&&!(square_position+1);i++)
507 if((*(table+i))>2)
508 square_position=i;
509 last_square_position=square_position;
510 a=getch();
511 ///loop finishes only when game has been won or lost with break
512 while(1)
513 {
514 ///finds where to move square and saves the last square position
515 if((a=='d'||a=='D')&&(square_position%3!=2))
516 {
517 (*(table+square_position))-=3;
518 (*(table+square_position+1))+=3;
519 last_square_position=square_position;
520 square_position+=1;
521 }
522 else if((a=='a'||a=='A')&&(square_position%3!=0))
523 {
524 (*(table+square_position))-=3;
525 (*(table+square_position-1))+=3;
526 last_square_position=square_position;
527 square_position-=1;
528 }
529 else if((a=='s'||a=='S')&&(square_position<6))
530 {
531 (*(table+square_position))-=3;
532 (*(table+square_position+3))+=3;
533 last_square_position=square_position;
534 square_position+=3;
535 }
536 else if((a=='w'||a=='W')&&(square_position>2))
537 {
538 (*(table+square_position))-=3;
539 (*(table+square_position-3))+=3;
540 last_square_position=square_position;
541 square_position-=3;
542 }
543 ///if player against cpu
544 else if(a==' '&&(*(table+square_position))%3==0&&players=='1')
545 {
546 ///player plays
547 (*(table+square_position))+=1;
548 ///cpu plays if game hasn't been ended and it saves the winner
549 if((winner=end(table)))
550 {
551 ///shows last tic tac toe board
552 player_update(pic,table,last_square_position);
553 system("cls");
554 square(pic,table,last_square_position);
555 break;
556 }
557 move(pic,table,first);
558 player_update(pic,table,last_square_position);
559 }
560 ///if player against player
561 else if(a==' '&&(*(table+square_position))%3==0&&players=='2')
562 {
563 ///adds 1 or 2 alternatively in a position so that players play alternatively(1 for X and 2 for O)
564 counter++;
565 (*(table+square_position))+=counter%2+1;
566 player_update(pic,table,last_square_position);
567 }
568 ///print new board
569 system("cls");
570 square(pic,table,last_square_position);
571 ///if at an end it stops else it continues
572 if((winner=end(table)))
573 break;
574 else
575 a=getch();
576 }
577 ///prints who won if any
578 if(winner==1&&players=='2')
579 {
580 printf(username1);
581 printf(" WINS!!!\n");
582 printf(username2);
583 printf(" LOST!!!\n");
584 sprintf(user_file,"%s.txt",username1);
585 somefile=fopen(user_file,"r");
586 fscanf(somefile,"%d|%d|%d",&win,&draw,&loss);
587 fclose(somefile);
588 somefile=fopen(user_file,"w");
589 fprintf(somefile,"%d|%d|%d",win+1,draw,loss);
590 fclose(somefile);
591 printf(username1);
592 printf(" HAS %d WINS, %d DRAWS AND %d LOSSES\n",win+1,draw,loss);
593 sprintf(user_file,"%s.txt",username2);
594 somefile=fopen(user_file,"r");
595 fscanf(somefile,"%d|%d|%d",&win,&draw,&loss);
596 fclose(somefile);
597 somefile=fopen(user_file,"w");
598 fprintf(somefile,"%d|%d|%d",win,draw,loss+1);
599 fclose(somefile);
600 printf(username2);
601 printf(" HAS %d WINS, %d DRAWS AND %d LOSSES\n",win,draw,loss+1);
602 }
603 else if(winner==2&&players=='2')
604 {
605 printf(username2);
606 printf(" WINS!!!\n");
607 printf(username1);
608 printf(" LOST!!!\n");
609 sprintf(user_file,"%s.txt",username2);
610 somefile=fopen(user_file,"r");
611 fscanf(somefile,"%d|%d|%d",&win,&draw,&loss);
612 fclose(somefile);
613 somefile=fopen(user_file,"w");
614 fprintf(somefile,"%d|%d|%d",win+1,draw,loss);
615 fclose(somefile);
616 printf(username2);
617 printf(" HAS %d WINS, %d DRAWS AND %d LOSSES\n\n",win+1,draw,loss);
618 sprintf(user_file,"%s.txt",username1);
619 somefile=fopen(user_file,"r");
620 fscanf(somefile,"%d|%d|%d",&win,&draw,&loss);
621 fclose(somefile);
622 somefile=fopen(user_file,"w");
623 fprintf(somefile,"%d|%d|%d",win,draw,loss+1);
624 fclose(somefile);
625 printf(username1);
626 printf(" HAS %d WINS, %d DRAWS AND %d LOSSES\n",win,draw,loss+1);
627 }
628 else if(winner==3)
629 {
630 printf("DRAW!\n");
631 sprintf(user_file,"%s.txt",username1);
632 somefile=fopen(user_file,"r");
633 fscanf(somefile,"%d|%d|%d",&win,&draw,&loss);
634 fclose(somefile);
635 somefile=fopen(user_file,"w");
636 fprintf(somefile,"%d|%d|%d",win,draw+1,loss);
637 fclose(somefile);
638 printf(username1);
639 printf(" HAS %d WINS, %d DRAWS AND %d LOSSES\n",win,draw+1,loss);
640 }
641 else if(winner==1)
642 {
643 printf("YOU WON!!!\n");
644 sprintf(user_file,"%s.txt",username1);
645 somefile=fopen(user_file,"r");
646 fscanf(somefile,"%d|%d|%d",&win,&draw,&loss);
647 fclose(somefile);
648 somefile=fopen(user_file,"w");
649 fprintf(somefile,"%d|%d|%d",win+1,draw,loss);
650 fclose(somefile);
651 printf(username1);
652 printf(" HAS %d WINS, %d DRAWS AND %d LOSSES\n",win+1,draw,loss);
653 }
654 else if(winner==2)
655 {
656 printf("YOU LOST\n");
657 sprintf(user_file,"%s.txt",username1);
658 somefile=fopen(user_file,"r");
659 fscanf(somefile,"%d|%d|%d",&win,&draw,&loss);
660 fclose(somefile);
661 somefile=fopen(user_file,"w");
662 fprintf(somefile,"%d|%d|%d",win,draw,loss+1);
663 fclose(somefile);
664 printf(username1);
665 printf(" HAS %d WINS, %d DRAWS AND %d LOSSES\n",win,draw,loss+1);
666 }
667 system("pause");
668 ///when player presses any key cpu will clear the screen
669 system("cls");
670 ///prints GAME OVER(but in a cool fashion)
671 printf("\n /%c%c%c%c%c\\ /\\ %c\\ /%c %c%c%c%c%c%c\n",238,238,238,238,238,179,179,179,238,238,238,238,238);
672 printf(" %c / \\ %c \\ / %c %c\n",179,179,179,179);
673 printf(" %c ___ /%c%c%c%c\\ %c \\/ %c %c%c%c%c%c%c\n",179,196,196,196,196,179,179,179,196,196,196,196,196);
674 printf(" %c %c / \\ %c %c %c\n",179,179,179,179,179);
675 printf(" \\_____/ / \\ %c %c %c_____\n\n\n",179,179,179);
676 printf(" /%c%c%c%c\\ \\ / %c%c%c%c%c%c %c%c%c\\\n",238,238,238,238,179,238,238,238,238,238,179,238,238);
677 printf(" %c %c \\ / %c %c )\n",179,179,179,179);
678 printf(" %c %c \\ / %c%c%c%c%c%c %c__/\n",179,179,179,196,196,196,196,196,179);
679 printf(" %c %c \\ / %c %c \\\n",179,179,179,179);
680 printf(" \\____/ \\/ %c_____ %c \\\n\n\n ",179,179);
681 printf("PLAY AGAIN? Y/N\n");
682 a=getch();
683 ///checking player's input
684 while(a!='y' && a!='n' && a!='Y' && a!='N')
685 {
686 printf("PLEASE ENTER Y FOR YES OR N FOR NO\n");
687 a=getch();
688 }
689 ///if answer is yes starts again
690 if(a=='y' || a=='Y')
691 {
692 system("cls");
693 main();
694 }
695 return;
696}
697void get_string(char *username)
698{
699 unsigned flag=0,i;
700 while(!flag)
701 {
702 scanf("%s",username);
703 username=(char*)realloc(username,strlen(username)+1);
704 fflush(stdin);
705 while(*username=='\0')
706 {
707 printf("USERNAME MUST BE AT LEAST 1 CHARACTER LONG: \n");
708 scanf("%s",username);
709 username=(char*)realloc(username,strlen(username)+1);
710 fflush(stdin);
711 }
712 flag=1;
713 for(i=0;username[i]!='\0';i++)
714 if(!((username[i]>='a'&&username[i]<='z')||(username[i]>='A'&&username[i]<='Z')||(username[i]>='0'&&username[i]<='9')||username[i]=='_'))
715 flag=0;
716 if(!flag)
717 printf("CHARCTERS MUST BE A LETTER, A NUMBER OR UNDERSCORE:\n");
718 }
719}
720void enter_name(char player,char *username,char number_of_players)
721{
722 char temp_char, *user_file, *temp_username,flag;
723 FILE *somefile;
724 user_file=(char *)malloc(1000*sizeof(char));
725 unsigned char i;
726 strcpy(temp_username,username);
727 system("cls");
728 if(number_of_players=='1')
729 printf("PLEASE ENTER YOUR USERNAME: \n");
730 else
731 {
732 printf("PLEASE ENTER WHO PLAYS ");
733 if(player==1)
734 printf("FIRSTLY:\n");
735 else
736 printf("SECONDLY:\n");
737 }
738 fflush(stdin);
739 get_string(username);
740 if(player==2)
741 while(!(strcmp(temp_username,username)))
742 get_string(username);
743 ///checks if file exists
744 sprintf(user_file, "%s.txt", username);
745 somefile = fopen(user_file,"r");
746 if(somefile==NULL)
747 {
748 while(somefile==NULL)
749 {
750 printf("USERNAME DOESN'T EXIST.\nDO YOU WANT TO CREATE NEW? Y/N\n");
751 temp_char=getch();
752 while(temp_char!='n' && temp_char!='N' && temp_char!='y' && temp_char!='Y')
753 {
754 printf("PLEASE ENTER Y FOR YES OR N FOR NO\n");
755 temp_char=getch();
756 }
757 if(temp_char=='y' || temp_char=='Y')
758 {
759 somefile = fopen(user_file, "w");
760 fprintf(somefile,"0|0|0");
761 fclose(somefile);
762 break;
763 }
764 else
765 {
766 system("cls");
767 if(number_of_players=='1')
768 printf("PLEASE ENTER YOUR USERNAME: \n");
769 else
770 {
771 printf("PLEASE ENTER WHO PLAYS ");
772 if(player==1)
773 printf("FIRSTLY:\n");
774 else
775 printf("SECONDLY:\n");
776 }
777 get_string(username);
778 if(player==2)
779 while(!(strcmp(temp_username,username)))
780 {
781 printf("USERNAMES MUST BE DIFFERENT:\n");
782 get_string(username);
783 }
784 sprintf(user_file, "%s.txt", username);
785 somefile = fopen(user_file,"r");
786 continue;
787 }
788 }
789 }
790 else
791 {
792 system("cls");
793 printf("WELCOME BACK ");
794 printf(username);
795 printf("\n");
796 fclose(somefile);
797 system("pause");
798 }
799 return;
800}
801void print_statistics()
802{
803 FILE *user_file;
804 char *username,*temp_username,temp_char;
805 username=(char*)malloc(1004*sizeof(char));
806 temp_username=(char*)malloc(1000*sizeof(char));
807 int win,draw,loss;
808 system("cls");
809 printf("PLEASE ENTER A USERNAME:\n");
810 get_string(username);
811 sprintf(temp_username,"%s.txt",username);
812 temp_username=(char*)realloc(temp_username,strlen(temp_username)+1);
813 user_file = fopen(temp_username,"r");
814 while(user_file==NULL)
815 {
816 printf("USERNAME DOESN'T EXIST. \nDO YOU WANT TO ENTER A NEW USERNAME? Y/N\n");
817 temp_char=getch();
818 while(temp_char!='y'&&temp_char!='Y'&&temp_char!='n'&&temp_char!='N')
819 {
820 printf("ENTER Y FOR YES AND N FOR NO:\n");
821 temp_char=getch();
822 }
823 if(temp_char=='y'||temp_char=='Y')
824 {
825 system("cls");
826 printf("PLEASE ENTER A USERNAME:\n");
827 get_string(username);
828 user_file = fopen(username,"r");
829 continue;
830 }
831 else
832 return;
833 }
834 fscanf(user_file,"%d|%d|%d",&win,&draw,&loss);
835 printf(username);
836 printf(" HAS %d WINS, %d DRAWS AND %d LOSSES\n",win,draw,loss);
837 fclose(user_file);
838 system("pause");
839}
840int main(void)
841{
842 char players,first=-1,*username1,*username2;
843 username1=(char*)malloc(1000*sizeof(char));
844 username2=(char*)malloc(1000*sizeof(char));
845 unsigned char i,j;
846 int table[3][3],pic[20][26];
847 ///builds tic tac toe board
848 start(pic);
849 do
850 {
851 system("cls");
852 printf("1 -> PLAY AGAINST CPU\n2 -> PLAY AGAINST ANOTHER PLAYER\n3 -> VIEW STATISTICS\n4 -> QUIT\n");
853 players=getch();
854 while(players<'1' || players>'4')
855 {
856 printf("\nYOU MUST ENTER 1, 2, 3 OR 4: ");
857 players=getch();
858 }
859 if(players=='4')
860 return 0;
861 else if(players=='3')
862 print_statistics();
863 }while(players=='3');
864 ///accepts usernames and checks if they are already in use or not
865 enter_name(1,username1,players);
866 strcpy(username2,username1);
867 if(players=='2')
868 enter_name(2,username2,players);
869 ///sets every position to 0 (0 indicates nothing, 1 for X, 2 for O and an added 3 for the square
870 for(i=0;i<3;i++)
871 for(j=0;j<3;j++)
872 table[i][j]=0;
873 ///if against the cpu asks who will play first
874 if(players=='1')
875 {
876 printf("DO YOU WANT TO PLAY FIRST? Y/N\n");
877 first=getch();
878 while(first!='y'&&first!='Y'&&first!='n'&&first!='N')
879 {
880 printf("PLEASE ENTER Y FOR YES OR N FOR NO\n");
881 first=getch();
882 }
883 }
884 system("cls");
885 ///cpu plays at top left corner
886 if(first=='n'||first=='N')
887 {
888 table[0][0]+=2;
889 cpu_update(pic,table,0);
890 }
891 ///sets square at first position
892 table[0][0]+=3;
893 ///builts square and prints board
894 square(pic,table,1);
895 ///game starts
896 wasd(pic,table,players,first,username1,username2);
897}