forked from mr-sergi/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTictactoe.c
214 lines (202 loc) · 5.27 KB
/
Tictactoe.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
/*
This code has been compiled in Code::Blocks 16.01 IDE on Windows 10
Author:- Ranjeet Deshpande
*/
#include<stdio.h>
#include<string.h>
char x,o;
char a[9]={'1','2','3','4','5','6','7','8','9'};
char u1[50],u2[50];
void board();
void rules();
int checkforwin();
int main()
{
FILE *p;
p=fopen("score.txt","a+");
fclose(p);
system("color 09");
int player=1;
int choice,score=-1;
char symbol,re;
char start,dec;
int s;
rules();
printf("\n\nType 1 to start the game:-\nType 2 to view leader board:-\n");
scanf("%d",&s);
if(s==1)
{
read:
p=fopen("score.txt","a+");
printf("\nEnter name of player1: ");
scanf("%s",u1);
fprintf(p,"\n%s",u1);
printf("Enter name of player2: ");
scanf("%s",u2);
fprintf(p,"\t%s",u2);
fclose(p);
if(!strcmp(u1,u2))
{
printf("Enter names of different players!\n\n");
goto read;
}
else
decision();
system("color fc");
board();
do
{
player=((player%2)?1:2);
if(player==1)
printf("%s Type any digit from 1-9 to fill your response:- ",u1);
else
printf("%s Type any digit from 1-9 to fill your response:- ",u2);
scanf("%d",&choice);
symbol=((player==1)?x:o);
if(choice==1 && a[0]=='1')
a[0]=symbol;
else if(choice==2 && a[1]=='2')
a[1]=symbol;
else if(choice==3 && a[2]=='3')
a[2]=symbol;
else if(choice==4 && a[3]=='4')
a[3]=symbol;
else if(choice==5 && a[4]=='5')
a[4]=symbol;
else if(choice==6 && a[5]=='6')
a[5]=symbol;
else if(choice==7 && a[6]=='7')
a[6]=symbol;
else if(choice==8 && a[7]=='8')
a[7]=symbol;
else if(choice==9 && a[8]=='9')
a[8]=symbol;
else
{printf("Wrong Selection\n");player--;}
score=checkforwin();
player++;
board();
}while(score == -1);
p=fopen("score.txt","a+");
if(score==1)
{
if(player==2)
{printf("\n\nPlayer1 %s Wins!\n\n",u1);fprintf(p,"\t%s",u1);
getch();}
else
{printf("\n\nPlayer2 %s Wins!\n\n",u2);fprintf(p,"\t%s",u2);
getch();
}
fclose(p);
}
else
printf("\n\nGame Draws!\n\n");fprintf(p,"\t%s","DRAW");
getch();
}
if(s==2)
{
int cho;
system("cls");
printf("\n\n");
printf("\tLEADERBOARD\n\n");
char c;
p=fopen("score.txt","r");
while((c=getc(p))!=EOF)
{
printf("%c",c);
}
fclose(p);
printf("\n\nPress 1 to start the game:- ");
scanf("%d",&cho);
if(cho==1)
goto read;
else
getch();
}
else
{
printf("\n\nShould have typed 1 to play the game!\nHope to see you back soon!\n\n");
getch();
}
}
int checkforwin()
{
if(a[0]==a[1] && a[1]==a[2])
return 1;
else if(a[3]==a[4] && a[4]==a[5])
return 1;
else if(a[6]==a[7] && a[7]==a[8])
return 1;
else if(a[0]==a[3] && a[3]==a[6])
return 1;
else if(a[1]==a[4] && a[4]==a[7])
return 1;
else if(a[2]==a[5] && a[5]==a[8])
return 1;
else if(a[0]==a[4] && a[4]==a[8])
return 1;
else if(a[2]==a[4] && a[4]==a[6])
return 1;
else if(a[0]!='1' && a[1]!='2' && a[2]!='3' && a[3]!='4' && a[4]!='5' && a[5]!='6' && a[6]!='7' && a[7]!='8' && a[8]!='9')
return 0;
else
return -1;
}
void board()
{
int i;
system("cls");
printf("\tTic-Tac-Toe\n\n");
printf("\n\n");
printf("%s:- (%c)\n%s:- (%c)\n\n\n",u1,x,u2,o);
printf(" %c | %c | %c\n",a[0],a[1],a[2]);
printf(" | | \n");
printf("----|----|----\n");
printf(" | | \n");
printf(" %c | %c | %c\n",a[3],a[4],a[5]);
printf(" | | \n");
printf("----|----|----\n");
printf(" %c | %c | %c\n",a[6],a[7],a[8]);
printf(" | | \n");
}
void rules()
{
char link;
printf("\tTic-Tac-Toe\n\n");
printf("Welcome to the most played 2D game and a sort of fun using X and O\n\n");
printf("Rules:-\n");
printf("\n1:Each player will be entering the number to put respective X or O in the desired position");
printf("\n2:Player who gets a combination of 3 same characters either diagonal or horizontally or \n vertically will be declared as the winner");
printf("\n\nEnjoy the game! Be a Winner!\n\n");
printf("For more clarifications press Y else type any other character:- ");
scanf("%c",&link);
if(link=='y' || link=='Y')
{
system("start http://www.wikihow.com/Play-Tic-Tac-Toe");
}
}
int decision()
{
char dec;
deci:
printf("\n\nPlayer1 %s choose the X or 0:",u1);
dec=getchar();
scanf("%c",&dec);
{
if(dec=='X' || dec=='x')
{
x='X';
o='0';
}
else if(dec=='0')
{
x='0';
o='X';
}
else
{
printf("Please enter either X or 0 only \n\n");
goto deci;
}
}
}