Outlab 6: Battleship

Due: Monday 8 Aug at 10 pm

Purpose

Apply everything you have learned this semester to solve a moderately sized problem (with many little sub-problems) with minimal guidance. i.e. Show me how well you can think for yourself.

Partners

This is a lab that allows you to work with 1 partner. A group of two is the maximum.

Problem Statement

You will build an application that allows a person to play Battleship against the computer. I will provide you a Driver and a class to represent spots on the board. You must use what I provide and build your own class to represent the game. Your application will create the board, place ships randomly, ask the user for spots they want to shoot at, report/update the result, and repeat until the game is over (all ships are hit in each spot they occupy). You must also be able to report the number of guesses it took the user to finish the game. There will also be limited error checking as described below.

• Create an Outlab6 project and type this into a Driver, and this into a class called Spot. Neither of these will be changed.
• Read through the Driver and understand the flow of how this program will execute.
• Here are some specifications and assumptions for you:
• The board size can change in the Constructor call. Just because the current Driver makes a 5x7 board does not give you permission to assume it will always be that size. You can assume the board will always be big enough to put the ships in randomly (5x4 or 4x5).
• The numbering of rows and columns in the printed output must start at 1, not 0. The user will use those values to reference spots in the board. (see output)
• There are 4 ships that need to be placed on the board. The Carrier occupies 5 spots, the Battleship 4, Submarine 3, and Patrol Boat 2.
• The ships will be placed randomly (random location, random vertical/horizontal orientation). This ships will not be placed diagonally and must not overlap.
• The output style and user interaction must match the sample output below.
• The following errors must be caught: sample
1. The user cannot input a row or column value that does not exist.
2. The user cannot chose a spot that they already selected.
3. The user cannot input anything other than an int for row and column values (we'll talk about this on 19 November).
• Here is sample output.

Hints

• Good Luck! Use what we have learned this semester about design. Work one method at a time. Test test test.

Submission

By Monday 8 August at 10:00 pm, submit the file Battleship.java into the appropriate D2L dropbox folder. DO NOT SUBMIT .class files!

Grading - 20 points

• 2 points - The Constructor is simple and does not save the input parameters as instance variables.
• 2 points - The print method produces output formatted in the same way as the sample output correctly uses the boolean parameter.
• 5 points - The ships are all placed randomly (and not overlapping) and will be placed successfully every single time in a board of size (5x4 or 4x5).
• 4 points - The makeGuess() method works as the output suggests, to include handling the errors described above.
• 2 points - The over() and printStatistics() methods work and are very simple.
• 5 points - Good programming principles are followed (comments, minimal unnecessary code, appropriate delegation of actions,...).