Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a battleship program. Include Javadoc and comments. Grade 12 level coding style using java only. You will need to create a Ship class, Location

Create a battleship program. Include Javadoc and comments. Grade 12 level coding style using java only. You will need to create a Ship class, Location class, Grid Class, Add a ship to the Grid, Create the Player class, the Battleship class, Add at least one extension. Make sure to include the tester. Example, Ship.java and ShipTester.java. Starting code/ sample/methods will be given for each class. Thank you.

The first part of writing our Battleship game is to write the Ship.java class. The Ship represents a Ship in the game, or a piece on the board. The Ship has several defining characteristics (think instance variables!) including a position, a length and a direction.

Youll need a few instance variables

row - What row location is the ship at? col - What column location is the ship at? length - How long is this ship? direction - Is this ship vertical or horizontal?

To keep track of the direction you should use a few constants:

// Direction constants public static final int UNSET = -1; public static final int HORIZONTAL = 0; public static final int VERTICAL = 1;

The Ship class should have one constructor, taking a parameter of the length of the ship.

// Constructor. Create a ship and set the length. public Ship(int length) // Has the location been initialized public boolean isLocationSet() // Has the direction been initialized public boolean isDirectionSet() // Set the location of the ship public void setLocation(int row, int col) // Set the direction of the ship public void setDirection(int direction) // Getter for the row value public int getRow() // Getter for the column value public int getCol() // Getter for the length of the ship public int getLength() // Getter for the direction public int getDirection() // Helper method to get a string value from the direction private String directionToString() // Helper method to get a (row, col) string value from the location private String locationToString() // toString value for this Ship public String toString()

--------------------------------------------------------------------------------------------------------------------------------------------------------

The next class to write is the Location.java file. The Location class stores the information for one grid position.

A location has two defining attributes

1) Is there a ship at this location?

2) What is the status of this location?

The status of this would be whether this position is unguessed, we got a hit, or got a miss.

public static final int UNGUESSED = 0; public static final int HIT = 1; public static final int MISSED = 2;

These are the methods you will need to implement for the Location class.

// Location constructor. public Location() // Was this Location a hit? public boolean checkHit() // Was this location a miss? public boolean checkMiss() // Was this location unguessed? public boolean isUnguessed() // Mark this location a hit. public void markHit() // Mark this location a miss. public void markMiss() // Return whether or not this location has a ship. public boolean hasShip() // Set the value of whether this location has a ship. public void setShip(boolean val) // Set the status of this Location. public void setStatus(int status) // Get the status of this Location. public int getStatus()

-------------------------------------------------------------------------------------------------------------------------------------------------------

Now that we have written the Ship class and the Location class, the next thing is to write the Grid class. The Grid class represents a Battleship Grid both for the user player and the computer. This will be used both as a grid to track where your ships are, as well as track the guesses that the user or computer has made.

The main state to track in the Grid is a 2-dimensional array of Location objects.

A few important variables youll want to have here are

private Location[][] grid; // Constants for number of rows and columns. public static final int NUM_ROWS = 10; public static final int NUM_COLS = 10;

To start off, you should implement the basic functionality to make this Grid class a little easier to use than a simple 2D array. Here are the methods you need to implement.

// Create a new Grid. Initialize each Location in the grid // to be a new Location object. public Grid() // Mark a hit in this location by calling the markHit method // on the Location object. public void markHit(int row, int col) // Mark a miss on this location. public void markMiss(int row, int col) // Set the status of this location object. public void setStatus(int row, int col, int status) // Get the status of this location in the grid public int getStatus(int row, int col) // Return whether or not this Location has already been guessed. public boolean alreadyGuessed(int row, int col) // Set whether or not there is a ship at this location to the val public void setShip(int row, int col, boolean val) // Return whether or not there is a ship here public boolean hasShip(int row, int col) // Get the Location object at this row and column position public Location get(int row, int col) // Return the number of rows in the Grid public int numRows() // Return the number of columns in the grid public int numCols() // Print the Grid status including a header at the top // that shows the columns 1-10 as well as letters across // the side for A-J // If there is no guess print a - // If it was a miss print a O // If it was a hit, print an X // A sample print out would look something like this: // // 1 2 3 4 5 6 7 8 9 10 // A - - - - - - - - - - // B - - - - - - - - - - // C - - - O - - - - - - // D - O - - - - - - - - // E - X - - - - - - - - // F - X - - - - - - - - // G - X - - - - - - - - // H - O - - - - - - - - // I - - - - - - - - - - // J - - - - - - - - - - public void printStatus() // Print the grid and whether there is a ship at each location. // If there is no ship, you will print a - and if there is a // ship you will print a X. You can find out if there was a ship // by calling the hasShip method. // // 1 2 3 4 5 6 7 8 9 10 // A - - - - - - - - - - // B - X - - - - - - - - // C - X - - - - - - - - // D - - - - - - - - - - // E X X X - - - - - - - // F - - - - - - - - - - // G - - - - - - - - - - // H - - - X X X X - X - // I - - - - - - - - X - // J - - - - - - - - X - public void printShips()

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Now we need to extend our Grid class with a new method.

/** * This method can be called on your own grid. To add a ship * we will go to the ships location and mark a true value * in every location that the ship takes up. */ public void addShip(Ship s)

This method should go to the Locations in the Grid that the Ship is in and set the boolean value to true to indicate that there is a ship in that location. You have some handy helper methods already to do this!

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Now that we have written Ship, Location, and Grid, now it is time to write the Player class.

Player will handle the logic for our two players, the user player and the computer player.

Each player has a few key characteristics.

They have a list of 5 ships. Each ship is of a set length. You have one ship of length 2, two ships of length 3, one ship of length 4 and one ship of length 5.

Putting these values in an array will be very handy.

// These are the lengths of all of the ships. private static final int[] SHIP_LENGTHS = {2, 3, 3, 4, 5};

So each Player should have a list of five ships. To store this:

Then each Player also will have 2 Grids. One grid represents that players grid, and the other grid represents their opponents grid. On the players grid, they will mark the location of their ships, as well as record the guesses of their opponent. On the opponents grid, the player will record their own guesses of where they think the battleships are.

In the constructor you should set up the instance variables.

For testing purposes here you may also want a method like

public void chooseShipLocation(Ship s, int row, int col, int direction)

This will set a ships row, column and direction and add it to the current players grid. You can use this to hard code in testing placement of battleships.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In this part well start writing our Battleship class, which hooks all of the parts of our game together.

Youll want to make two Player objects.

The goal for this part is to create two players, then be able to print out the current board status that they have, and then make a guess and have it update the underlying data stucture as well as show in the display.

Here you may need to go back and make modifications to your Player class.

We recommend writing a method in the Battleship class called:

askForGuess

Which asks the user player for a valid row and column to guess as a location of the opponents battleship. If there is a ship on the opponents board it should indicate that.

In this part we will finish the Battleship game

Here the key is making the game work really well, testing for robustness so you cant break the game, and also handling other edge cases.

First, the user should be asked to choose the locations of their battleships at the start.

You should ask for the row, column and direction (horizontal or vertical) in order to place the ships.

Then for the computer player you should automatically generate the locations of the initial ships.

After that youll want to alternate with the user making a guess and the computer making a guess. You should continue this until one of the players wins. The winning condition is if you have hit every battleship location.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This part of the project is for making extensions. There are lots of ways to improve this game. What can you think of? One idea is: can you leave a message if the user has sunk a battle ship? What touches can you add to the game?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

New Trends In Databases And Information Systems Adbis 2019 Short Papers Workshops Bbigap Qauca Sembdm Simpda M2p Madeisd And Doctoral Consortium Bled Slovenia September 8 11 2019 Proceedings

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Robert Wrembel ,Mirjana Ivanovic ,Johann Gamper ,Mikolaj Morzy ,Theodoros Tzouramanis ,Jerome Darmont

1st Edition

3030302776, 978-3030302771

More Books

Students also viewed these Databases questions