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

Problem: - Write a C program specification below: In this program you will be mi

ID: 3911148 • Letter: P

Question

Problem:

- Write a C program specification below:

In this program you will be mimicking 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”. The program should ensure that the slots are consecutive. In other words, your ship cannot be placed at slot 2 and at slot 6. Since the board has only 10 slots, valid slots are in the range of 0 to 9.

void setComputerBoard(int *)

Exactly the same as the setBoard method except that the position of the computer’s ship is set randomly. The computer’s ship must also be in consecutive slots.

intplayGame(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 computer’s board is a ship, a “HIT” message is displayed. If the slot on the computer’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.

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 0 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

You can assume the input will always be a positive integer.

Important: You should use pointer notation when referencing values inside an array as opposed to array notation. For example, use *ptrToArray instead of array[0] and *(ptrToArray+1) instead of array[1], etc. You will lose 10 points for not following this notation.

A sample run of the code is below –

Enter 1st position: 4

Enter 2nd position: 5

Computer guesses 9

MISS!

Human Board:

0 1 2 3 4 5 6 7 8 9

* * * * S S * * * M

Computer Board:

0 1 2 3 4 5 6 7 8 9

* * S S * * * * * *

Enter guess: 4

You guessed 4

MISS!

Human Board:

0 1 2 3 4 5 6 7 8 9

* * * * S S * * * M

Computer Board:

0 1 2 3 4 5 6 7 8 9

* * S S M * * * * *

Computer guesses 4

HIT!

Human Board:

0 1 2 3 4 5 6 7 8 9

* * * * H S * * * M

Computer Board:

0 1 2 3 4 5 6 7 8 9

* * S S M * * * * *

Enter guess: 3

You guessed 3

HIT!

Human Board:

0 1 2 3 4 5 6 7 8 9

* * * * H S * * * M

Computer Board:

0 1 2 3 4 5 6 7 8 9

* * S H M * * * * *

Computer guesses 7

MISS!

Human Board:

0 1 2 3 4 5 6 7 8 9

* * * * H S * M * M

Computer Board:

0 1 2 3 4 5 6 7 8 9

* * S H M * * * * *

Enter guess: 9

You guessed 9

MISS!

Human Board:

0 1 2 3 4 5 6 7 8 9

* * * * H S * M * M

Computer Board:

0 1 2 3 4 5 6 7 8 9

* * S H M * * * * M

Computer guesses 3

MISS!

Human Board:

0 1 2 3 4 5 6 7 8 9

* * * M H S * M * M

Computer Board:

0 1 2 3 4 5 6 7 8 9

* * S H M * * * * M

Enter guess: 5

You guessed 5

MISS!

Human Board:

0 1 2 3 4 5 6 7 8 9

* * * M H S * M * M

Computer Board:

0 1 2 3 4 5 6 7 8 9

* * S H M M * * * M

Computer guesses 0

MISS!

Human Board:

0 1 2 3 4 5 6 7 8 9

M * * M H S * M * M

Computer Board:

0 1 2 3 4 5 6 7 8 9

* * S H M M * * * M

Enter guess: 2

You guessed 2

HIT!

Human Board:

0 1 2 3 4 5 6 7 8 9

M * * M H S * M * M

Computer Board:

0 1 2 3 4 5 6 7 8 9

* * H H M M * * * M

Human wins!

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote