LAB 6 HoMEwORK ASSIGNMENT to be turned in Your task is to wr
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 * * * * * * * * * *



