In this assignment, you will write a program to play a version of the game of Battleship against the computer. Our simplified version of the game will be played on a single 8 by 8 grid. Before the actual game, each player secretly places 6 ships and 4 grenades on the grid. Ships and grenades are a single position on the grid. The position of these ships and grenades are of course hidden from the opponent. Once both players have placed their ships and grenades, the actual game starts. Each player, in turn, "shoots a rocket" on the grid (i.e. calls a position).

  • If the rocket (the position called) falls on a position where there is nothing, then nothing happens, and the other player can shoot his/her rocket.
  • If the rocket falls on a coordinate where the opponent (or the player) has a grenade, then the player loses a turn, and next time, the opponent will play twice in a row.
  • If the rocket falls on a coordinate where the opponent (or the player...) has a ship, then that ship sinks.
  • If the rocket falls on a coordinate that has been called before, regardless of what was there before, nothing happens. (So, for example, a grenade can only explode once).

The goal of the game is to sink all of your opponent's ships before your opponent sinks yours.

For the sake of simplicity, you can assume that ships and grenades cannot overlap. So, you cannot have 2 grenades on the same position, have 2 ships on the same position, or have a grenade on a ship.

For example, let's consider the following grid: see image.

Here, one player (identified in black) has his/her 6 ships at positions A1, G1, A4, C4, G4 and A6 and 4 grenades at positions D1, B6, B7 and H6. The other player has his/her ships at H2, A3, E3, H4, E5 and C8 and 4 grenades at positions H1, B2, C6 and H7.

If black starts the game and "shoots a rocket" at position D2 then nothing happens, and we have the following grid.

Figure: see image.

It is white's turn now. White shoots at G4. The rocket hits one of blacks ship. The ship sinks. We now have: see image.

Black shoots at H1. The rocket hits a grenade. Black will lose a turn. We now have: see image.

White shoots at H8. Nothing.

Figure: see image.

White shoots again (remember, black lost a turn). White shoots at G1. It sinks another of black's ships.

Figure: see image.

White plays now. He is a bit confused and shoots at H4. He just sunk his own ship...

Figure: see image.

White shoots at a safe position H1. It is a grenade that already exploded. Nothing happens.

Figure: see image.

The game goes on until one player has all his/her ships sunk and he/she loses the game.

Your program must simulate a game of battleship between a human user (H) and a computer (C). The program must:

1. Ask H to place his/her ships and grenades. For each ship and grenade, 1 coordinate will be entered (a character followed by an integer). You can assume that the user will enter a character followed by an integer. However, your program must check that the coordinates are within the grid and that there is nothing already at that position. If the character is not between 'A' (or a) and H (or h) or the integer is not between 1 and 8 or there is already a grenade or a ship at that position, then your program must display an error message and let the user try again.

2. Place C's own ships and grenades at random, making sure that the coordinates are valid and that there is nothing at that position already.

3. Display the initial grid hiding all ships and grenades. To display the grid, use the following convention:

  • a position that has never been called should be displayed with the character '_'
  • a position that has been called and hit a ship should be displayed with the character 's' if the ship belongs to the user and S if the ship belongs to the computer.
  • a position that has been called and hit a grenade should be displayed with the character 'g' if the grenade belongs to the user and G if the grenade belongs to the computer.
  • a position that has been called and did not hit anything should be displayed with the character '*'
  • a position that has already been called should be displayed the same way as it was after its first call

4. Let H play first. Ask for the position of H's rocket and modify the grid according to the rules of the game. Then display the new grid showing only the positions that have been called to date using the convention above.

You can assume that the user will enter coordinates of the correct type (i.e. a character followed by an integer), but you must verify that the values are within the grid. If the character is not between 'A' (or a) and H (or h) or the integer is not between 1 and 8, then your program must display an error message and let the user try again.

5. Let C play. Generate a random position for C's rocket and modify the grid according to the rules of the game. Then display the new gird showing only the positions that have been called to date using the convention above.

To have C play, your program will simply generate a position at random every time.

6. Keep simulating the game and display the grid after each turn, and eventually declare a winner.

7. Once the winner is declared, the grid is then displayed showing the position of all ships and grenades.

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.