Question: Make a C program of the simple Battleship game. Extend on the program below and let it show the current status. Unknown: - Hit: *

Make a C program of the simple Battleship game. Extend on the program below and let it show the current status.

Unknown: -

Hit: *

Miss: x

-----------------------------------------------------

Reference program (2D game):

#include #include #include

struct Ship { char name[32]; int topLeftX; int topLeftY; int bottomRightX; int bottomRightY; int hit; };

void initialize(struct Ship * ships) { strcpy(ships[0].name, "Carrier"); ships[0].topLeftX = 2; ships[0].topLeftY = 2; ships[0].bottomRightX = 2; ships[0].bottomRightY = 6; ships[0].hit = 0;

strcpy(ships[1].name, "BattleShip"); ships[1].topLeftX = 3; ships[1].topLeftY = 4; ships[1].bottomRightX = 3; ships[1].bottomRightY = 7; ships[1].hit = 0;

strcpy(ships[2].name, "Cruiser"); ships[2].topLeftX = 7; ships[2].topLeftY = 4; ships[2].bottomRightX = 9; ships[2].bottomRightY = 4; ships[2].hit = 0;

strcpy(ships[3].name, "Submarine"); ships[3].topLeftX = 5; ships[3].topLeftY = 5; ships[3].bottomRightX = 5; ships[3].bottomRightY = 7; ships[3].hit = 0;

strcpy(ships[4].name, "Destroyer"); ships[4].topLeftX = 8; ships[4].topLeftY = 8; ships[4].bottomRightX = 9; ships[4].bottomRightY = 8; ships[4].hit = 0; }

int isHit(struct Ship ship, int posX,int posY) {

if(posX>=ship.topLeftX && posX=ship.topLeftY && posY

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

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

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

int hit = 0; for (int i = 0; i 0) { printf("hit "); if (isFinished(ships, 5)) { printf("All ships are sunk "); break; } } else { printf("miss "); } } return 0; } -------------------------------------------

Please be able to produce the outputs below:

Make a C program of the simple Battleship game. Extend on the

\begin{tabular}{|l|l|} \hline$./a. out \\ \hline - \\ \hline hit \\ \hline 2 \\ \hline \\ \hline - \\ \hline hit \\ \hline \end{tabular}

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!