Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #define numships 5 int board[10][10]; int coordinate[2]; int teardown() { for (int i=0;i <10;i++) { for (int j=0;j <10;j++) { board[i][j]

#include #include #include #include

#define numships 5 int board[10][10]; int coordinate[2];

int teardown() { for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i][j] = 0; } } }

void initialize() { int sizeofship; int is_vertical = 0; int randx = 0; int randy = 0; int intersection = 0; do { intersection = 0; teardown(); for (int n = 1; n < numships+1; n++) { switch (n) { case 1: sizeofship = 5; break; case 2: sizeofship = 4; break; case 3: sizeofship = 3; break; case 4: sizeofship = 3; break; case 5: sizeofship = 2; break; default: break; } if (rand() % 2 == 1) { is_vertical = 1; } else { is_vertical = 0; } if (is_vertical) { randx = rand() % 10; randy = rand() % (10-sizeofship)+1; for (int m = 0; m < sizeofship; m++) { if (board[randx][randy+m] == 0) { board[randx][randy+m] = n; } else { intersection = 1; } } } else { randx = rand() % (10-sizeofship)+1; randy = rand() % 10; for (int m = 0; m < sizeofship; m++) { if (board[randx+m][randy] == 0) { board[randx+m][randy] = n; } else { intersection = 1; } } } } } while(intersection == 1); } void inp(char input[3]) { switch (toupper(input[0])) { case 'A': coordinate[0] = 0; break; case 'B': coordinate[0] = 1; break; case 'C': coordinate[0] = 2; break; case 'D': coordinate[0] = 3; break; case 'E': coordinate[0] = 4; break; case 'F': coordinate[0] = 5; break; case 'G': coordinate[0] = 6; break; case 'H': coordinate[0] = 7; break; case 'I': coordinate[0] = 8; break; case 'J': coordinate[0] = 9; break; default: printf("Invalid coordinate"); break; } if (input[1] != ' ') { printf("Invalid coordinate"); } switch (input[2]) { case '0': coordinate[1] = 0; break; case '1': coordinate[1] = 1; break; case '2': coordinate[1] = 2; break; case '3': coordinate[1] = 3; break; case '4': coordinate[1] = 4; break; case '5': coordinate[1] = 5; break; case '6': coordinate[1] = 6; break; case '7': coordinate[1] = 7; break; case '8': coordinate[1] = 8; break; case '9': coordinate[1] = 9; break; default: printf("Invalid coordinate"); break; } }

int update() { int hitormiss; if (board[coordinate[0]][coordinate[1]] != 0) //0 is empty space, 9 is a shot, other numbers are ships { hitormiss = 1;//hit } else { hitormiss = 0; } board[coordinate[0]][coordinate[1]] = 9; return hitormiss; }

void display(int hit) { if(hit == 0) { printf("You have missed the shot. Get better. "); } else { printf("You have hit your target. Keep it up. "); } for (int i = 0; i < 10; ++i) { for (int j = 0; j < 10; ++j) { printf("%d ", board[i][j]); } printf(" "); } }

int main() { char input[3]; initialize(); while (input[0] != 'z') { printf("Enter a coordinate A-J, 0-9 (ex. D 7), z to stop : "); scanf("%[^ ]%*c", input); inp(input); display(update()); } teardown(); }

please anyone can help me to update the above c program (battleship game) just i want to do a minesweeper game only just i want to change that game in the form of the following way

Create functions:

  • Initialization print Setting up the game
  • Teardown print Destroying the game
  • Accept Input accept a letter for a command. The commands are the things that you can do to the board: Flag a square, unflag a square, assert that the square is bomb-free, and quit the game. Accept coordinates if appropriate. Prompt and error check appropriately.
  • Update the state of the world pass the input data to this function. Since we do not have a world right now, choosing (0,0) as bomb free will print BOOM, other moves will print OK
  • Display the state of the world Print the result calculated in the state of the world. Later, this should print the board.
  • Main call initialization, then loop until a flag is set, calling accept input, update and display. Outside of the loop, call teardown.

Make variables to define the size of the board (width and height). These can be global variables.

0

Enter your vertical coordinate (0 - 10) and press enter

0

BOOM!

(F) flag a spot as a mine, (R) remove a flag, (A) assert that a spot is mine free, (Q)uit

(F) flag a spot as a mine, (R) remove a flag, (A) assert that a spot is mine free, (Q)uit

Q

Destroying the game

Note that sometimes the prompt for a command prints twice. Dont worry about this if yours does it (or doesnt do it).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions

Question

Evaluate employees readiness for training. page 275

Answered: 1 week ago