Skip to content

Commit 0a3a977

Browse files
authored
Merge pull request #20 from shubhranshii/game
basic program to create TicTacToe in C
2 parents 6a39240 + fbf86de commit 0a3a977

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

TicTacToe in C

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<windows.h>
4+
#include<stdlib.h>
5+
6+
char square[10]= {'0','1','2','3','4','5','6','7','8','9'};
7+
int checkWin();
8+
void gameBoard();
9+
10+
int main()
11+
{
12+
int player=1, i, choice;
13+
char mark; //to store X or O
14+
do
15+
{
16+
gameBoard();
17+
player=(player % 2)?1:2;
18+
printf("player %d enter your choice : ", player);
19+
scanf("%d", &choice);
20+
21+
mark= (player==1)? 'X':'O';
22+
if(choice==1 && square[1]=='1')
23+
square[1]=mark;
24+
else if(choice==2 && square[2]=='2')
25+
square[2]=mark;
26+
else if(choice==3 && square[3]=='3')
27+
square[3]=mark;
28+
else if(choice==4 && square[4]=='4')
29+
square[4]=mark;
30+
else if(choice==5 && square[5]=='5')
31+
square[5]=mark;
32+
else if(choice==6 && square[6]=='6')
33+
square[6]=mark;
34+
else if(choice==7 && square[7]=='7')
35+
square[7]=mark;
36+
else if(choice==8 && square[8]=='8')
37+
square[8]=mark;
38+
else if(choice==9 && square[9]=='9')
39+
square[9]=mark;
40+
else
41+
{
42+
printf("invalid choice");
43+
player--;
44+
getch();
45+
}
46+
i=checkWin();
47+
player++;
48+
49+
}while (i==-1);
50+
51+
gameBoard();
52+
if(i==1)
53+
{
54+
printf("player %d has won", --player);
55+
}
56+
else
57+
{
58+
printf("game is a draw");
59+
}
60+
getch();
61+
return 0;
62+
}
63+
int checkWin()
64+
{
65+
if(square[1]==square[2] && square[2]==square[3])
66+
return 1;
67+
else if (square[4]==square[5] && square[5]==square[6])
68+
return 1;
69+
else if (square[7]==square[8] && square[8]==square[9])
70+
return 1;
71+
else if (square[1]==square[4] && square[4]==square[7])
72+
return 1;
73+
else if (square[2]==square[5] && square[5]==square[8])
74+
return 1;
75+
else if (square[3]==square[6] && square[6]==square[9])
76+
return 1;
77+
else if (square[1]==square[5] && square[5]==square[9])
78+
return 1;
79+
else if (square[3]==square[5] && square[5]==square[7])
80+
return 1;
81+
else if (square[1]!='1' && square[2]!='2' && square[3]!='3' && square[4]!='4' && square[5]!='5' && square[6]!='6' && square[7]!='7' && square[8]!='8' && square[9]!='9')
82+
return 0;
83+
else
84+
return -1;
85+
}
86+
87+
void gameBoard()
88+
{
89+
system("cls");
90+
printf("\n\n\t TicTacToe \n\n");
91+
printf("Player1(X)-Player2(O) \n\n");
92+
printf(" | | \n");
93+
printf(" %c | %c | %c \n", square[1], square[2], square[3]);
94+
printf("_____|_____|_____\n");
95+
printf(" | | \n");
96+
printf(" %c | %c | %c \n", square[4], square[5], square[6]);
97+
printf("_____|_____|_____\n");
98+
printf(" | | \n");
99+
printf(" %c | %c | %c \n", square[7], square[8], square[9]);
100+
printf(" | | \n");
101+
102+
103+
}

0 commit comments

Comments
 (0)