Problem statement:

You will write a program that plays the game Battleship. In this program, you will be graded on having functions, as well as the ability to play the game correctly.

The game is described at: https://www.hasbro.com/common/instruct/Battleship.PDF

This is traditionally a 2-person game, where each player picks where to put their ships on a 10 x 10 matrix. Ships can touch each other, but can't both occupy the same spot. Your computer game will simulate this process by first asking player 1 where he/she wants to put the ships, and then asking player 2.

Each player is given the following ships, and each ship takes up a specific number of spots on the 10 x 10 grid:

  • 1 Aircraft Carrier, 5 spots
  • 1 Battleship, 4 spots
  • 1 Destroyer, 3 spots
  • 1 Submarine, 3 spots
  • 1 Patrol Boat, 2 spots

After you determine where each player wants to put their ships, then you can prompt each player to choose a position on the opponent's board. If there is a boat in the corresponding position on the opponents board, then it was a hit, and you can use whatever you want to symbolize that it was a hit, i.e. X or 1.

In the case of a hit, the other player must mark the hit on his setup board containing the ships. Whenever a ship is sunk, the game must announce to your opponent that their ship has sunk. If there isn't a boat in the opponents position, then it is a miss, and you can use whatever you want to symbolize a miss on your board, i.e. N or 0.

The player who sinks all of their opponent's ships first is the winner!

Tips

1. You can clear the screen after each player chooses their positions for their ships, by using system("clear") available by including the stdlib.h header file.

2. Some example functions you might want to include are:

  • initialize_board(), which initializes the boards to spaces,
  • determine_player_choice() that allows players to pick their spot on their opponents grid,
  • fill_board(), which fills the board with the player's choice
  • print_board() that prints the board to the screen after each user's turn,
  • check_for_winner(), which checks to see if there is a winner, and a
  • print_winner_results() that prints the results of the game to the screen.

3. You can print the X's or 1s red by using the following code: cout << "\033[22;31m X \033[01;37m"; \033[22;31m turns your text red, and \033[01;37m turns your text back to white.

Requirements:

  • You must provide a message for each action and result of that action.
  • Ships can touch each other, but can't both occupy the same spot.
  • Ships can only be arranged horizontally or vertically on the board, and they can't hang over the grid!
  • You should use functions.
  • You must not have functions over 15-20 lines long.
  • You must not use global variables
  • At the end of the game display for each player:
    • Ships still standing
  • You must not have any memory leaks
  • You must detect and handle these invalid inputs by asking the user to enter again. Invalid position to move the ship on:
    • If a ship is already at that position
    • If the position does not exist on the matrix
    • Any other possibility
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.