battleship

Battleship

Getting Started

The entire setup guide is here.

API

These are the functions you can use when developing your own ships.

Table of contents

Ship
Arena
Coordinate

Ship Class

Initial Ship Properties

You have a total of 10 points to distribute amongst the four properties. These methods must be called in the constructor of the ship class.

initializeName(String name)

initializeOwner(String owner)

initializeHull(int hull)

initializeFirepower(int firepower)

initializeSpeed(int speed)

initializeRange(int range)

public class SampleShip extends Ship {
    // constructor method are called once when a new object is instantiated(created)
    public SampleShip() {
        this.initializeName("Boaty McBoatFace");
        this.initializeOwner("Nick");
        // 10 points to distribute among the methods below
        this.initializeHull(2);
        this.initializeFirepower(3);
        this.initializeSpeed(2);
        this.initializeRange(3);
    }
}

Ship Actions

Protected Methods

These methods can only be called by the ship they belong to.

doTurn(Arena arena);

The code you put this method will be run each turn to instruct ship behavior. Use data from the battlefield given by the Arena object to perform different actions.

@override
protected void doTurn(Arena arena) {
    // instruction for ship behavior here
}

move(Arena arena, Direction direction)

Move ship in a give direction if there are remaining moves available, the spot is not taken by another ship or considered outside of the game boundary.

@override
protected void doTurn(Arena arena) {
    // instruction for ship behavior here
    int numMoves = this.getRemainingMoves();

    while(numMoves > 0) {
        // move north, west until there's only one turn left
        this.move(arena, Direction.NORTH);
        this.move(arena, Direction.WEST);
        // update the number of turn remaining
        numMoves = this.getRemainingMoves();
    }
    this.move(arena, Direction.EAST);
}

fire(Arena arena, int x, int y)

Fire at given coordinates if there are remaining shots available.

@override
protected void doTurn(Arena arena) {
    // instruction for ship behavior here
    this.fire(arena, 1, 3);

    // get a list of nearby ships
    List<Ship> targets = this.getNearbyShips(arena);
    // access the list if there are any nearby ships
    if (targets.size() > 0) {
        // get the first ship in the targets list
        Ship ship = targets.get(0);
        Coord location = ship.getCoord();
        int x = location.getX();
        int y = location.getY();
        this.fire(arena, x, y);
    }
}

Public Methods

These methods can be called on any ship.

[Deprecated] getShipCoord(Arena arena, Ship ship)

Get the coordinates of another ship if it is within the range of your ship.

getNearbyShips(Arena arena)

Returns a list of ships (exclusing your own) that are within the range of your ship.

Q: “What do the angle brackets mean?”

A: These are called “generics.” You can use Lists to store any kind of object. So the way to read List<Ship> out loud is: “a List of Ship objects.”

@override
protected void doTurn(Arena arena) {
    // instruction for ship behavior here
    this.fire(arena, 1, 3);

    // get a list of nearby ships
    List<Ship> targets = this.getNearbyShips(arena);

    // loop over all nearby ships
    for (int index = 0; index < targets.size(); index += 1) {
        Ship shipInfo = targets.get(index);
        System.out.println("One nearby ship has " + shipInfo.getHealth() + " HP left.");
    }

    System.out.println("There are " + targets.size() + " number of nearby ships");

    // if there is atleast one nearby ships
    if (targets.size() > 0) {
        // get the first ship in the list
        Ship first = targets.get(0);
        // get the fourth ship in the list
        Ship fourth = targets.get(0);
    }
}

Methods that can be used on any ship

getCoord()

Get the coordinate of your ship, check out the Coord class for more information.

Example of getting x and y from Coord object

Coord coord = this.getCoord():
int x = coord.getX();
int y = coord.getY();
System.out.println("My ship is at (" + x + ", " + y + ").");

isSameTeamAs(Ship other)

Check if another ship is on the same team as the ship calling this method.

Example of checking if a ship is on your team:

List<Ship> nearby = this.getNearbyShips(arena);
for (int i = 0; i < nearby.size(); i++) {
    Ship other = nearby.get(i);
    boolean isOnMyTeam = this.isSameTeamAs(other);
    if (isOnMyTeam) {
        System.out.println("This ship is on my team!");
    } else {
        System.out.println("This ship is an enemy.");
    }
}

Example of checking if two different ships are on the same team as each other:

Ship first = nearby.get(0);
Ship second = nearby.get(1);
if (first.isSameTeamAs(second)) {
    System.out.println("These two ships are conspiring with each other.");
} else {
    System.out.println("These two ships are not friends with each other.");
}

getRemainingMoves()

Get the number of moves a ship has left on this turn.

getRemainingShots()

Get the number of shots a ship has left on this turn.

isSunk()

Get whether or not the ship has sunk

getName()

Get the name of a ship.

getOwner()

Get the name of the owner of a ship.

getHealth()

Get the number of hits a ship has remaining before it sinks.

getHull()

Get the hull strength of a ship.

getFirepower()

Get the firepower of a ship.

getSpeed()

Get the speed of a ship.

getRange()

Get the range of a ship.

Example Usage:

List<Ship> list = this.getNearbyShips(arena);
Ship enemy = list.get(0);
System.out.println("Enemy Ship: " + enemy.getName());
System.out.println("Firepower: " + enemy.getFirepower());

if (enemy.getSpeed() > this.getSpeed()) {
    System.out.println("The enemy ship will move before my ship.");
} else if (enemy.getSpeed() > this.getSpeed()) {
    System.out.println("The enemy ship will move after my ship.");
} else {
    System.out.println("The enemy ship might move before or after my ship.");
}

int hitsTaken = enemy.getHull() - enemy.getHealth();
System.out.println("Enemy has taken " + hitsTaken + " hits.");

Arena Class

getAllShips()

Returns a list of ships in the arena that have not yet sunk.

getAllSpawnedShips()

Returns a list of ships, whether or not they have sunk.

isInRange(Ship a, Ship b)

Determines if ship b is within the range of ship a.

getXSize()

Get horizontal size of arena.

getYSize()

Get vertical size of arena.

Example of map size usage

Coord myShipLocation = this.getCoord();
// if current x location plus one is equal to the size of the map
// then we have reached the EAST edge of the map
if (myShipLocation.getX() + 1 == arena.getXSize()) {
    this.move(arena, Direction.WEST);
}
// similar example but uses y location and a variable to store the values
int shipYLocation = myShipLocation.getY();
// if current y location minus one is equal to zero
// then we have reached the NORTH edge of the map
if (shipYLocation - 1 == 0) {
    this.move(arena, Direction.SOUTH);
}

getTurn()

Get the number of this turn.

countLiveShips()

Get the number of ships that are currently alive in the arena.

getRandom()

Get the random object for the arena. Do not create your own random objects, use this object for any randomness.

Examples of common random methods:

// Get a random integer between 0 (inclusive) and 4 (exclusive)
int i = arena.getRandom().nextInt(4);
// Get a random double between 0.0 and 1
double d = arena.getRandom().nextDouble();
// Get a random object from a list
List<Ship> list = this.getNearbyShips(arena);
int index = arena.getRandom().nextInt(list.size());
Ship r = list.get(index);

Coord Class

public int getX()

gets the x location of a coordinate object

public int getY()

gets the y location of a coordinate object

Full Example of a simple ship

// doTurn Example, place in your ship class
@Override
protected void doTurn(Arena arena) {
    // your implementation of a ship

    // gets the current location of the ship
    Coord coord = this.getCoord();
    int x = coord.getX();
    int y = coord.getY();

    if (x > 8) {
        this.move(arena, Direction.WEST);
    }
    else if (x < 2) {
        this.move(arena, Direction.EAST);
    }
    else if (y > 8) {
        this.move(arena, Direction.NORTH);
    }
    else if (y < 2) {
        this.move(arena, Direction.SOUTH);
    }
    else {
        // make a list of all the location, and store it in a variable
        Direction[] possibleMovement = {Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.EAST};

        // get a random number and store it in a variable
        int randomNumber = arena.getRandom().nextInt(4)

        // get a random movement by using the random number to access one possibleMovement
        this.move(arena, possibleMovement[randomNumber]);
    }

    // ship using this instruction will fire at location (x: 0, y: 0) each turn
    this.fire(arena, 0, 0);
}