|
|
Course Project
This homework will continue to develop the course project, which is the
game of Battleship.
We have developed a number of classes so far:
- Ship (HW J6): Represents a ship, of course. This can be any
size ship, placed anywhere on the board.
- Cell (HW J6): A cell is a single spot on a board. Thus, a
sample board could have a 15x15 grid of cells.
- Descriptions (Lab 9): This class allows one to pick the names of
their ships.
For each of these classes, you can either use your previous code, or use
our solutions. To use our solutions, click on the link, and select
"save as". Note that we are only providing the .class files for the
solutions. If you are unsure if your code works properly, then it might be
safer to use our solutions. You should save all
your code in a separate directory than HW J6, so you do not overwrite your
files.
In this homework, you will develop one class:
- ShipList (HW J7): A list of ships. For example, each player
(human or computer) will have a list of 5 (or so) ships that were placed
on the board.
We will provide you with one class for this homework:
- Game: The class with the main() method that will run the game.
It ties together all the other classes.
The remaining assignments will develop the remaining classes:
- Board (HW J9): This class represents the board that players place
their ships on, and takes shots on as well.
- Parser (Lab 10): This class reads in commands from the keyboard, and
figures out (or 'parses') what the player means by them.
- Human (HW J8): A class to represent the human player in the game.
This class will be in charge of reading in input from the keyboard as to
where to take the next shot, for example
- MapPrinter (Lab 11): This allows easy printing of the board to the
text-based screen.
- AI (HW J8): This class represents the computer player in the game
(AI stands for Artificial Intelligence). It is responsible for
making the decisions as to which move to make next.
Purpose
For this homework, you are responsible for implementing the ShipList
class. You must submit this class, along with the Game.java file.
We are not providing skeleton code for ShipList.java, but are providing a
skeleton code for Game.java
ShipList class
The class you must implement for this homework represents a list of ships
in the game. Each player will have his or her own list of ships that
are on the board; the computer player will also have a list of ships.
Nothing should be static. All
fields must be private. Unless specified otherwise,
all methods must be public.
The purpose of writing this class (and thus this homework) is to give you
experience using one dimensional arrays. Thus, all the methods listed
here must use arrays -- you CANNOT use Vectors (or anything other
than arrays).
Properties
The ShipList class only has two properties:
- int shipCount: The number of ships in the list
array.
- Ship[] list: The array that holds the ships.
Note that the array might be of size 4, but there are only 2 ships
stored in it (the other 2 positions are null). In this
case, list.length would be 4, but count
would be 2.
Constructors
There is only a need for the default constructor in this class.
- ShipList(): Sets count to 0, and
list to an array of size 2.
We will want to eventually be able to add more ships to the ShipList than
the array of size 2 can hold (i.e. we will want to add 5 ships at some
point). This will be handled in the increaseCapacity() method, which is described below.
Accessors and Mutators
While normally you would have an accessor and mutator for each of the
properties listed above, in this class, that does not make sense. The
count field should not be modified by the code using this class
(what if you modify count, but without adding or removing
ships?).
Thus, the only accessor that should be in this class is
getShipCount(); no mutators should be present.
Other Methods
Note that one of these methods should be private (increaseCapacity()).
Most of the methods that require searching through the
list[] array will require a for loop to do so.
- void increaseCapacity(): This method will increase
the capacity of the list[] array. It is discussed
in more detail on slide 78 of the
array lecture slides.
- void add(Ship ship): This method will add the passed
ship onto the list[] array. We know how many
elements are in the array (via the shipCount field).
If there is no more space for the passed ship, then
increaseCapacity() should be called. Either way, the
passed ship is added to the array, and shipCount is
incremented.
- Ship containsShip(int x, int y): This method will
search through the array, and check if any of the ships contain the
position (x,y). Recall that the Ship class contains a
hasPoint() method that you implemented in HW J6 -- if any of
the ships' hasPoint() method returns true,
then this method should return that particular Ship. Note that we
are not worrying about overlapping ships at this point -- if multiple
ships are on the same point, you only have to return one of them (i.e.
the first one you find). If none contain the point, this method
should return null.
- Ship getShip (int index): This method will just
return the ship at position index in the array. If
index is an invalid value (negative, or greater than or equal
to shipCount), it should return null.
- int getShipNumber (Ship ship): This method will
search through the array, and search for the passed Ship. If
found, it will return the index of that ship in the array; if not
found, it will return -1. The Ship class does not have a
.equals() method (as we did not program one in HW J6).
Thus, in order to tell if two ships are equal, you should ensure that
both their names and their lengths are the same.
- boolean allShipsSunk(): This method will return
true or false, depending on if all the ships
are sunk. Each ship in the list has an isSunk()
method, which you implemented in HW J6. If all of them are true,
then this method returns true. If any one (or more) is false, then
this method returns false.
- void print(): This method should print out all the
ships in the ShipList. Recall that the Ship class has a
toString() method. Thus, this method should print out the
index (very important!) and what the toString() method
returns for that Ship. We need to print out the index (i.e. the
position in the array) so that we can tell where that ship is on the
board (we'll see this in HW J8).
Game class, Revisited
Your main() method in the Game.java file should test all
of the methods that you implement. In HW J6, we showed you how to do
this for the Cell class, and required you to do so for the Ship class.
This homework requires the same, but for the ShipList class. You can
create a bunch of ships (using the default constructor would be the
easiest), and start calling the methods you implemented in your ShipList
class. It will be much easier if you implement a
method at a time, then write the test code in Game.java to ensure that it
works correctly!
Good Programming Practices
All the good
programming practices from HW J1 should be included here as well.
Since you are providing code in the
main() method of the Game class to test your other code, you don't need
to include execution runs as comments.
Submission
When you are finished,
submit
both the ShipList.java and Game.java files.
|
|