-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleshipP4.c
713 lines (581 loc) · 20.4 KB
/
battleshipP4.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/* Project 4 template
* Please check the comments and hints, and fill the codes.
* You only need to write your code in this file, and you do not
* need to write the server and client sides in two files.
*
* You also need to modify the code from project 3 accordingly to
* make it adapted to the network communication.
* */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>
#define SIZE 10
// you can change the struct as you like
struct move{
char letter;
int number;
char state[20];
char ship[20];
int player;
int win;
struct move *next;
};
char ipAddress[200], port[200];
int ourSocket,listenSocket;
void generateShip(char** board, int size, char letter) {
int noGood = 1;
int orientation , row, col;
int curRow, curCol;
while (noGood) {
orientation = random() %2;
if (orientation == 0) { // Horizontal
row = random() % 10;
col = random() % (10 - size);
} else {
row = random() % (10 - size);
col = random() % 10;
}
int noObstructions = 1;
for (int i=0;i<size;i++) {
curRow = row, curCol = col;
if (orientation == 0) { // Horizontal
curCol += i;
} else {
curRow += i;
}
if (board[curRow][curCol] != '-')
noObstructions = 0;
}
if (noObstructions == 0)
continue;
noGood = 0;
}
for (int i=0;i<size;i++) {
curCol = col, curRow = row;
if (orientation == 0) { // Horizontal
curCol += i;
} else {
curRow += i;
}
board[curRow][curCol] = letter;
}
}
void sigchld_handler(int s)
{
(void)s; // quiet unused variable warning
// waitpid() might overwrite errno, so we save and restore it:
int saved_errno = errno;
while(waitpid(-1, NULL, WNOHANG) > 0);
errno = saved_errno;
}
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
void createSendingSocket() {
/*write the code here, you can refer to the lab9 handout */
int numbytes;
char buf[30];
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(ipAddress, port, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((ourSocket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
if (connect(ourSocket, p->ai_addr, p->ai_addrlen) == -1) {
perror("client: connect");
close(ourSocket);
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "client: failed to connect\n");
exit(2);
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);
printf("Connecting to %s ...\n", s);
freeaddrinfo(servinfo); // all done with this structure
}
void createListenSocket() {
/*write the code here, you can refer to the lab9 handout */
int new_fd; // listen on sock_fd, new connection on new_fd
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes = 1;
char s[INET6_ADDRSTRLEN];
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP
int backlog = 10;
if ((rv = getaddrinfo("127.0.0.1", port, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
exit(1);
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((listenSocket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(listenSocket, p->ai_addr, p->ai_addrlen) == -1) {
close(listenSocket);
perror("server: bind");
continue;
}
break;
}
freeaddrinfo(servinfo); // all done with this structure
if (p == NULL) {
fprintf(stderr, "server: failed to bind\n");
exit(1);
}
if (listen(listenSocket, backlog) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
printf("Waiting for opponent to connect...\n");
sin_size = sizeof their_addr;
new_fd = accept(listenSocket, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
}
inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
printf("Got connection from %s\n", s);
close(listenSocket);
listenSocket = new_fd;
}
char** initialization(char ***ourMoveBoard){
if (ipAddress[0] == 0){
/*add function call of create listen socket*/
createListenSocket();
}
else{
/*add function call of create sending socket*/
createSendingSocket();
}
int i, j;
char **board = (char**)malloc(sizeof(char*)*SIZE);
*ourMoveBoard = (char**)malloc(sizeof(char*)*SIZE);
for (i = 0; i < SIZE; i++){
board[i] = (char*)malloc(sizeof(char)*SIZE);
(*ourMoveBoard)[i] = (char*)malloc(sizeof(char)*SIZE);
}
for(i = 0; i < SIZE; i++){
for(j = 0; j < SIZE; j++){
board[i][j] = '-';
(*ourMoveBoard)[i][j] = '-';
}
}
generateShip(board, 2,'D');
generateShip(board, 3,'S');
generateShip(board, 3,'C');
generateShip(board, 4,'B');
generateShip(board, 5,'R');
return board;
}
void insert_move(struct move **head, struct move **tail,struct move *temp){
if (*head == NULL){
/* List is currently empty. */
*head = *tail = temp;
}
else{
(*tail)->next = temp;
*tail = (*tail)->next;
}
}
void update_state(char* state, char ** board, struct move** head,struct move** tail, struct move** temp){
int row, i, j;
char letter = (*temp)->letter;
int col = (*temp)->number;
row = letter % 65;
if(board[row][col] == '-'){
strcpy(state, "MISS");
strcpy((*temp)->state, "MISS");
strcpy((*temp)->ship, "NONE");
board[row][col]='O';
}
else{
strcpy(state, "HIT");
strcpy((*temp)->state, "HIT!");
switch (board[row][col]){
case 'C': strcpy((*temp)->ship, "Crusier"); break;
case 'R': strcpy((*temp)->ship, "Carrier"); break;
case 'B': strcpy((*temp)->ship, "Battleship"); break;
case 'S': strcpy((*temp)->ship, "Submarine"); break;
case 'D': strcpy((*temp)->ship, "Destroyer"); break;
}
board[row][col]='X';
}
int counter = 0;
for(i=0; i < SIZE; i++){
for(j=0; j < SIZE; j++){
if(board[i][j] == '-' || board[i][j] == 'X' || board[i][j] == 'O')
counter += 1;
}
}
if(counter == SIZE * SIZE)
strcpy(state, "GAME OVER!");
insert_move(head,tail,*temp);
}
void update_our_move_board(char ***ourMoveBoard, struct move *ourMove){
int row, i, j;
char letter = ourMove->letter;
int col = ourMove->number;
row = letter % 65;
if(!strcmp(ourMove->state, "HIT!")){
char ship = ourMove->ship[2];
switch (ship)
{
case 'u':
(*ourMoveBoard)[row][col] = 'C';
break;
case 'r':
(*ourMoveBoard)[row][col] = 'R';
break;
case 't':
(*ourMoveBoard)[row][col] = 'B';
break;
case 'b':
(*ourMoveBoard)[row][col] = 'S';
break;
case 's':
(*ourMoveBoard)[row][col] = 'D';
break;
}
}else
(*ourMoveBoard)[row][col] = 'O';
}
struct move* accept_input(char **ourMoveBoard){
char letter;
int number;
bool flag = true;
do{
printf("Enter a letter A-J and number 0-9 (ex. B4). Enter Z0 to quit.\n");
int size = scanf(" %c%d", &letter, &number);
if(size != 2){
printf("INVALID INPUT\n");
continue;
}
letter = toupper(letter);
if(letter == 'Z' && number == 0)
break;
int row = letter % 65;
if(letter < 65 || letter > 74)
printf("INVALID INPUT\n");
else if(number <0 || number >9)
printf("INVALID INPUT\n");
else if(ourMoveBoard[row][number] != '-')
printf("You have already entered this move!\n");
else
flag = false;
}while(flag);
struct move *temp;
temp = (struct move *)malloc(sizeof(struct move));
temp->letter = letter;
temp->number = number;
temp->win = 0;
return temp;
}
void display_state(char** board, char** ourMoveBoard){
int i, j;
printf("\n**** Your guesses **** **** Their Guesses ****\n");
printf(" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9\n");
for (i = 0; i < SIZE; i++){
printf("%c ", 65+i);
for (j = 0; j < SIZE; j++){
printf("%c ", ourMoveBoard[i][j]);
}
printf(" %c ", 65+i);
for (j = 0; j < SIZE; j++){
printf("%c ", board[i][j]);
}
printf("\n");
}
}
int teardown(char ** board, struct move* head, struct move* tail){
int i;
struct move* temp;
for(i = 0; i < SIZE; i++)
free(board[i]);
free(board);
FILE *fptr;
if(ipAddress[0] == 0)
fptr = fopen("server_log.txt", "w");
else
fptr = fopen("clietn_log.txt", "w");
if (fptr == NULL)
{
exit(-1);
}
if (head==NULL){
fprintf(fptr, "No moves were made.");
}
else{
while (head != NULL){
if(head->player == 1){
switch (head->state[0]){
case 'H':
fprintf(fptr, "You fired at %c%d %s %s \n", head->letter, head->number, head->state, head->ship);
break;
default:
fprintf(fptr, "You fired at %c%d %s \n", head->letter, head->number, head->state);
break;
}
}else{
switch (head->state[0]){
case 'H':
fprintf(fptr, "Your opponent fired at %c%d %s %s \n", head->letter, head->number, head->state, head->ship);
break;
default:
fprintf(fptr, "Your opponent fired at %c%d %s \n", head->letter, head->number, head->state);
break;
}
}
temp = head;
head = head->next;
free(temp);
}
if(tail->win == 1)
fprintf(fptr, "You sunk all of your opponent's ships. You won the game!");
else
fprintf(fptr, "Your opponent sunk all of your ships. You lost the game!");
}
fclose(fptr);
close(listenSocket);
close(ourSocket);
return 0;
}
void main(int argc, char **argv) {
if (argc != 3 && argc != 2) { printf ("usage: battleship [ipaddress] port\n"); return; }
if (argc == 3) {
// if there are two command line arguments, where
// first is the ipaddress and
// second is the port, then we initialize
// the client side in initialization() function
strcpy(ipAddress,argv[1]);
strcpy(port,argv[2]);
}
else {
// if there is only one command line argument,
// then we initialize the server side in initialization
// function
memset(ipAddress,0,200);
strcpy(port,argv[1]);
}
srand(time(NULL));
char **board, **ourMoveBoard;
char state[] = "GAME START";
char flag[] = "GAME OVER!";
struct move *head, *tail, *ourMove, *theirMove;
head = tail = NULL;
/*modify the initialization function */
board = initialization(&ourMoveBoard);
printf("\n\nYour guesses are on the board to the left.\n");
printf("Your ships and your opponent's guesses will appear on the board to the right.\n");
char buff[20];
char ourMoveStateAndShip[40] = {};
char theirMoveStateAndShip[40] = {};
do{
if(ipAddress[0] != 0){
//You are the client
display_state(board, ourMoveBoard);
//make move
ourMove = accept_input(ourMoveBoard);
ourMove->player = 1;
char num[2];
sprintf(num, "%d", ourMove->number);
buff[0] = ourMove->letter;
buff[1] = ' ';
buff[2] = num[0];
//send move
send(ourSocket, buff, sizeof(buff), 0);
if(ourMove->letter == 'Z' && ourMove->number == 0){
printf("\nYou quit the game.\n");
break;
}
//wait for state
recv(ourSocket, ourMoveStateAndShip, sizeof(ourMoveStateAndShip), 0);
if(ourMoveStateAndShip[0] == 'W'){
//You sunk all of your opponent's ships
strcpy(state, "GAME OVER!");
strcpy(ourMoveStateAndShip, ourMoveStateAndShip + 1);
}
//store move
strcpy(ourMove->state, strtok(ourMoveStateAndShip, " "));
strcpy(ourMove->ship, strtok(NULL, " "));
strcpy(ourMoveStateAndShip, "");
if(ourMove->state[0] == 'H')
printf("\nYou hit their %s!\n", ourMove->ship);
else
printf("\nYou missed!\n");
//Update our move board
update_our_move_board(&ourMoveBoard, ourMove);
display_state(board, ourMoveBoard);
if(strcmp(state, flag) == 0){
printf("\nYou sunk all of your opponent's ships! You win!\n");
ourMove->win = 1;
insert_move(&head, &tail, ourMove);
break;
}
//Insert the move in the list
insert_move(&head, &tail, ourMove);
//wait for move
printf("\nWaiting for your opponent to make a move...\n");
fflush(stdout);
recv(ourSocket, buff, sizeof(buff), 0);
theirMove = (struct move *)malloc(sizeof(struct move));
char letter = buff[0];
int number = buff[2] - '0';
if(letter == 'Z' && number == 0){
printf("\nYour opponent quit the game!\n");
break;
}
printf("\nTheir move was: %c%d\n", letter, number);
theirMove->letter = letter;
theirMove->number = number;
theirMove->player = 2;
update_state(state, board, &head, &tail, &theirMove);
if(theirMove->state[0] == 'H')
printf("They hit your %s!\n", theirMove->ship);
else
printf("They missed!\n");
if(strcmp(state, flag) == 0){
//Your opponent sunk all your ships
strcpy(theirMoveStateAndShip, "W");
}
//send state
strcat(theirMoveStateAndShip, theirMove->state);
strcat(theirMoveStateAndShip, " ");
strcat(theirMoveStateAndShip, theirMove->ship);
send(ourSocket, theirMoveStateAndShip, sizeof(theirMoveStateAndShip), 0);
strcpy(theirMoveStateAndShip, "");
if(strcmp(state, flag) == 0){
display_state(board, ourMoveBoard);
printf("\nYour opponent sunk all of your ships! You lost!\n");
}
}else{
//you are server
//wait for move
display_state(board, ourMoveBoard);
printf("\nWaiting for your opponent to make a move...\n");
fflush(stdout);
//Receive move
recv(listenSocket, buff, sizeof(buff), 0);
theirMove = (struct move *)malloc(sizeof(struct move));
char letter = buff[0];
int number = buff[2] - '0';
if(letter == 'Z' && number == 0){
printf("\nYour opponent quit the game!\n");
break;
}
printf("\nTheir move was: %c%d\n", letter, number);
theirMove->letter = letter;
theirMove->number = number;
theirMove->player = 2;
update_state(state, board, &head, &tail, &theirMove);
if(theirMove->state[0] == 'H')
printf("They hit your %s!\n", theirMove->ship);
else
printf("They missed!\n");
if(strcmp(state, flag) == 0){
//Your opponent sunk all your ships
strcpy(theirMoveStateAndShip, "W");
}
//send state
strcat(theirMoveStateAndShip, theirMove->state);
strcat(theirMoveStateAndShip, " ");
strcat(theirMoveStateAndShip, theirMove->ship);
send(listenSocket, theirMoveStateAndShip, sizeof(theirMoveStateAndShip), 0);
strcpy(theirMoveStateAndShip, "");
display_state(board, ourMoveBoard);
if(strcmp(state, flag) == 0){
printf("\nYour opponent sunk all of your ships! You lost!\n");
break;
}
//make move
ourMove = accept_input(ourMoveBoard);
ourMove->player = 1;
char num[2];
sprintf(num, "%d", ourMove->number);
buff[0] = ourMove->letter;
buff[1] = ' ';
buff[2] = num[0];
//send move
send(listenSocket, buff, sizeof(buff), 0);
if(ourMove->letter == 'Z' && ourMove->number == 0){
printf("\nYou quit the game.\n");
break;
}
//wait for state
recv(listenSocket, ourMoveStateAndShip, sizeof(ourMoveStateAndShip), 0);
if(ourMoveStateAndShip[0] == 'W'){
//You sunk all of your opponent's ships
strcpy(state, "GAME OVER!");
strcpy(ourMoveStateAndShip, ourMoveStateAndShip + 1);
}
//store move
strcpy(ourMove->state, strtok(ourMoveStateAndShip, " "));
strcpy(ourMove->ship, strtok(NULL, " "));
strcpy(ourMoveStateAndShip, "");
if(ourMove->state[0] == 'H')
printf("\nYou hit their %s!\n", ourMove->ship);
else
printf("\nYou missed!\n");
//Update our move board
update_our_move_board(&ourMoveBoard, ourMove);
if(strcmp(state, flag) == 0){
display_state(board, ourMoveBoard);
printf("\nYou sunk all of your opponent's ships! You win!\n");
ourMove->win = 1;
}
//Insert the move in the list
insert_move(&head, &tail, ourMove);
}
} while(strcmp(state, flag));
teardown(board, head, tail);
exit(0);
}