Question
Make a C program of the Battleship game. Extend the program below (one-dimension game) into two-dimension game. Update the following parts: - Struct Ship -
Make a C program of the Battleship game. Extend the program below (one-dimension game) into two-dimension game.
Update the following parts:
- 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)
------------------------------------------------------------------
Reference program (1D game):
#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) { 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; } ----------------------------------------------------------------------
Produce the following outputs:
\begin{tabular}{|l|} \hline$./a. out \\ 55 \\ hit \\ 11 \\ miss \\ 22 \\ hit \\ 310 \\ miss \\ 39 \\ miss \\ 38 \\ miss \\ 37 \\ hit \\ \\ \hline \end{tabular} \begin{tabular}{|l|} \hline 74 \\ hit \\ 75 \\ hit \\ 73 \\ miss \\ 84 \\ miss \\ 99 \\ miss \\ 88 \\ hit \\ All ships are sunk \\ \\ \hline \end{tabular}Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started