Implement the classic game of Battleship, which allows a human player (user) to face the computer (CPU player).

Review

The game is played on two grids, one for each player. The grids are typically square – usually 10×10 – and the individual squares in the grid are identified by letter and number Before play begins, each player secretly arranges their ships on their grid. Each ship occupies a number of consecutive squares on the grid, arranged either horizontally or vertically. The number of squares for each ship is determined by the type of the ship. The types and numbers of ships allowed are the same for each player. There are two typical complements of ships: Type of ship Size Number per player

aircraft carrier 5 1
battleship 4 2
frigate 3 2
submarine 2 2

The application will ask to the user to specify the location where he wants to place the ships, and will verify that the position of the ships is correct. The ships cannot overlap (i.e., only one ship can occupy any given square in the grid) and a ship cannot “touch” another ship. If the position of the ships is not correct, the application will ask the player to specify the ships location again. After the ships have been positioned by the user, the application will randomly set the location of the CPU player ships.

The game proceeds in a series of rounds. User goes first, and the application will ask the user a grid (ie (2, 5)). If he hits his shoot, he will shoot again (successively). If he misses, his round is over. In the CPU player round, the application will generate a random grid and if it is a hit, the CPU will shoot again. The next shoot will be the grid on top of the previous hit, the grid on the right of the previous hit, the grid at the bottom of the previous hit, and the grid on the left of the previous hit. IN THAT ORDER. ie, if (3,5) is a hit, the next shoot will be (2,5). If it is another hit, next shoot will be (1, 5), and the CPU will look for the ship up and down. But if (2, 5) was a miss, the next shoot will be (3,6) and the CPU will look for the ship right and left.

If (3,6) was a miss, the next shoot will be (4,5) and if (4,5) is a miss, the next shoot will be (3,4) When all of the squares of a ship have been hit, the ship is sunk, if all of a player's ships have been sunk, the game is over and their opponent wins. The application has to implement a mechanism to leave the game if the user doesn’t want to keep playing.

Application Restrictions

  • The board is 10 x 10, but everything should be indicated through constants so if you want to play in a 15 x 15 board, you can play simply by changing the value of the constant of 10 to 15
  • No ships may be placed diagonally.
  • In the perimeter surrounding a ship you can´t place another ship
  • At home player's turn, the board is shown with the shots that have been made, the ships have sunk (placed on the board) and the state of our fleet (boats remaining afloat).
  • You can choose the way of representing the hits on the board and missed shots

Delivery

The application will be coded in Java. Be warned that you must use the characteristics of the programming language (object orientation, inheritance ,etc .). Using the Java language in a “Unnatural" way (ie, using Java to use an imperative programming) is not allowed and the project will be rejected.

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.