Question: (C) Battleship game in two dimensions. Example: Original code (one dimension game); #include #include #include struct Ship { char name[32]; int left; int right; int

(C) Battleship game in two dimensions.

(C) Battleship game in two dimensions. Example: Original code (one dimension game);

Example:

#include #include #include struct Ship { char name[32]; int left; int right;

Original code (one dimension game);

#include #include #include

struct Ship { char name[32]; int left; int right; int hit; };

void initialize(struct Ship * ships) { strcpy(ships[0].name, "Carrier"); ships[0].left = 2; ships[0].right = 6; ships[0].hit = 0;

strcpy(ships[0].name, "Submarine"); ships[1].left = 15; ships[1].right = 17; ships[1].hit = 0;

strcpy(ships[0].name, "Destroyer"); ships[2].left = 8; ships[2].right = 9; ships[2].hit = 0; }

int isHit(struct Ship ship, int pos) { if(pos>=ship.left && pos

int isFinished(struct Ship * ships, int n) { int cnt=0; for(int i=0;i

int main() { struct Ship ships[3]; initialize(ships);

while (1) { int pos = 0; scanf("%d", & pos);

int hit = 0; for (int i = 0; i 0) { printf("hit "); if (isFinished(ships, 3)) { printf("All ships are sunk "); break; } } else { printf("miss "); } } return 0; } // end of code

Extend your program into 2D game. Update the following part struct Ship User Input Function related to Ship such as initialize, isHit and isFinished. Suppose the ships are located as follows: Carrier: from (2, 2) to (6, 2) Battleship: from (3, 4) to (3, 7) Cruiser: from (7,4) to (9, 4) Submarine: from (5, 5) to (5, 7) Destroyer: from (8, 8) to (9,8) 74 hit $ ./a.out 55 hit 11 miss 22 hit 3 10 miss 39 miss 38 miss 37 hit 75 hit 73 miss 84 miss 99 miss 88 hit All ships are sunk

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!