Class Ship

java.lang.Object
  extended byShip

public class Ship
extends Object

Details of a ship and its status on the board


Field Summary
private  Location[] body
          Location of the body parts : an array 0..length-1
private  int[] hit
          Indices of the body parts destroyed
private  int hitNumber
          Number of body parts destroyed
private  int length
          Number of holes this ship occupies
private  String name
          Name of the ship
 
Constructor Summary
(package private) Ship(String n, int l, Location end, int dir)
          Creates a ship, and places it in a specified place.
 
Method Summary
 String detail()
           
 boolean guess(Location l)
          Carries out opponent's guess.
private  void hit(int i)
          Marks body part i, as hit
 boolean isClash(Ship s)
          Checks if two ships are in the same hole
 boolean isSunk()
           
 int length()
           
 Location locPart(int i)
          Get Location of specified body part
 int numHit()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

name

private String name
Name of the ship


length

private int length
Number of holes this ship occupies


body

private Location[] body
Location of the body parts : an array 0..length-1


hit

private int[] hit
Indices of the body parts destroyed


hitNumber

private int hitNumber
Number of body parts destroyed

Constructor Detail

Ship

Ship(String n,
     int l,
     Location end,
     int dir)
Creates a ship, and places it in a specified place.

Parameters:
n - Ship's name
l - Ship's length
end - Location of Ship's top, left end point
dir - Direction to place the Ship (1=vertical, 0=horizontal)
Method Detail

length

public int length()
Returns:
Length of this ship

locPart

public Location locPart(int i)
Get Location of specified body part

Parameters:
i - index of body part required
Returns:
Location of body part i

detail

public String detail()
Returns:
name and length of Ship, as a string

isSunk

public boolean isSunk()
Returns:
True if all body parts are hit

numHit

public int numHit()
Returns:
number of body parts hit

hit

private void hit(int i)
Marks body part i, as hit

Parameters:
i - number of body part to be destroyed

guess

public boolean guess(Location l)
Carries out opponent's guess.

Parameters:
l - Location guessed
Returns:
true if this ship occupies location l

isClash

public boolean isClash(Ship s)
Checks if two ships are in the same hole

Parameters:
s - Ship to compare with
Returns:
true if this ship and s have a common hole