-
Notifications
You must be signed in to change notification settings - Fork 1
/
ts.c
195 lines (171 loc) · 3.38 KB
/
ts.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char c[3][3];
int arr[3][3]={
{1,2,3},
{4,5,6},
{7,8,9}
};
//FOR PRINTING MATRIX
void print(char a[3][3])
{
int i,j,k;
for( i = 0; i<3 ; i++)
{
for(j = 0; j<3; j++)
{ if(a[i][j]=='\0')
printf(" ");
else
printf(" %c ",a[i][j]);
if( j != 2)
printf("|");
}
printf("\n");
if( i != 2)
printf("-----------\n");
}
printf("\n");
}
//FOR CHECKING X AND O
void checkXO(int a, int d, char c[3][3])
{
if (c[a][d] == 'X')
printf("\n******************YOU WON!!******************\n");
else
printf("\n******************YOU LOST!!******************\n");
}
//FOR CHECKING IF SOMEONE IS WON
int check(){
int n;
for( n=0;n<3;n++)
{
if((c[0][n]==c[1][n]) && (c[0][n]==c[2][n]) && (c[0][n]!='\0'))
{checkXO(0,n,c);return 1;}
else if( (c[n][1]==c[n][0]) && (c[n][1]==c[n][2]) && (c[n][1]!='\0'))
{checkXO(n,0,c);return 1;}
}
if((c[0][0]==c[1][1]) && (c[1][1]==c[2][2]) && (c[1][1]!='\0'))
{checkXO(0,0,c);return 1;}
else if((c[0][2]==c[1][1]) && (c[1][1]==c[2][0]) && (c[1][1]!='\0'))
{checkXO(0,2,c);return 1;}
return 0;
}
int play(int sockfd)
{
int n, buffer;
int i,j;
printf("*********Instructions*********\nType the number to print X on that position \n");
//print((char) arr);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j] = '\0';
}
}
printf("You are first \n");
while(1)
{
printf("You: \n");
printf("Enter cell number-> ");
scanf("%d",&buffer);
//SERVER WILL PLAY FIRST
n = write(sockfd, &buffer, sizeof(buffer));
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(arr[i][j]==buffer)
c[i][j] = 'X';
}
}
print(c);
if(check()==1)
{
return 0;
}
int temp = 0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(c[i][j] == 'X' || c[i][j] == 'O')
temp++;
}
}
if(temp==9){
printf("\n******************Draw******************\n");
break;
}
printf("\nYour friends turn :\n");
n = read( sockfd, &buffer , sizeof(buffer) );
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(arr[i][j]==buffer)
c[i][j] = 'O';
}
}
print(c);
if(check()==1)
{
return 0;
}
}
return 1;
}
void Send(int sockfd)
{
char buffer[512];
bzero(buffer,512);
FILE *f;
int words = 0;
char c;
char fn[20];
Q:
printf("Enter the file name \n");
scanf("%s",fn);
f=fopen(fn,"r");
if(f == NULL){
printf("Error no file found");
goto Q;
}
while((c=getc(f))!=EOF) //Counting No of words in the file
{
fscanf(f , "%s" , buffer);
if(isspace(c)||c=='\t')
words++;
}
write(sockfd, &words, sizeof(int));
rewind(f);
char ch ;
while(ch != EOF)
{
fscanf(f , "%s" , buffer);
write(sockfd,buffer,512);
ch = fgetc(f);
}
printf("The file was sent successfully");
bzero(buffer,512);
}
void Recv(int newsockfd)
{
FILE *fp;
int ch = 0;
char buffer[512];
fp = fopen("recieved.txt","a");
int words;
read(newsockfd, &words, sizeof(int));
while(ch != words)
{
read(newsockfd , buffer , 512);
fprintf(fp , " %s" , buffer);
ch++;
}
printf("The file was received successfully\n");
printf("The new file created is glad5.txt");
bzero(buffer,512);
//fclose(fp);
}