-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattleship.c
More file actions
674 lines (591 loc) · 16.5 KB
/
battleship.c
File metadata and controls
674 lines (591 loc) · 16.5 KB
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
/*
NAME: Evan Waldmann
CLASS: COP 3502H
ASSIGNMENT: Assignment 6: Battleship
DATE: 4/24/2016
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NORTH 'N'
#define SOUTH 'S'
#define WEST 'W'
#define EAST 'E'
#define MISS '~'
#define HIT 'X'
#define BLANK ' '
#define MAX 10
#define CARRIER_LENGTH 5
#define CARRIER 'C'
#define BATTLESHIP_LENGTH 4
#define BATTLESHIP 'B'
#define SUB_LENGTH 3
#define SUB 'S'
#define DESTROYER_LENGTH 3
#define DESTROYER 'D'
#define PATROL_LENGTH 2
#define PATROL 'P'
#define NUM_SHIPS 5
//point struct definition
typedef struct point
{
int x;
int y;
} point1 ;
//ship struct definition
struct ship
{
int length;
char * start;
char * stop;
char * direction;
int hits;
int sunk;
char *name;
};
//function that displays the board with ships, hits, and misses
void printBoard(char** grid)
{
int r,c;
printf("\n 1 2 3 4 5 6 7 8 9 10 (x)\n");
printf(" |------------------------------|\n");
for (r=0; r<MAX; r++)
{
for (c=0; c<MAX; c++)
{
if (c==0)
printf("%3d|", r+1);
printf(" %c ", grid[r][c]);
}
printf("|\n");
}
printf("(y)|------------------------------|\n");
}
//function that displays the current state of the game with hits or misses
void specialPrintBoard(char** grid)
{
int r,c;
printf("\n 1 2 3 4 5 6 7 8 9 10 (x)\n");
printf(" |------------------------------|\n");
for (r=0; r<MAX; r++)
{
for (c=0; c<MAX; c++)
{
if (c==0)
printf("%3d|", r+1);
//the only difference between the special print and regular print is this, which makes sure that no ship will be printed
if (grid[r][c] == CARRIER || grid[r][c] == SUB || grid[r][c] == DESTROYER || grid[r][c] == PATROL || grid[r][c] == BATTLESHIP)
printf(" %c ", BLANK);
else
printf(" %c ", grid[r][c]);
}
printf("|\n");
}
printf("(y)|------------------------------|\n");
}
//function that populates board with blanks
char** popBoard(char** grid)
{
int r,c;
for (r=0; r<MAX; r++)
{
for (c=0; c<MAX; c++)
{
grid[r][c] = BLANK;
}
}
return grid;
}
//converts a direction (N S E W) into a point that really is the basis vectors for the direction
point1* convertDirection (char dir)
{
point1* point = malloc(sizeof(point1));
if (dir == NORTH)
{
point->x = 0;
point->y = -1;
}
else if (dir == SOUTH)
{
point->x = 0;
point->y = 1;
}
else if (dir == WEST)
{
point->x = -1;
point->y = 0;
}
else if (dir == EAST)
{
point->x = 1;
point->y = 0;
}
else
printf("error");
return point;
}
//function to determine if ship will fit in selected position
//0 is false, any thing else is true
int isFit(int x, int y, char direction, int shipLength, char** grid)
{
int i;
//get point (basis vectors)
point1* point = convertDirection(direction);
//make the normal(unit) basis vector into the correct magnitude(ship length)
int xmax = point->x * shipLength;
int ymax = point->y * shipLength;
//check the bounds
if (x+xmax >= MAX|| x+xmax <0 || y+ymax >= MAX || y+ymax <0)
{
return 0;
}
//make sure there are not ships where the ship is going to be placed
for (i=0; i<xmax; i++)
{
if (grid[y][x+i] != BLANK)
return 0;
}
for (i=0; i>xmax; i--)
{
if (grid[y][x+i] != BLANK)
return 0;
}
for(i=0; i<ymax; i++)
{
if (grid[y+i][x] != BLANK)
return 0;
}
for(i=0; i>ymax; i--)
{
if (grid[y+i][x] != BLANK)
return 0;
}
//returns true if there is a fit
return 1;
}
//inserts the ship into the correct positions, does basically the same thing as the fit, except it doesn't check bounds
char** insertShip(char ship,int shipLength, int x, int y, char direction, char** grid)
{
int i;
x=x-1;
y=y-1;
point1* point = convertDirection(direction);
int xmax = point->x * shipLength;
int ymax = point->y * shipLength;
for (i=0; i<xmax; i++)
{
grid[y][x+i] = ship;
}
for (i=0; i>xmax; i--)
{
grid[y][x+i] = ship;
}
for(i=0; i<ymax; i++)
{
grid[y+i][x] = ship;
}
for(i=0; i>ymax; i--)
{
grid[y+i][x] = ship;
}
return grid;
}
//function for human to place ship
char** placeHumanShips (char ** grid)
{
int x, y;
char dir;
printf("\n");
//each do while is taking in input and then inserting that input into the grid
do
{
printf("Where do you want to place the start of your Carrier(length 5) and which direction do you want it to go? insert in format \"x y z\" where x and y in the bounds 1-10 and z is a char N, S, E, W");
scanf("%d %d %c", &x, &y, &dir);
}
while(!(isFit(x-1,y-1,dir,5,grid)));
grid = insertShip(CARRIER,CARRIER_LENGTH, x, y , dir, grid);
printf("\n");
do
{
printf("Where do you want to place the start of your BattleShip(length 4) and which direction do you want it to go? insert in format \"x y z\" where x and y in the bounds 1-10 and z is a char N, S, E, W");
scanf("%d %d %c", &x, &y, &dir);
}
while(!(isFit(x-1,y-1,dir,4,grid)));
grid = insertShip(BATTLESHIP,BATTLESHIP_LENGTH, x, y , dir, grid);
printf("\n");
do
{
printf("Where do you want to place the start of your Submarine(length 3) and which direction do you want it to go? insert in format \"x y z\" where x and y in the bounds 1-10 and z is a char N, S, E, W");
scanf("%d %d %c", &x, &y, &dir);
}
while(!(isFit(x-1,y-1,dir,3,grid)));
grid = insertShip(SUB,SUB_LENGTH, x, y , dir, grid);
printf("\n");
do
{
printf("Where do you want to place the start of your Destroyer(length 3) and which direction do you want it to go? insert in format \"x y z\" where x and y in the bounds 1-10 and z is a char N, S, E, W");
scanf("%d %d %c", &x, &y, &dir);
}
while(!(isFit(x-1,y-1,dir,3,grid)));
grid = insertShip(DESTROYER,DESTROYER_LENGTH, x, y , dir, grid);
printf("\n");
do
{
printf("Where do you want to place the start of your Patrol Boat(length 2) and which direction do you want it to go? insert in format \"x y z\" where x and y in the bounds 1-10 and z is a char N, S, E, W: ");
scanf("%d %d %c", &x, &y, &dir);
}
while(!(isFit(x-1,y-1,dir,2,grid)));
grid = insertShip(PATROL,PATROL_LENGTH, x, y , dir, grid);
return grid;
}
char** placeCompShips(char** grid)
{
int x, y,z, count =0;
char dir;
//the computer randomly will pick spots for the 5 ships and insert them
printf("\n");
printf("\nComputer randomly picking 5 more spots... ");
do
{
count++;
srand(time(NULL));
x = (rand() % 10)+1;
y = (rand() % 10)+1;
z=(rand() % 4) +1;
if (z == 1)
dir = 'N';
else if (z == 2)
dir = 'S';
else if (z==3)
dir = 'W';
else
dir = 'E';
}
while(!(isFit(x-1,y-1,dir,5,grid)));
// printf("x:%d y:%d dir: %c\n", x,y,dir);
grid = insertShip(CARRIER,CARRIER_LENGTH, x, y , dir, grid);
// printBoard(grid);
printf("\nComputer randomly picking 4 more spots... ");
do
{
count++;
srand(time(NULL));
x = (rand() % 10)+1;
y = (rand() % 10)+1;
z=(rand() % 4) +1;
if (z == 1)
dir = 'N';
else if (z == 2)
dir = 'S';
else if (z==3)
dir = 'W';
else
dir = 'E';
}
while(!(isFit(x-1,y-1,dir,4,grid)));
// printf("x:%d y:%d dir: %c\n", x,y,dir);
grid = insertShip(BATTLESHIP,BATTLESHIP_LENGTH, x, y , dir, grid);
// printBoard(grid);
printf("\nComputer randomly picking 3 more spots... ");
do
{
count++;
srand(time(NULL));
x = (rand() % 10)+1;
y = (rand() % 10)+1;
z=(rand() % 4) +1;
if (z == 1)
dir = 'N';
else if (z == 2)
dir = 'S';
else if (z==3)
dir = 'W';
else
dir = 'E';
}
while(!(isFit(x-1,y-1,dir,3,grid)));
// printf("x:%d y:%d dir: %c\n", x,y,dir);
grid = insertShip(SUB,SUB_LENGTH, x, y , dir, grid);
// printBoard(grid);
printf("\nComputer randomly picking 2 more spots... ");
do
{
count++;
srand(time(NULL));
x = (rand() % 10)+1;
y = (rand() % 10)+1;
z=(rand() % 4) +1;
if (z == 1)
dir = 'N';
else if (z == 2)
dir = 'S';
else if (z==3)
dir = 'W';
else
dir = 'E';
}
while(!(isFit(x-1,y-1,dir,3,grid)));
// printf("x:%d y:%d dir: %c\n", x,y,dir);
grid = insertShip(DESTROYER,DESTROYER_LENGTH, x, y , dir, grid);
// printBoard(grid);
printf("\nComputer randomly picking 1 more spot... ");
do
{
count++;
srand(time(NULL));
x = (rand() % 10)+1;
y = (rand() % 10)+1;
z=(rand() % 4) +1;
if (z == 1)
dir = 'N';
else if (z == 2)
dir = 'S';
else if (z==3)
dir = 'W';
else
dir = 'E';
}
while(!(isFit(x-1,y-1,dir,2,grid)));
// printf("x:%d y:%d dir: %c\n", x,y,dir);
grid = insertShip(PATROL,PATROL_LENGTH, x, y , dir, grid);
// printBoard(grid);
printf ("\nComputer finished placing ships!\nNumber of tries for the computer to randomly place its ships: %d\n", count);
return grid;
}
//checks to see if the co-ordinates are in bounds
//0 if false, and 1 if true
int isInBound (int x, int y)
{
if (x< MAX && x>=0 && y<MAX && y>=0)
return 1;
else
return 0;
}
//check bounds and checks if the co-ordinate was picked by the user before, it alerts user if something went wrong
//0 if false, and 1 if true
int isPickedBeforeHuman(char** grid, int x, int y)
{
// printf("CHECKING x:%d y:%d\n",x,y);
if (!(isInBound(x, y)))
{
printf("Selection out of bounds. Try again\n");
return 0;
}
if (grid[x][y] == MISS || grid[x][y] == HIT)
{
printf("Selection has been picked before. Try again\n");
return 0;
}
else
{
return 1;
}
}
//computer will always pick a point in bounds so this only checks picked before. it doesnt know display anything
//0 if false, and 1 if true
int isPickedBeforeComputer(char** grid, int x, int y)
{
// printf("CHECKING x:%d y:%d\n",x,y);
if (grid[x][y] == MISS || grid[x][y] == HIT)
return 0;
else
return 1;
}
//generates a random move for the computer and makes sure that the move is do able
void getViableMove(char** grid, int *x, int* y)
{
do
{
srand(time(NULL));
*x = (rand() % 10);
*y = (rand() % 10);
}
while(!(isPickedBeforeComputer(grid, *x, *y)));
}
//function to determine if a ship is sunk
//0 is false, any thing else is true
int isShipSunken( char ** grid, int x, int y)
{
int r, c,count=0;
//gets the ship char
char shipHit = grid[x][y];
// printf("%c ", shipHit);
for (r=0; r<MAX; r++)
{
for(c=0; c<MAX;c++)
{
if (grid[r][c] == shipHit) // counts the amount of those chars in the grid
count++;
}
}
// printf("count: %d\n", count);
if (count-1 ==0) //returns the shipHit, which is also a true value if there is only the one ship char left. this means that type of ship was sunken
return shipHit;
else //otherwise the ship was no sunken
return 0;
}
//function to determine if the selected cell is a hit or miss
//0 is false, any thing else is true
int isHit(int x, int y, char** grid)
{
if (grid[x][y] == BLANK) //only need to check if the space holds a BLANK char because you cant pick the same spot. thus if it is not blank it is a ship
return 0;
else
return grid[x][y];
}
//function to determine if the game is over
//0 is false, any thing else is true
int isGameOver(char** grid)
{
//check for C,B,P,D,S
int r,c;
for (r=0; r<MAX; r++)
{
for (c=0; c<MAX; c++)
{
//make sure there are no ships left
if (grid[r][c] == CARRIER || grid[r][c] == SUB || grid[r][c] == DESTROYER || grid[r][c] == PATROL || grid[r][c] == BATTLESHIP)
return 0;
}
}
return 1;
}
//function to manage switching play between human and computer
//returns 1 if computer wins, 2 if human wins, and 0 is no one wins
int switchTurns(char** uGrid, char** cGrid)
{
int x,y;
int shipHit;
//human turn
//give the User the boards
printf("Hits and Misses on computer:\n");
specialPrintBoard(cGrid);
printf("\nYour ships with computer's Hits and Misses: \n");
printBoard(uGrid);
//prompt user for input and make sure it was not repeated input(picked before)
do
{
printf("What position would you like to shoot? insert in format \"x y\" where x and y in the bounds 1-10: ");
scanf("%d %d", &x,&y);
x=x-1;
y=y-1;
int temp = x;
x=y;
y=temp;
printf("\n");
}
while (!(isPickedBeforeHuman(cGrid,x,y)));
//check to see if the input was a hit
if (isHit(x,y,cGrid))
{
//if hit check to see if ship was sunken
if((shipHit = isShipSunken(cGrid, x,y)))
{
printf("You have SUNKEN an enemy ship(%c) with your HIT at %d %d.\n", shipHit,y+1, x+1);
}
else
{
printf("You HIT an enemy ship at %d %d.\n", y+1, x+1);
}
//mark hit
cGrid[x][y]= HIT;
//after each hit check to see if game is over
if (isGameOver(cGrid))
{
printf("\nGAME IS OVER!\n");
return 2;
}
}
else
{
//mark miss
cGrid[x][y]= MISS;
printf("%d %d was a MISS.\n",y+1, x+1 );
}
printf("\n");
//computer turn
//get computer's move
getViableMove(uGrid, &x, &y);
// printf("x:%d, y:%d\n", x,y);
//check to see if the computer hit
if (isHit(x,y,uGrid))
{
//if hit check to see if ship was sunken
if((shipHit =isShipSunken(uGrid,x,y)))
{
printf("One of your ships(%c) was SUNKEN by the computer's HIT at %d %d.\n", shipHit, y+1, x+1);
}
else
{
printf("One of your ships was HIT by the computer's shot at %d %d.\n", y+1, x+1);
}
//mark hit
uGrid[x][y]= HIT;
//after each hit check to see if game is over
if (isGameOver(uGrid))
{
return 1;
}
}
else
{
printf("The computer fired at %d %d and MISSED.\n", y+1, x+1);
//mark miss
uGrid[x][y]= MISS;
}
return 0;
}
int main(void)
{
int i, turn, count=0;
// allocate memory for each of the grids
char** userGrid = malloc(sizeof(char*)*10);
for (i=0; i<MAX; i++)
userGrid[i] = malloc(sizeof(char)*10);
char** compGrid = malloc(sizeof(char*)*10);
for (i=0; i<MAX; i++)
compGrid[i] = malloc(sizeof(char)*10);
//fill each grid with blanks
userGrid = popBoard(userGrid);
compGrid = popBoard(compGrid);
//place the ships
placeHumanShips(userGrid);
placeCompShips(compGrid);
//game begins
//game loop
do
{
count++;
printf("\n\n---------------------------------------\n");
printf("| Turn %2d |\n", count);
printf("---------------------------------------\n");
//method that allows user and computer to take turns
turn = switchTurns(userGrid, compGrid);
}
while (turn == 0);
//end game print out
printf("The boards after %d turns were: \n", count);
printf("\nUser's board: ");
printBoard(userGrid);
printf("\nComputer's board:");
printBoard(compGrid);
if (turn == 1)
{
printf("\nThe computer has won!\n");
}
else if (turn ==2)
{
printf("\nYou have won!\n");
}
else
printf("Error");
printf("\nThe game will now close.\n");
return 0;
}
/*
1 1 S
2 1 S
3 1 S
4 1 S
5 1 S
*/