Question: (C) Make a program which provide the simple Battleship game. Extend step 2 and enable to show the current status. -: Unknown Hit: * Miss:
(C) Make a program which provide the simple Battleship game.
Extend step 2 and enable to show the current status.
-: Unknown
Hit: *
Miss: x
Example

Code from step 2
#include
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 if(ships[i].hit==1) cnt++; } if(cnt==n) return 1; else return 0; } 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; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
