Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 7: Battleship Blueprint class for Player objects 15 points; pair-optional This problem continues our implementation of the game of Battleship, which is described in

Problem 7: Battleship Blueprint class for Player objects

15 points; pair-optional

This problem continues our implementation of the game of Battleship, which is described in the previous problem.

Your tasks

  1. (0 points) We are giving you the complete implementation of two other blueprint classes that you will need for Battleship:

    • Guess.java: This class is a blueprint for objects that represent a single guess i.e., a single (row, column) pair.

    • Board.java: This class is a blueprint for objects that represent a single players board.

    Download these two files, storing them in the folder that youre using for this assignment. These classes will not compile until you complete the remaining tasks.

    You should not make any modifications to these two classes.

  2. (0 points) We are also giving you the beginnings of a blueprint class for Player objects that will be used to represent a player of the game: Player.java

    Store this file in the same folder as the other classes for this assignment. Our starter code includes some of the necessary fields and methods. In addition, we have given you headers and in some cases partial implementations for a number of other methods.

  3. (0 points) Begin by carefully reviewing the code we have provided.

    When you review the nextGuess method in the Player class, youll notice that the type of player represented by this class makes random guesses. (You dont need to fully understand how the random numbers are chosen!) More specifically, nextGuess chooses a random position on the other players board that hasnt been tried before.

    Because this type of player makes random guesses, nextGuess doesnt make use of theScanner object that is passed in as a parameter. In Problem Set 3, youll create classes for other types of players, including one for human players in which the nextGuessmethod will need a Scanner object, and thats why a Scanner is included as a parameter in this version of nextGuess as well.

  4. Answer questions about the provided code (6 points) Use a text editor to create a plain-text file named ps2_code_reading.txt for your work on this part of the problem.

    After a careful review of the provided code, answer the following code-reading questions:

    1. How many fields does a Guess object have? If a client has a Guess object g, how would it obtain the values of its fields?

    2. Once a Guess object has been created, is it possible for a client to change the values of the fields? Why or why not?

    3. What fields in a Board object are used to keep track of the ships on the board, and what does each of them represent?

    4. Lets say that a client wants to determine whether the position (r, c)where r and care integersis the location of a prior hit or a prior miss on the Board object b. Write the single method call using the variables r, c, and b that the should the client make. What will the value returned by this call mean?

  5. Add new fields and update the constructor (2 points) Add two new fields to the Player class:

    • one called ships that represents an array of Ship objects; it will be used to store the players collection of ships
    • one called numShips that stores an integer that will be used to keep track of the number of ships in the players collection of ships.

    Note that you do not need to keep track of where the ships are on the board. The code that we have given you in the Board class already does that.

    Start by defining the new fields, making sure to protect them from direct access by client code. Then modify the Player constructor to initialize the new fields. In particular, you should:

    • create an array that is big enough to store 5 ships, and assign it to the ships field. Use the provided class constant SHIPS_PER_PLAYER to specify the length of the array, rather than hard-coding the integer 5.

    • initialize the numShips field, giving it a value of 0.

    Important: You should not change the header of the constructor, because no additional parameters are needed.

  6. Complete the remaining Player methods (7 points) We have given you partial implementations for the remaining Player methods, which you should now complete. Make sure that you use the method headers that we have provided, without changing them in any way.

    Here are the steps you should take:

    • Complete the method named addShip, which takes a reference to a Ship object as a parameter. If the Player has room for another Ship, the method should add the specified ship to the players collection of ships and increment the count of the number of ships in the collection. If the player already has the maximum number of ships, the method should throw an IllegalStateException with an appropriate error message.

    • Complete the method named removeShip, which takes a reference to a Ship object as a parameter and should remove that Ship from the players collection of ships.

      • Your method will need to search through the collection of ships for a ship that is equal to the ship passed in as a parameter.

      • Once you have found a match for the ship, you should remove it by replacing it with the rightmost ship in the collection. For example, if the collection of ships is currently {Battleship, Cruiser, Tanker, Patrol Boat} and you are removing the Cruiser, you should replace it with the last ship (the Patrol Boat), and thus the resulting collection would be {Battleship, Patrol Boat, Tanker}. Make sure that you also decrease the count of the number of ships in the collection.

      • If the ship passed in as a parameter is not found in the collection of ships, the method should just return without changing the collection.

    • Complete the method named printShips, which should print the current contents of the players collection of ships. This method may be useful for debugging purposes. Each ship should be printed on a separate line. For example, if the players current collection is {Battleship, Patrol Boat, Tanker}, the output of printShips would look like this:

      Battleship of length 4 Patrol Boat of length 2 Tanker of length 3 

      Note that you can simply print each ship in the collection, one at a time, and thetoString() method that you implemented in the Ship class will provide the appropriate string to be printed. You do not need to do anything special when the collection of ships is empty.

    • Complete the method named hasLost. It should return true if the player has lost (i.e., if it has no remaining ships in its collection) and false otherwise.

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

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions

Question

Does it exceed two pages in length?

Answered: 1 week ago

Question

Does it avoid typos and grammatical errors?

Answered: 1 week ago