Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program language is C. Please help. Please follow guide exactly and put comments

ID: 3912935 • Letter: P

Question

Program language is C. Please help. Please follow guide exactly and put comments in code. Make sure there’s good indentation, good variable names and consistent bracing.
Problem: Inthis program you will be m micking a game of Battleship. Your game board is a line of 10 slots. You have to implement 3 methods. The following methods must be implemented: void setBoard(int) This method sets up the human's game board. The method prompts the user for 2 slots to place the "ship slot 2 and at slot 6. Since the board has only 10 slots, valid slots are in the range of 0 to 9 The program should ensure that the slots are consecutive. In other words, your ship cannot be placed at void setComputerBoard(int) Exactly the same as the setBoard method except that the position of the computer's ship is set randomly. The computers ship must also be in consecutive slots. int playGame(int ",int This method should do the following steps Have the computer pick at random a slot to "fire at. If the slot the computer picked on the human's board is a ship, a HIT message is displayed. if the slot on the human's board is empty, a MISS message is displayed. Print out the computer's board and the human's board as described at the bottom of the page. After the computer goes, have the human pick a slot to "fire" at. If the slot the human picked on the computers board is a ship, a "HiT message is displayed. If the slot on the computers board is empty,a MISS" message is displayed. Print out the compüter's board and the human's board as described at the bottom of the page. Repeat steps 1 and 2 until there is a winner. The computer wins if both of the human's ship slots are hit. The human wins if both of the computer's ship slots are hit The playGame method returns a O if the computer won. The playGame method returns a 1 if the human won. The computer board is printed off with the following characters: (The human board is printed off similarly) s- printed off in the slots where the computer's ship is located. M - printed off in the slots where the human guessed incorrectly H - printed off in the slots where the ship is located and where the human hit. printed off in all other slots

Explanation / Answer

#include <stdio.h>

#include<stdlib.h>//to use random function from library

#include<time.h>//to use srand function

//in this function i used an array which store value 1 if the index are consecutive

//these index are the inputs from the user where you want to put ship in Human Board

void setBoard(int Board[]){

int index1,index2;

printf("Enter the first position ");

scanf("%d",&index1);

printf("Enter the second position ");

scanf("%d",&index2);

//check if indexes are in range of 0 to 9 and consecutive.

if((index1>=0&&index1<9)&&(index2>0&&index2<=9)&&(index2==index1+1)){

Board[index1]=1;

Board[index2]=1;

}

else{

printf("invalid input ");

}

}

//here Cboard picks random number using rand function and store 1 at those position

//random position will be the indexes where we computer put its ship

void setComputerBoard(int CBoard[]){

int r=rand()%9;//check for random number between 0 to 8 inclusive

CBoard[r]=1;

CBoard[r+1]=1;

}

int playgames(int Board[],int CBoard[]){

int guess,pick,i,sumH,sumC;

// H array to store the value 1 if that index is hit by human.

// C array to store the value 1 if that index is hit by computer

int H[]={0,0,0,0,0,0,0,0,0,0};

int C[]={0,0,0,0,0,0,0,0,0,0};

//while loop to run infinite time until we find our winner

while(1){

pick=rand()%10;

//pick store the computer guess value.

printf("Computer Guess: %d ",pick);

if(Board[pick]==1){

printf("HIT! ");

C[pick]=1;//store 1 to C array at index pick so as to identify that this index or position is hit by computer

}

else

printf("MISS! ");

//printing human board

printf("Human Board: ");

for(i=0;i<10;i++)

printf("%d",i);

printf(" ");

for(i=0;i<10;i++){

if(i==pick){ //if i-th index is equal to what is guessed by computer than it maybe a miss or hit and so we check

if(Board[i]==0)//for miss

printf("M");

if(Board[i]==1)//for hit

printf("H");

}

else{

if(Board[i]==0)

printf("*");

if(Board[i]==1)

printf("S");

}

}

printf(" ");

//computer board

printf("Computer Board : ");

for(i=0;i<10;i++)

printf("%d",i);

printf(" ");

for(i=0;i<10;i++){

if(CBoard[i]==0)

printf("*");

if(CBoard[i]==1)

printf("S");

}

printf(" ");

//human guess stored in guess variable

printf("Enter your Guess");

scanf("%d",&guess);

printf("your Guess: %d ",guess);

if(CBoard[guess]==1){

printf("HIT! ");

H[guess]=1;//store 1 to H array at index guess so as to identify that this index or position is hit by human

}

else

printf("MISS! ");

//human board slot after human guess

printf("Human Board Slot ");

for(i=0;i<10;i++)

printf("%d",i);

printf(" ");

for(i=0;i<10;i++){

if(i==pick){

if(Board[i]==0)

printf("M");

if(Board[i]==1)

printf("H");

}

else{

if(Board[i]==0)

printf("*");

if(Board[i]==1)

printf("S");

}

}

printf(" ");

//computer board after human guess

printf("Computer Board Slot ");

for(i=0;i<10;i++)

printf("%d",i);

printf(" ");

for(i=0;i<10;i++){

if(i==guess){//if i-th index is equal to what is guessed by human than it maybe a miss or hit and so we check

if(CBoard[i]==0)//miss

printf("M");

if(CBoard[i]==1)//hit

printf("H");

}

else{

if(CBoard[i]==0)

printf("*");

if(CBoard[i]==1)

printf("S");

}

}

printf(" ");

//sumH counts in H array

//sumH counts in C array

//when both the indexes of H or C are 1 denoting winning situation than sumH or sumC will be 2

sumH=0;sumC=0;

for(i=0;i<10;i++){

sumH=sumH+H[i];

sumC=sumC+C[i];

}

if(sumH==2)

return 1;//human wins

if(sumC==2)

return 0;//computer wins

}

}

int main(void) {

int Board[]={0,0,0,0,0,0,0,0,0,0};

//initializing human board 0 which will 1 on given position

int CBoard[]={0,0,0,0,0,0,0,0,0,0};//computer board that store random positions

int win;

setBoard(Board);

srand(time(0));//random function

setComputerBoard(CBoard);

win=playgames(Board,CBoard);

printf(" %d",win);

return 0;

}