This repository was archived by the owner on May 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbs_server.c
361 lines (338 loc) · 7.17 KB
/
bs_server.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
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
//game harbours
int home[10][10];
int away[10][10];
// connection buffers
int readbfr;
int postbfr;
// socket
int sock;
int csock;
// socket details
struct sockaddr_in listeningsocket;
struct sockaddr_in clisock;
/*
network protocall for the game
message = 00000
ones position = position status /occupied/not occupied/failed hit/successful hit
tens position = y coord
hundreds position = x coord
thousands position = ship type
ten thousands position = misc message;
*/
// function prototypes
int checkfill(int x,int y,int orientation,int size);
int gshow(char *query);
void setz();
int post();
int recieve();
int addship(int x,int y,int orientation,int size);
int uphit();
int upstrike();
void status(int value);
int placescheckhome();
int placescheckaway();
//Execution Begin !!
int main(int argc, char *argv[]){
if (argc != 2) /* Test for correct number of arguments */
{
fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]);
return 0;
}
unsigned short port = atoi(argv[1]); /* First arg: local port */
/* Create socket for incoming connections */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
printf("\nsocket creation failed");
return 0;
}
else {
printf("Green socket \n");
}
/* Construct local address structure */
memset(&listeningsocket, 0, sizeof(listeningsocket)); /* Zero out structure */
listeningsocket.sin_family = AF_INET; /* Internet address family */
listeningsocket.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
listeningsocket.sin_port = htons(port); /* Local port */
/* Bind to the local address */
if (bind(sock, (struct sockaddr *) &listeningsocket, sizeof(listeningsocket)) < 0){
printf("\nbind failed");
return 0;
}
else {
printf("Green bind \n");
}
// listening for a request from the server
if(listen(sock,1)<0){
printf("\nattempts to connect to tihs pc failed aka listen failed");
return 0;
}
else {
printf("incoming connecction");
}
// accepting a connection request from client and setting up a dedicated socket apart from the listening socket
socklen_t rsize=sizeof(clisock);
csock=accept(sock,(struct sockaddr *)&clisock,&rsize);
if(csock<0){
printf("\n error in accept function");
return 0;
}
else{
printf("\nnow connected to %s",inet_ntoa(clisock.sin_addr));
}
//awesome
int a,b,c,d=1;
//start the game get filling
setz();
gshow("");
while(d<=5){
printf("Enter The x coordinate for placement of ship (size==|%2d:",d);
scanf("%d",&a);
printf("Enter The y coordinate for placement of ship (size==|%2d:",d);
scanf("%d",&b);
printf("Enter The orientation for placement of ship \n1= vertical , 0=horizontal(size==|%2d:",d);
scanf("%d",&c);
if((checkfill((a-1),(b-1),c,d))==1){
addship((a-1),(b-1),c,d);
d++;
}
else{
printf("invalid position");
}
gshow("");
}
printf("Waiting for the other player");
//start
post(0);
while(recieve()!=0){
}
int t;
t=0;
printf("\ninitiating game");
a=0;
int x;
int y;
int p;
//the game
while(a==0){
if (t%2==0){
printf("opponents turn %%.%%.%%..");
uphit();
t++;
}
else{
gshow("enter The coordinates for the strike");
printf("The X coordinate:");
scanf("%d",&x);
printf("The Y coordinate:");
scanf("%d",&y);
upstrike(x-1,y-1);
t++;
}
if (placescheckhome() == 0){
gshow("You LOST . Better luck next time :( ");
break;
}
else if(placescheckaway() ==0){
gshow("You WON !! Awesome !! ,keep it up :) ");
break;
}
}
if(shutdown(csock,2)==0){
printf("closed Client Socket");
}
if(shutdown(sock,2)==0){
printf("closed Listening Socket");
}
printf("Hope you Enjoyed the Game (c) Paul George For GCI LABS \n Bye !");
return 0;
}
// places left
int placescheckhome(){
int i,j,k=0;
for (i=0;i<10;i++){
for(j=0;j<10;j++){
if ((home[i][j]%10)==1){
k++;
}
}
}
return k;
}
// places left on the away
int placescheckaway(){
int i,j,k=0;
for (i=0;i<10;i++){
for(j=0;j<10;j++){
if ((away[i][j]%10)==4){
k++;
}
}
}
return (15-k);
}
//update when hit by the enemy
int uphit (){
int a = recieve();
int x =(a%10);
int y =((a/10)%10);
if (((home[x][y])%10)==1||((home[x][y]%10)==4)){
home[x][y]=4;
post(4);
gshow ("you have been HIT");
return 1;
}
else if((home[x][y]%10)!=1){
home[x][y]=3;
gshow("the opponent missed");
post(3);
return 1;
}
return 8;
}
//update when attacking the enemy
int upstrike(int x, int y){
post(x+(10*y));
int a = recieve();
if ((a)==4){
away[x][y]=4;
gshow("hit successful");
}
else{
away[x][y]=3;
gshow("failed hit");
}
return 1;
}
//checking if the ship can be inserted in the given position
int checkfill(int x,int y,int orientation,int size){
int i,j,k=0,l;
if(orientation ==1){
for(i=0;((x+i)<10)&&(i<size);i++){
if (home[x+i][y]==0){
k++;
}
}
}
else if(orientation ==0) {
for(i=0;((y+i)<10)&&(i<size);i++){
if (home[x][y+i]==0){
k++;
}
}
}
if (k==size){
return 1;
}
else{
return 8;
}
}
// displaying the array s and related information
int gshow(char message[1024]){
system("clear");
int i=-1,j=-1,k,l;
printf("\n");
printf("\nNETOWRK BATTLESHIP V 1.0.1 Beta (c) Paul George For GCI LABS\n");
for (i=-1;i<10;i++){
for(j=-1;j<10;j++){
if(i==-1&&j==-1){
printf(" x\\y ");
}
else if (i==-1){
printf(" %2d ",j+1);
}
else if(j==-1){
printf(" %2d ",i+1);
}
else{
status(home[i][j]);
}
}
printf("\n");
}
printf("ships remaing to attack: %2d",placescheckaway());
printf("\tships left : %2d",placescheckhome());
printf("\n");
for (i=-1;i<10;i++){
for(j=-1;j<10;j++){
if(i==-1&&j==-1){
printf(" x\\y ");
}
else if (i==-1){
printf(" %2d ",j+1);
}
else if(j==-1){
printf(" %2d ",i+1);
}
else{
status(away[i][j]);
}
}
printf("\n");
}
puts(message);
}
// char graphics later upgrade to open gl
void status(int value){
if (value%10==0){
printf(" ~~~ ");
}
else if (value%10==3){
printf(" ~*~ ");
}
else if (value%10==4){
printf(" /O/ ");
}
else if (value%10==1){
printf(" /#/ ");
}
else {
printf("error");
}
}
// setting the arrays to zero in the begining
void setz(){
int i=0,j=0;
for (i=0;i<10;i++){
for(j=0;j<10;j++){
home[i][j]=0;
}
}
for (i=0;i<10;i++){
for(j=0;j<10;j++){
away[i][j]=0;
}
}
}
// posting the message integer
int post(int32_t num){
postbfr = htonl(num);
send(csock, &postbfr, sizeof(int32_t), 0);
return 0;
}
// recieve ing the sent integer
int recieve(){
recv(csock,&readbfr, sizeof(int32_t), 0);
readbfr = ntohl(readbfr);
return readbfr;
}
// insert ship
int addship(int x,int y,int orientation,int size){
int i=0,j=0;
if(orientation ==1){
for(i=0;i<size;i++){
home[x+i][y]=10*(x+i)+100*(y)+1000*(size)+1;
}
}
else{
for(i=0;i<size;i++){
home[x][y+i]=10*(x)+100*(y+i)+1000*(size)+1;
}
}
}