Battleship
The Battleship Service provides helpful utilities for building a distributed game of battleship.
Overview
Like regular Battleship, the Battleship service has two states: placing ships and shooting at ships. During placement, it expects each role to place each ship on his/her board and will not allow the game to proceed to the shooting phase until each role has placed all his/her ships. Placement, firing and starting blocks will return true if successful or an error message if it fails.
Blocks
place <ship> at <row> <column> facing <direction>
- Places a ship on your board with the front at the given row and column facing the given direction. Returnstrue
if placed successfully (eg, on the board and not overlapping another ship). Also, placing a ship twice results in a move (not duplicates).start game
- Try to start the game. If both users have all their ships placed, it should return true and sendstart
messages to all roles. Otherwise, it will return with a message saying that it is waiting on a specific role.fire at <row> <column>
- This block allows the user to try to fire at the given row and column. It returnstrue
if it was a valid move; otherwise it will return an error message likeit's not your turn!
. On a successful move, the server will send either ahit
ormiss
message to everyone in the room. Then it will send ayour turn
message to the player to play next.active ships for <role>
- This block returns a list of all ships that are still afloat for the given role. If no role is specified, it defaults to the sender’s role.all ships
- Returns a list of all ship names. Useful in programmatically placing ships.ship length <ship>
- Returns the length of the given ship.restart game
- Restarts the given game (all boards, etc)
Message Types
start
- Received whenstart game
finishes successfully for any role. After game has officially started, users can no longer move ships.your turn
- Received when the given role’s turn starts.hit
-role
is the owner of the ship that has been hit.ship
is the name of the ship that has been hit, androw
andcolumn
provide the location on the board where it was hit.sunk
provides a true/false value for if the ship was sunk.miss
-role
is the owner of the board receiving the shot androw
andcolumn
correspond to the board location or the shot.
RPCS
- Battleship.allShips()
Get list of ship types
- Battleship.fire(row: BoundedNumber<1, 100>, column: BoundedNumber<1, 100>)
Fire a shot at the board
Arguments:
row: BoundedNumber<1, 100>
(BoundedNumber) - Row to fire atcolumn: BoundedNumber<1, 100>
(BoundedNumber) - Column to fire at
Returns:
Boolean
(Boolean) - If ship was hit
- Battleship.placeShip(ship: String, row: BoundedNumber<1, 100>, column: BoundedNumber<1, 100>, facing: String)
Place a ship on the board
Arguments:
ship: String
(String) - Ship type to placerow: BoundedNumber<1, 100>
(BoundedNumber) - Row to place ship incolumn: BoundedNumber<1, 100>
(BoundedNumber) - Column to place ship infacing: String
(String) - Direction to face
Returns:
Boolean
(Boolean) - If piece was placed
- Battleship.remainingShips(roleID: String)
Get number of remaining ships of a role
Arguments:
roleID: String
(String) - Name of role to use
Returns:
Integer
(Integer) - Number of remaining ships
- Battleship.reset()
Resets the game by clearing the board and reverting to the placing phase
Returns:
Boolean
(Boolean) - If game was reset