Question

Program in C computer language In this program you will be mimicking a game of Battleship....

Program in C computer language

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:

1. 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.

2. 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.

3. 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.

This homework can be solved using char pointers as well. I would accept that as a valid solution too. It is your choice whether you want to use int pointers or char pointers.

You can also modify the parameters mentioned for the methods. As long as you have the methods performing the tasks stated in the problem, I will give credit.

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!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

#include<stdio.h>
#include <stdlib.h>
#include <time.h>
void setComputerBoard(char *);
int intplayGame(char *, char *);
void setBoard(char *);
void printBorad(char *);
int getRandomNumber();
int main()
{
   char humnaBoard[10];
   char computerBoard[10];
   int wins;
   srand(time(0));
   setBoard(humnaBoard);
   setComputerBoard(computerBoard);
   wins=intplayGame(humnaBoard, computerBoard);
   if(wins==1)
       printf("\nHuman Wins\n");
   else
       printf("\nComputer Wins\n");
   return 1;
}
void setBoard(char *board)
{
   char *p;
   int i=0,first_pos,second_pos;
   p=board;
   for (i = 0; i<10; i++)
   {
       *(p+i)='*';
   }
   printf("Enter 1st position: ");
   scanf("%d",&first_pos);
   printf("Enter 2nd position: ");
   scanf("%d",&second_pos);
   *(p+first_pos)='S';
   *(p+second_pos)='S';
}
void setComputerBoard(char *board)
{
   char *p;
   int i=0,first_pos,second_pos;
   p=board;
   for (i = 0; i<10; i++)
   {
       *(p+i)='*';
   }
   first_pos = (rand() % (8 - 0 + 1)) + 0;
   second_pos=first_pos+1;
   *(p+first_pos)='S';
   *(p+second_pos)='S';
}
int intplayGame(char *hBoard, char * cBoard)
{
   int computer_hit=0,human_hit=0;
   int computer_guess,human_guess;
   int i,j;
   do
   {
       computer_guess=getRandomNumber();
       printf("\nComputer guesses %d",computer_guess);
       if(*(hBoard+computer_guess)=='S')
       {
           printf("\nHIT!");
           *(hBoard+computer_guess)='H';
           computer_hit++;
       }
       else
       {
           printf("\nMISS!");
           *(hBoard+computer_guess)='M';
       }
       printf("\nHuman Board:");
       printBorad(hBoard);
       printf("Computer Board:");
       printBorad(cBoard);
       fflush(stdin);
       printf("\nEnter guess: ");
       scanf("%d",&human_guess);
       if(*(cBoard+human_guess)=='S')
       {
           printf("\nHIT!");
           *(cBoard+human_guess)='H';
           human_hit++;
       }
       else
       {
           printf("\nMISS!");
           *(cBoard+human_guess)='M';
       }
       printf("\nHuman Board:");
       printBorad(hBoard);
       printf("Computer Board:");
       printBorad(cBoard);
   }while(computer_hit!=2 && human_hit!=2);
   if(human_hit==2)
       return 1;
   else
       return 0;
}
void printBorad(char *board)
{
   int i=0;
   printf("\n");
   for (i = 0; i<10; i++)
   {
       printf("%d ",i);
   }  
   printf("\n");
   for (i = 0; i<10; i++)
   {
       printf("%c ",*(board+i));
   }
   printf("\n");
}
int getRandomNumber()
{
   return ((rand() % (9 - 0 + 1)) + 0);
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Program in C computer language In this program you will be mimicking a game of Battleship....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next...

    Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next two assignments you will be creating a one-player version of the game. The game is extremely simple. Each player arranges a fleet of ships in a grid. The grid is hidden from the opponent. Here is an ASCII representation of a 10x10 grid. The ‘X’s represent ships, the ‘~’s represent empty water. There are three ships in the picture: A vertical ship with a...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • In C++ program use the new style od C++ not the old one. Simple Battleship You...

    In C++ program use the new style od C++ not the old one. Simple Battleship You will make a game similar to the classic board game Battleship. You will set up a 5 x 5, 2 dimensional array. In that array, you will use a random number generator to select 5 elements that will act as the placeholders for your "battleships". Your user will get 10 guesses to "seek and destroy" the battleships. After their 10 guesses, you will tell...

  • PYTHON Write a program for playing a game with the computer. First, generate a random integer...

    PYTHON Write a program for playing a game with the computer. First, generate a random integer in the range of 6 through 9. This will be the computer’s pick. Then generate three random integers for the human player. These random integers are in the range of 1 through 10. If any of these three random integers is greater than the computer’s pick, the human player wins the game. Otherwise, the human player loses. You must use a for loop to...

  • PYTHON Stuck on this problem: Write a program for playing a game with the computer. First,...

    PYTHON Stuck on this problem: Write a program for playing a game with the computer. First, generate a random integer in the range of 6 through 9. This will be the computer’s pick. Then generate three random integers for the human player. These random integers are in the range of 1 through 10. If any of these three random integers is greater than the computer’s pick, the human player wins the game. Otherwise, the human player loses. You must use...

  • WRITE IN LANGUAGE C. THANKS Heroic Game Write a program that plays a simple game. Mostly...

    WRITE IN LANGUAGE C. THANKS Heroic Game Write a program that plays a simple game. Mostly the purpose of the program is to practice using structs. Here is a struct that contains information about a character in the game: typedef struct {   int hitPoints;    /* how much life */   int strength;     /* fighting strength */   char name[MAXNAME]; } Hero; The same structure is used for all characters (even non-heroic.) First write the function void printHero( Hero hr )that prints a...

  • Complete the methods from random import randint class Spinner: """A spinner for a board game. A...

    Complete the methods from random import randint class Spinner: """A spinner for a board game. A spinner has a certain number of slots, numbered starting at 0 and increasing by 1 each slot. For example, if the spinner has 6 slots, they are numbered 0 through 5, inclusive. A spinner also has an arrow that points to one of these slots. === Attributes === slots: The number of slots in this spinner. position: The slot number that the spinner's arrow...

  • Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...

    Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...

  • Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...

    Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...

  • Assume that you have a ten-slot closed hash table (the slots are numbered 0 through 9)....

    Assume that you have a ten-slot closed hash table (the slots are numbered 0 through 9). Show the final hash table that would result if you used the hash function h(k) = k mod 10 and pseudo-random probing. The list of numbers to be inserted in order are: 6, 15, 24, 19, 25, 44 The permutation of offsets to be used by the pseudo-random probing will be: 2, 5, 1, 8, 4, 9, 3, 6, 7

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT