-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoefunction.c
209 lines (162 loc) · 9.6 KB
/
tictactoefunction.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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include <stdbool.h>
//initializing all variables all global variables.
char board[9]={'1','2','3','4','5','6','7','8','9'};
int chance1; //player 1 chance
int chance2; //player 2 chance
char X='x';
char O='o';
bool win=false;
int k=1; //it is number of turns
int count=0;
// declaring all functions before main
void askValue(void);
void drawBoard(void);
void caseWin(void);
int main()
{
drawBoard(); //calling both functions
askValue();
}
//function3 checks if the player has won or match is draw
void caseWin()
{
win=false; //initializing the boolean win to false
if (((board[0]==board[1]) && (board[1]==board[2]))||((board[3]==board[4]) && (board[4]==board[5]))||((board[6]==board[7]) && (board[7]==board[8]))) //checks if 3 elements of row becomes same then player wins
{
win=true;
system("cls");
if(k % 2 !=0) //for player chances and who won i.e if k is odd and player wins therefore player 1 is winner
{
printf("\n\n\t\t \t\t\t\tplayer 1 has won.\n\n");
drawBoard(); // draws the board for the last time.
}
else if(k % 2==0)
{
printf("\n\n\t\t \t\t\t\tplayer 2 has won.\n\n");
drawBoard();
}
}
else if(((board[0]==board[3]) && (board[3]==board[6]))||((board[1]==board[4]) && (board[4]==board[7]))||((board[2]==board[5]) && (board[5]==board[8]))) //checks if 3 elements of columm becomes same then player wins
{
win=true;
system("cls");
if(k % 2 !=0)
{
printf("\n\n\t\t \t\t\t\tplayer 1 has won.\n\n");
drawBoard();
}
else if(k % 2==0)
{
printf("\n\n\t\t \t\t\t\tplayer 2 has won.\n\n");
drawBoard();
}
}
else if(((board[0]==board[4]) && (board[4]==board[8]))||((board[2]==board[4]) && (board[4]==board[6]))) //checks if 3 elements of diagonal becomes same then player wins
{
win=true;
system("cls");
if(k % 2 !=0)
{
printf("\n\n\t\t \t\t\t\tplayer 1 has won.\n\n");
drawBoard();
}
else if(k % 2==0)
{
printf("\n\n\t\t \t\t\t\tplayer 2 has won.\n\n");
drawBoard();
}
}
else if(k==9) // it is for the condition of draw i.e if number of turns becomes 9 and no one is winner then draw.
{
win=true;
system("cls");
printf("\n\n\t\t \t\t\t the match is draw\n\n");
drawBoard(); // draws the board for the last time.
}
else{win=false;}
if(win==false) // if none of above is satisfied then it moves forward to ask another value.
{
system("cls");
drawBoard();
}
}
//function2 // asks value from player and checks for invalid input.
void askValue()
{
//player 1
if(k % 2 !=0) // if k is even then player 1 turn
{
printf("\nPlayer 1 enter the value:");
scanf("%d",&chance1);
if(chance1<=0||chance1>9|| board[chance1-1] =='o' || board[chance1-1]=='x') //checks for invalid input if it is invalid then it goes to last else and again asks for value.
{
printf("enter valid number");
}
else //if input is valid then it assigns the value to array and converts it to x and o
{
int p1=chance1 - 1;
board[p1]=X;
system("cls");
caseWin(); //checks everytime for the condition of win or draw
k++; //increases k by 1 to go to next player chance
}
}
// player 2
else if(k % 2==0)
{
printf("\nPlayer 2 enter the value:%d", chance2);
scanf("%d",&chance2);
if( chance2<=0|| chance2>9|| board[ chance2-1]=='x' || board[ chance2-1]=='o')
{
printf("enter valid number");
}
else
{
int p2= chance2 - 1;
board[p2]=O;
system("cls");
caseWin();
k++;
}
}
if(k<=9 && win==false) // only asks value again from player if no. of turns is less than equal to 9 and win is false.
{
askValue();
}
}
//function 1 draws the board
void drawBoard(void)
{
printf("\n\t\t\t\t\t\t tic tac toe\n\n");
printf("player(X) 1 vs player(O) 2\n\n\n");
count=0;
for (int i=0;i<9;i++)
{
for(int j=0;j<20;j++)
{
if(j==6 || j==13)
{
printf("|");
}
else if(i==2 || i==5)
{
printf("_");
}
else if(i==1 || i==4 || i==7)
{
if (j==2 || j==8 || j==14)
{
printf(" %c ",board[count]);
count++;
}
}
else
printf(" ");
}
printf("\n");
}
}