forked from usmanmumtaz/Tic-Tac-Toe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
396 lines (380 loc) · 12.7 KB
/
game.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <stdio.h>
int main()
{ //Initialize most outer loop that brings the program to the main menu.
int oooloop,rtrn,rptt1=1,rptt2=9; // Initialize variables.
for (oooloop=0; oooloop<1; oooloop++){ // Initialize loop that will bring program back to main menu if wrong input is entered.
int ooloop,mm;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); // Creating User Interface.
printf("***MAIN MENU***\n\nSingle Player: 1\n\nMultiplayer: 2\n\nHow to play: 3\n\nQuit: 4");
printf("\n\n\n\n\n\n");
printf("\t\t:\t\t:\t\t\n");
printf("\tX\t:\tO\t:\tX\t\n");
printf("\t\t:\t\t:\t\t\n");
printf(" -----------------------------------------\n");
printf("\t\t:\t\t:\t\t\n");
printf("\tO\t:\tX\t:\tO\t\n");
printf("\t\t:\t\t:\t\t\n");
printf(" -----------------------------------------\n");
printf("\t\t:\t\t:\t\t\n");
printf("\tX\t:\tO\t:\tX\t\n");
printf("\t\t:\t\t:\t\t\n");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("Enter your choice: ");
scanf("%d",&mm); // Taking input of main menu.
const int cnst=mm;
if (mm<1 || mm>4){ // Check if there is any wrong input.
printf("Wrong input.\n");
oooloop--; // Go back to main menu.
continue;
}
if (mm==4){break;} // Exit Game.
// Initializing Game.
if (mm==2 || mm==1){ // Checks the MAIN MENU option selected by the user and runs Single or Multiplayer game.
for (ooloop=0; ooloop<1; ooloop++){
char chr,ui,uc,X,arry[3][3]={{'\0','\0','\0'},{'\0','\0','\0'},{'\0','\0','\0'}};
// "arry" is a 2-dimentional array which will bear my original "TIC-TAC-TOE ALGORITHM".
int ctod,loop;
printf("Select your character (O/X): ");
scanf("%s",&uc); // Taking input to select user's character in the game.
ctod=uc;
if (ctod!=120 && ctod!=88 && ctod!=111 && ctod!=79){ //check for wrong input of character.
printf("Wrong Input.\nYou are given 'X' by default.\n");
uc='X'; // If user enters wrong input 'X' will be given by default.
X='O';
}
else if (ctod==120 || ctod==88){ // Assigning characters.
X='O';
uc='X';
}
else {
X='X';
uc='O';
}
//Characters has been assigned.
for (loop=rptt1; loop<=rptt2; loop++){ // This loop is iterating the whole structure.
int stop=0; // Check Variable to exit game when array is filled.
if (loop%2==0 && cnst==2){ // Player 2 turn if Multiplayer game is running.
chr=X; // "chr" is the variable which will be printed in game. It will have corresponding characters according to 1st player, 2nd player or computer's turn.
printf("Player 2 turn: ");
scanf("%d",&ui); // Takes input at which point Player 2 has to mark.
}
else if (loop%2==0 && cnst==1){ // Assigning Computer's character to "chr" when singe player game is running.
chr=X;
printf("Computer: This is from my side...\n");
// Games's Algorithm starts here.
int lp;
for (lp=1; lp<=2; lp++){
if (stop>0){break;}
int ii,jj,k,rslt=9,rslt2=9,rslt3=9,rslt4=3,r2,r3,r4;
char chkk;
if (lp==1){chkk=X;}
if(lp==2){chkk=uc;}
if (stop>0){break;}
for (ii=2; ii>=0; ii--){
if (stop>0){break;}
r2=2;
k=0;
for (jj=0; jj<2; jj++){
if (arry[ii][jj]==arry[ii][k+1] && arry[ii][k+1]==chkk){
if (arry[ii][r2]=='\0'){
ui = rslt;
stop++;
break;
}
}
if (arry[jj][ii]==arry[k+1][ii] && arry[k+1][ii]==chkk){
if(arry[r2][ii]=='\0'){
ui = rslt2;
stop++;
break;
}
}
if (ii==2){
if (arry[jj][jj]==arry[k+1][k+1] && arry[k+1][k+1]==chkk){
if(arry[r2][r2]=='\0'){
ui = rslt3;
stop++;
break;
}
}
}
if (k==0){
jj--;
k++;
}
r2--;
rslt--;
rslt2-=3;
rslt3-=4;
}
rslt2+=8;
}
if (arry[2][0]==arry[1][1] && arry[1][1]==chkk){
if (arry[0][2]=='\0'){stop++; ui = 3;}
}
if (arry[2][0]==arry[0][2] && arry[0][2]==chkk){
if (arry[1][1]=='\0'){stop++; ui = 5;}
}
if (arry[0][2]==arry[1][1] && arry[1][1]==chkk){
if (arry[2][0]=='\0'){stop++; ui = 7;}
}
}
if (stop==0){
int lp;
for (lp=1; lp<=4; lp++){
if (stop>0){break;}
int ii,jj,k,rslt=9,rslt2=9,rslt3=9,rslt4=3,r2,r3,r4;
char chkk,chkkk;
if (lp==1){chkk=X;chkkk='\0';}
if(lp==2){chkk='\0';chkkk=X;}
if(lp==3){chkk=uc;chkkk=X;}
if(lp==4){chkk=X;chkkk=uc;}
if (stop>0){break;}
for (ii=2; ii>=0; ii--){
if (stop>0){break;}
r2=2;
k=0;
for (jj=0; jj<2; jj++){
if (arry[ii][jj]==chkk && arry[ii][k+1]==chkkk){
if (arry[ii][r2]=='\0'){
if (lp==1){ui=((ii*3)+k+2);}
else{ui = rslt;}
stop++;
break;
}
}
if (arry[jj][ii]==chkk && arry[k+1][ii]==chkkk){
if(arry[r2][ii]=='\0'){
if (lp==1){ui=(((k+1)*3)+ii+1);}
else{ui = rslt2;}
stop++;
break;
}
}
if (ii==2){
if (arry[jj][jj]==chkk && arry[k+1][k+1]==chkkk){
if(arry[r2][r2]=='\0'){
if (lp==1){ui=(((k+1)*3)+k+2);}
else{ui = rslt3;}
stop++;
break;
}
}
}
if (k==0){
jj--;
k++;
}
r2--;
rslt--;
rslt2-=3;
rslt3-=4;
}
rslt2+=8;
}
if (arry[2][0]==chkk && arry[1][1]=='\0'){
if (arry[0][2]=='\0'){stop++; ui = 5;}
}
if (arry[0][2]==chkk && arry[1][1]=='\0'){
if (arry[2][0]=='\0'){stop++; ui = 5;}
}
if (arry[2][0]=='\0' && arry[1][1]==chkk){
if (arry[0][2]=='\0'){stop++; ui = 3;}
}
if (arry[2][0]=='\0' && arry[0][2]==chkk){
if (arry[1][1]=='\0'){stop++; ui = 5;}
}
if (arry[0][2]=='\0' && arry[1][1]==chkk){
if (arry[2][0]=='\0'){stop++; ui = 7;}
}
}
}
}
else {
chr=uc;
if(cnst==1){printf("Your Turn: \n\n\n\n");}
else {printf("Player 1 turn: \n\n\n\n");}
scanf("%d",&ui);
}
if (ui!=1 && ui!=2 && ui!=3 && ui!=4 && ui!=5 && ui!=6 && ui!=7 && ui!=8 && ui!=9){
printf("Wrong input.\nInput a number from 0 to 9.\n");
loop--;
continue;
}
if (ui==1){
if (arry[0][0]=='\0'){
arry[0][0]=chr;
}
else{
printf("Already inputed\n");
loop--;
continue;
}
}
else if (ui==2){
if (arry[0][1]=='\0'){
arry[0][1]=chr;
}
else{
printf("Already inputed\n");
loop--;
continue;
}
}
else if (ui==3){
if (arry[0][2]=='\0'){
arry[0][2]=chr;
}
else{
printf("Already inputed\n");
loop--;
continue;
}
}
else if (ui==4){
if (arry[1][0]=='\0'){
arry[1][0]=chr;
}
else{
printf("Already inputed\n");
loop--;
continue;
}
}
else if (ui==5){
if (arry[1][1]=='\0'){
arry[1][1]=chr;
}
else{
printf("Already inputed\n");
loop--;
continue;
}
}
else if (ui==6){
if (arry[1][2]=='\0'){
arry[1][2]=chr;
}
else{
printf("Already inputed\n");
loop--;
continue;
}
}
else if (ui==7){
if (arry[2][0]=='\0'){
arry[2][0]=chr;
}
else{
printf("Already inputed\n");
loop--;
continue;
}
}
else if (ui==8){
if (arry[2][1]=='\0'){
arry[2][1]=chr;
}
else{
printf("Already inputed\n");
loop--;
continue;
}
}
else if (ui==9){
if (arry[2][2]=='\0'){
arry[2][2]=chr;
}
else{
printf("\nAlready inputed\n");
loop--;
continue;
}
}
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\t\t\t%c\t:\t%c\t:\t%c\t\n",arry[0][0],arry[0][1],arry[0][2]);
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\t\t -----------------------------------------\n");
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\t\t\t%c\t:\t%c\t:\t%c\t\n",arry[1][0],arry[1][1],arry[1][2]);
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\t\t -----------------------------------------\n");
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\t\t\t%c\t:\t%c\t:\t%c\t\n",arry[2][0],arry[2][1],arry[2][2]);
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
if (loop>=3){
if ((arry[0][0]==arry[0][1] && arry[0][1]==arry[0][2] && arry[0][2]!='\0')||(arry[0][0]==arry[1][0] && arry[1][0]==arry[2][0] && arry[2][0]!='\0')||(arry[0][0]==arry[1][1] && arry[1][1]==arry[2][2] && arry[2][2]!='\0')||(arry[0][1]==arry[1][1] && arry[1][1]==arry[2][1] && arry[2][1]!='\0')||(arry[0][2]==arry[1][2] && arry[1][2]==arry[2][2] && arry[2][2]!='\0')||(arry[0][2]==arry[1][1] && arry[1][1]==arry[2][0] && arry[2][0]!='\0')||(arry[1][0]==arry[1][1] && arry[1][1]==arry[1][2] && arry[1][2]!='\0')||(arry[2][0]==arry[2][1] && arry[2][1]==arry[2][2] && arry[2][2]!='\0')){
if (loop%2==0){
if (cnst==1){printf("Computer: I Wins!!\n\n\n\n");}
else{printf("Player 2 Wins!!\n\n\n\n");}
X='E';
break;
}
else{
if (cnst==1){printf("You Win!!\n\n\n\n");}
else{printf("Player 1 Wins!!\n\n\n\n");}
X='E';
break;
}
}
}
}
if (X!='E'){printf("Game Draw!!\n");}
for (loop=0; loop<1; loop++){
printf("Replay: 1\nReturn to main menu: 2\nQuit: 3\n");
printf("Enter your choice: ");
scanf("%d",&rtrn);
if (rtrn!=1 && rtrn!=2 && rtrn!=3){
printf("Wrong input.\n");
loop-=1;
continue;
}
if (rtrn==1){
ooloop-=1;
if (cnst==1){rptt1++;rptt2++;}
}
}
if(rtrn==2){oooloop-=1;}
}
}
if (mm==3){
int loophtp;
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf("1-Select your character 'X' or 'O'.\n\n");
printf("2-If you enter anything other than 'O' or 'X' you will be given 'X' by default.\n\n");
printf("3-Enter the number of the location where you want to put your character.\n\n");
printf("Numbers of locations are as follows: \n\n");
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\t\t\t1\t:\t2\t:\t3\t\n");
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\t\t -----------------------------------------\n");
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\t\t\t4\t:\t5\t:\t6\t\n");
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\t\t -----------------------------------------\n");
printf("\t\t\t\t:\t\t:\t\t\n");
printf("\t\t\t7\t:\t8\t:\t9\t\n");
printf("\t\t\t\t:\t\t:\t\t\n\n");
printf("4-If you enter a character while entering the number of location game will crash.\n\n");
printf("5-If you are playing with computer and play a 'replay', Computer will play first turn in 2nd game.\n\n");
printf("6-If you enter more than one character at a time, game may crash.\n\n");
printf("7-Not so hard game that you are reading the instructions so carefully.\n\n");
printf("Go play the game everything will be revealed on you with experience.\n\n");
printf("Wanna play??...Enter 1..\n");
printf("Wanna Quit??...Enter 2..\n");
printf("Choice is the only thing that is yours.");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
for (loophtp=0; loophtp<1; loophtp++){
printf("Enter your choice: ");
scanf ("%d",&rtrn);
if (rtrn!=1 && rtrn!=2){
printf("Wrong choice.\n\n");
loophtp--;
}
if (rtrn==1){oooloop--;}
}
}
}
}