LAB 6 HoMEwORK ASSIGNMENT to be turned in Your task is to wr

LAB 6 HoMEwORK ASSIGNMENT to be turned in Your task is to write a program to implement the game Battleship shown in the Figure below. size grid spaces in width and one grid space in height. Atthe start of the game, your program should to randomly select an initial of the then keep asking the the ship\'s location. until the user\'s guess hits any of the 5 grid spaces occupied by the ship. Once happens, your program should display the battleship (the grid) and the ship: the grid space guessed by the user should be marked with and the rest of the grid spaces occupied by the ship marked with \'b\' The format of the input and output of your program should follow exactly the format in the following example: Welcome to to Battleship I 1 2 3 4 5 6 7 8 9 10 A ship was placed somewhere on this grid. Your goal is to hit it to destroy it. Please enter a location in the grid to hit the ship: A11. Invalid location. Please try again. You missed it, try again. Invalid location. Please try again. You missed it, try again. E7

Solution

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void printGrid(char grid[10][10], int viewMode){
int i,j;
  
printf(\" \");
for(i=0;i<10;i++){
printf(\"%d \",i+1);
}
printf(\"\ \");
for(i=0;i<10;i++){
printf(\"%c \",\'A\'+i);
for(j=0;j<10;j++){
if(viewMode == 1){
printf(\"%c \",grid[i][j]);
}
else{
if(grid[i][j]!=\'*\'){
printf(\"%c \",\'*\');
}
else{
printf(\"%c \",grid[i][j]);
}
}
}
printf(\"\ \");
}
}

int main()
{
char grid[10][10];
  
int i,j;
for(i=0;i<10;i++){
for(j=0;j<10;j++){
grid[i][j] = \'*\';
}
}
printGrid(grid,0);
  
srand(time(NULL));
int column = rand()%6+1;
int row = rand()%10+1;
char row_letter = \'A\'+row-1;
//printf(\"column: %d, row: %d, rowletter:%c\ \",column,row,row_letter);
  
for(i=column-1;i<column+4;i++){
grid[row_letter-\'A\'][i]=\'b\';
}
int hit = 0;
printf(\"A ship was placed somewhere on the Grid. You goal is to hit it destroy it.\ \");
  
char input[2];
//printGrid(grid,1);
int count = 0;
while(hit == 0){
count ++;
printf(\"Please enter location int the grid to hit ship:\");
scanf(\"%s\",input);
  
printf(\"%c, %c\ \",input[0],input[1]);
  
if(\'A\'> input[0] || \'Z\'<input[0]){
printf(\"Invalid Input!!\");
}
if(row_letter == input[0]){
if(column<= input[1]-\'0\' && input[1]-\'0\'<column+5){
printf(\"Congratulations!!! You sank the ship after %d guesses.\ \",count);
hit = 1;
}
}
else{
printf(\"You Missed it. Please try again:\");
}
}
printGrid(grid,1);
return 0;
}

Outut:

sh-4.2$ main                                                                                                                                                    

    1  2  3  4  5  6  7  8  9  10                                                                                                                               

A   *  *  *  *  *  *  *  *  *  *                                                                                                                                

B   *  *  *  *  *  *  *  *  *  *                                                                                                                                

C   *  *  *  *  *  *  *  *  *  *                                                                                                                                

D   *  *  *  *  *  *  *  *  *  *                                                                                                                                

E   *  *  *  *  *  *  *  *  *  *                                                                                                                                

F   *  *  *  *  *  *  *  *  *  *                                                                                                                                

G   *  *  *  *  *  *  *  *  *  *                                                                                                                                

H   *  *  *  *  *  *  *  *  *  *                                                                                                                                

I   *  *  *  *  *  *  *  *  *  *                                                                                                                                

J   *  *  *  *  *  *  *  *  *  *

A ship was placed somewhere on the Grid. You goal is to hit it destroy it.                                                                                      

Please enter location int the grid to hit ship:@3                                                                                                               

@, 3                                                                                                                                                            

Invalid Input!!You Missed it. Please try again:Please enter location int the grid to hit ship:i9                                                                

i, 9                                                                                                                                                            

Invalid Input!!You Missed it. Please try again:Please enter location int the grid to hit ship:J1                                                                

J, 1                                                                                                                                                            

You Missed it. Please try again:Please enter location int the grid to hit ship:i4                                                                               

i, 4                                                                                                                                                            

Invalid Input!!You Missed it. Please try again:Please enter location int the grid to hit ship:A3

A, 3                                                                                                                                                            

You Missed it. Please try again:Please enter location int the grid to hit ship:A9                                                                               

A, 9                                                                                                                                                            

You Missed it. Please try again:Please enter location int the grid to hit ship:B2                                                                               

B, 2                                                                                                                                                            

You Missed it. Please try again:Please enter location int the grid to hit ship:B8                                                                               

B, 8                                                                                                                                                            

You Missed it. Please try again:Please enter location int the grid to hit ship:C2                                                                               

C, 2                                                                                                                                                            

Please enter location int the grid to hit ship:C8

C, 8                                                                                                                                                            

Congratulations!!! You sank the ship after 10 guesses.                                                                                                          

    1  2  3  4  5  6  7  8  9  10                                                                                                                               

A   *  *  *  *  *  *  *  *  *  *                                                                                                                                

B   *  *  *  *  *  *  *  *  *  *                                                                                                                                

C   *  *  *  b  b  b  b  b  *  *                                                                                                                                

D   *  *  *  *  *  *  *  *  *  *                                                                                                                                

E   *  *  *  *  *  *  *  *  *  *                                                                                                                                

F   *  *  *  *  *  *  *  *  *  *                                                                                                                                

G   *  *  *  *  *  *  *  *  *  *                                                                                                                                

H   *  *  *  *  *  *  *  *  *  *                                                                                                                                

I   *  *  *  *  *  *  *  *  *  *                                                                                                                                

J   *  *  *  *  *  *  *  *  *  *

 LAB 6 HoMEwORK ASSIGNMENT to be turned in Your task is to write a program to implement the game Battleship shown in the Figure below. size grid spaces in width
 LAB 6 HoMEwORK ASSIGNMENT to be turned in Your task is to write a program to implement the game Battleship shown in the Figure below. size grid spaces in width
 LAB 6 HoMEwORK ASSIGNMENT to be turned in Your task is to write a program to implement the game Battleship shown in the Figure below. size grid spaces in width
 LAB 6 HoMEwORK ASSIGNMENT to be turned in Your task is to write a program to implement the game Battleship shown in the Figure below. size grid spaces in width

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site