Hey everyone, it's approaching the end of the semester, and the last project I have to do is to create a Battleship game in a GUI.
I need to know how to start it. I have to create 3 programs, Ship.java, Square.java, and BattleBoard.java.
Ship.java:
Code
import java.util.*;
/**
* This class represents a single ship in a fleet of ships.
* It knows the length of the ship, the number of times it
* has been hit, it's orientation, the row with the upper
* left corner of the Ship, and the column containing the
* upper left corner of the Ship.
* @author Jackson Yuen
* @version 1.0
*/
public class Ship {
/**
* This constructor must initialize the class's instance
* variables using the parameter values.
*/
public Ship(int length, boolean isHorizontal, int startRow, int startCol) {
}
/**
* This returns the length of the ship
* @return length the length of the ship
*/
public int getLength() {
}
/**
* This returns true if the ship has horizontal orientation,
* false otherwise
* @return true if ship has horizontal orientation
* @return false if ship has vertical orientation
*/
public boolean isHorizontal {
}
/**
* This returns the row of the upper left corner of
* the ship.
* @return int row of the upper left corner of the ship
*/
public int getStartRow() {
}
/**
* This returns the column of the upper left corner of
* the ship
* @return int column of the upper left corner of the ship
*/
public int getStartCol() {
}
/**
* This simulates hitting the ship (updates the appropriate
* instance variable)
*/
public void hit() {
}
/**
* This returns true if the ship has been sunk (hit as
* many times as its length). It's false otherwise.
* @return true if ship has been sunk
* @return false if ship has not been sunk
*/
public boolean isSunk() {
}
/**
* This returns a String representation of a Ship that lists
* its length, location, orientation, number of times hit,
* and if the ship is sunk. This will list all the info
* needed about the ship.
* @return
*/
public String toString() {
}
}
Square.java requirements:
The Square class represents a location in a BattleBoard's grid. A Square knows:
-if it has been hit by enemy fire
-the Ship located in its space (if there is a Ship)
This class will have private fields (instance variables) sufficient to keep track of the above information and it must have the following methods:
public Square(): Providing a constructor for this class is optional. Java will provide one if you choose not to, setting the instance variable keeping track of whether the square has been hit to false and setting the reference to a Ship to null.
public boolean hasBeenHit(): returns true if the Square has been hit by enemy fire.
public Ship getShip(): returns a Ship instance if there is a ship that includes this square or null if there is no such ship
public void fireAt(): the Square should update the fact that it has been hit and, if appropriate, let a ship occupying the square know that it has been hit.
public boolean hasShip(): returns true if the Square contains a Ship.
public void addShip(Ship ship): add the given Ship to the Square.
public String toString(): Returns a String representation of a Square as follows:
"-" (if the square does not contain a ship and has not been hit)
"W" (if the square has been hit and does not contain a ship)
"R" (if the square has been hit and contains a ship)
"1","2","3", or "4" (if the square has not been hit and contains a ship -- the number returned should correspond to the length of the ship)
These single-character representations allow a BattleBoard object to be printed in a compact form.
BattleBoard.java requirements:
The BattleBoard class is a model template for the human player's and computer's boards in BattleShip. A BattleBoard contains the following fields (instance variables):
-a 2 dimensional array of Squares
-an array of Ships
-the number of Ships on the board.
public BattleBoard(): This is the constructor of the BattleBoard class. The fields should be initialized. BattleGame provides three constants: BattleGame.ROWS, BattleGame.COLS, and BattleGame.NUMBER_OF_SHIPS. These constants should be used to initialize the BattleBoard's fields since the same constants are used to create the GUI.
public boolean addShip(int length, boolean isHorizontal, int startRow, int startCol): adds a Ship to the board. This means that all Squares the length of the ship starting at the startRow and startCol will be updated appropriately. The isHorizontal determines which direction to fill Squares. The method returns true if the Ship is successfully added to the BattleBoard and false if the Ship could not be added due to another Ship in the way, reaching a BattleBoard boundary, or if the array of Ships is full. If the Ship cannot be placed, then the squares should not change.
public int getNumberOfShips(): returns the number of Ships deployed on the BattleBoard.
public Ship[] getShips(): returns an array of the Ships deployed on the BattleBoard.
public boolean fireAtLocation(int row, int col): returns true if the enemy can successfully fire at the Square located at the specified row and column and actually fires at the location before returning. If the Square has already been fired at, the method should return false.
public boolean hasBeenHit(int row, int col): returns true if the enemy has already fired on the Square located at the specified row and column.
public boolean areAllShipsSunk(): returns true if all of the Ships on the BattleBoard have been sunk by enemy fire.
public int getNumberOfRows(): returns the number of rows in the BattleBoard.
public int getNumberOfColumns(): returns the number of columns in the BattleBoard.
public String toString(): returns a String representation of a BattleBoard. HINT: Using the toString() method of the Square class, you can easily create a String representation of the BattleBoard similar to the following, which will come in handy for testing and debugging:
4 4 R 4 - - - -
- - - - - - W -
- W - 2 2 - - -
- - - - - W - -
- W - - - - R -
- - - 1 - - 3 -
- - - - - - R -
- W - - - - - -
I tried starting this project, and I just don't know where to start. I know it's at Ship.java, but I'm not sure how to go about creating it.
Help is appreciated! Explanation is best because I want to understand it.
If there is a price, let me know as well.