-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic tac toe.txt
207 lines (188 loc) · 5.56 KB
/
tic tac toe.txt
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
/*If you find any bug, do send me an s.s. of your entire played game*/
/*
Game creator: Mehrin Farzana
Game created on: 21-4-2023
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
char room[9] = "123456789";
char name[2][101];
int move=0;
int the_game();
void player_move_against_computer();
void player_move_against_human();
int computer_move();
int check();
int check_draw();
void board();
int enter_name();
int main(){
srand(time(NULL));
printf("Welcome to a game of Tic-Tac-Toe!\nPlayer 1, Try to write 'EID' horizontally, vertically or diagonally to WIN!\n");
printf("Player 2, Try to write 'XXX' horizontally, vertically or diagonally to WIN!\n");
the_game();
return 0;
}
int enter_name(){
char computer[9];
int flag=0;
printf("Player 1, plz enter your name here: ");
scanf(" %[^\n]s", name[0]);
printf("To play against COMPUTER, enter in name for 2nd player 'computer'.\n");
printf("Player 2, plz enter your name here: ");
scanf(" %[^\n]s", name[1]);
for(int i=0; i<8; i++)
computer[i]=toupper(name[1][i]);
computer[8]='\0';
if(strcmp("COMPUTER", computer)==0)
flag=1;
return flag;
}
void board(){
printf("\n");
for(int i=0; i<9; i+=3){
printf(" %c | %c | %c \n", room[i], room[i+1], room[i+2]);
if(i<4)
printf("---|---|---\n");
}
printf("\n");
}
int check_draw(int check){
int flag=0;
if(!check) return 1;
for(int i=0; i<9; i++){
if(room[i]!='E' && room[i]!='I' && room[i]!='D' && room[i]!='X'){
flag=1;
break;
}
}
if(!flag){
printf("Match Draw!!!\n");
return 0;
}
}
int check(){
if((room[0]=='E' && room[1]=='I' && room[2]=='D') || (room[3]=='E' && room[4]=='I' && room[5]=='D') || (room[6]=='E' && room[7]=='I' && room[8]=='D') || (room[0]=='E' && room[3]=='I' && room[6]=='D') || (room[1]=='E' && room[4]=='I' && room[7]=='D') || (room[2]=='E' && room[5]=='I' && room[8]=='D') || (room[0]=='E' && room[4]=='I' && room[8]=='D') || (room[2]=='E' && room[4]=='I' && room[6]=='D')){
printf("%s won the match!\n", name[0]);
return 0;
}
else if((room[0]==room[1] && room[0]==room[2]) || (room[3]==room[4] && room[3]==room[5]) || (room[6]==room[7] && room[6]==room[8]) || (room[0]==room[3] && room[0]==room[6]) || (room[1]==room[4] && room[1]==room[7]) || (room[2]==room[5] && room[2]==room[8]) || (room[0]==room[4] && room[0]==room[8]) || (room[2]==room[4] && room [2]==room[6]))
{
printf("%s won the match!\n", name[1]);
return 0;
}
return 1;
}
int computer_move(int p){
if(p==4 && room[p-1]!='E' && room[p-1]!='I' && room[p-1]!='D' && room[p-1]!='X'){
int a[4]={0,2,6,8};
return a[rand()%3];
}
else if((p==0 || p==2 || p==6 || p==8) && room[p-1]!='E' && room[p-1]!='I' && room[p-1]!='D' && room[p-1]!='X')
return 4;
else{
int a[9]={0,1,2,3,4,5,6,7,8};
return a[rand()%8];
}
}
void player_move_against_human(){
int p;
if(move==0)
printf("Enter the room number where you want to make your move: ");
else if(move%2==1)
printf("player 2, Enter room#: ");
else if(move%2==0)
printf("player 1, Enter room#: ");
scanf("%d", &p);
if(room[p-1]!='E' && room[p-1]!='I' && room[p-1]!='D' && room[p-1]!='X' && p>0 && p<10){
if(move%2==0){
if(move==0 || move==6)
room[p-1]='E';
else if(move==2 || move==8)
room[p-1]='I';
else if(move==4)
room[p-1]='D';
}
else if(move%2==1)
room[p-1]='X';
move++;
}
else{
printf("Invalid! Try again.\n");
player_move_against_human();
}
}
void player_move_against_computer(){
static int p;
if(move==0){
printf("Enter the room number where you want to make your move: ");
scanf("%d", &p);
}
else if(move%2==1){
p=computer_move(p-1)+1;
while(room[p-1]=='E' || room[p-1]=='I' || room[p-1]=='D' || room[p-1]=='X'){
p=computer_move(p-1)+1;
}
printf("COMPUTER's move: %d\n", p);
}
else if(move%2==0){
printf("player 1, Enter room#: ");
scanf("%d", &p);
}
if(room[p-1]!='E' && room[p-1]!='I' && room[p-1]!='D' && room[p-1]!='X' && p>0 && p<10){
if(move%2==0){
if(move==0 || move==6)
room[p-1]='E';
else if(move==2 || move==8)
room[p-1]='I';
else if(move==4)
room[p-1]='D';
}
else if(move%2==1)
room[p-1]='X';
move++;
}
else{
printf("Invalid! Try again.\n");
player_move_against_human();
}
}
int the_game(){
int a=1, b=1, flag, enter;
char computer[8];
flag=enter_name();
board();
//
// printf("\nharat!\n");
//printf("%d\n", flag);
if(flag==1){
while(a && b){
a=check();
b=check_draw(a);
if(a==0 || b==0) break;
player_move_against_computer();
board();
}
}
else if(flag==0){
while(a && b){
a=check();
b=check_draw(a);
if(a==0 || b==0) break;
player_move_against_human();
board();
}
}
printf("\n\nWould you like to play again?\nEnter 1 for YES & 0 for NO\n");
scanf("%d", &enter);
if(enter){
system("cls");
strcpy(room, "123456789");
move=0;
the_game();
}
else return 0;
}