Skip to content

Battleship

Rules

  • Join with lobby.subscribe and {"game": "battleship"}.
  • 2 players.
  • Board: 10 by 10.
  • Ships: Carrier (5), Battleship (4), Cruiser (3), Submarine (3), Destroyer (2).
  • Ships can't touch, not even corner to corner.
  • First you place your ships. Then you shoot.
  • Hit? Shoot again. Miss? The other player's turn.
  • The player who joined first shoots first.
  • A shot is one number, 0 to 99. 42 = row 4, column 2.
  • Result: WIN or LOSS. No draws.
  • The server does not send boards. You know your own ships. Track your shots from game.action events.

Step by step

Steps 1 to 4 are saying hello and logging in. For <id1>, <m> and the other placeholders, put in the real value.

# Who Line
1 you REQ <id1> session.hello {"version": 1}
2 server RES <id1> OK
3 you REQ <id2> session.login {"player": "<name>"}
4 server RES <id2> OK {"session_token": "<session_token>"} (save the session token to reconnect)
5 you REQ <id3> lobby.subscribe {"game": "battleship"}
6 server RES <id3> OK
7 server EVT <m> game.match_started {"opponent": "<other>"}
8 server EVT <m> game.update {"type": "place_ships", "ships": [{"ship": "Carrier", "length": 5}, ...], "size": 10}
9 you REQ <id4> <m> game.action {"type": "place_fleet", "ships": [{"ship": "Carrier", "row": <row>, "col": <col>, "orientation": "H"}, ...]}
10 server RES <id4> OK
11 server EVT <m> game.update {"type": "opponent_ready"} (only if you are still placing)
12 server EVT <m> game.your_turn (when both players are done placing)
13 you REQ <id5> <m> game.action {"type": "move", "move": <move>}
14 server RES <id5> OK
15 server EVT <m> game.action {"type": "move", "player": "<name>", "move": <move>, "result": "HIT", "sunk": null}
16 server EVT <m> game.your_turn (you again after a HIT, the other player after a MISS)
repeat 13-16 until all ships of one player are sunk
17 server EVT <m> game.result {"result": "WIN", "reason": "normal"} (match results)

Actions

Send these with game.action. Put the type in the data.

type You send Everyone gets EVT game.action with
place_fleet ships (see Placing ships) nothing: your ships are secret, so this action is not sent to the other player
move move: the square you shoot, 0 to 99 player, move, result, sunk (see Shot results)

The server sends these with game.update:

type Who gets it Data
place_ships each player, when the match starts ships: each ship's name and length. size: the board's width and height.
opponent_ready the player still placing, when the other player is done -

Placing ships

  • Send all 5 ships in one place_fleet action.
  • For each ship: ship, row, col, orientation.
  • <row> and <col>: where the ship starts, 0 to 9.
  • orientation: "H" (horizontal) or "V" (vertical).

Shot results

  • result: HIT or MISS
  • sunk: the ship's name if this shot sank it, or null

Errors

What each code means: Errors.

  • UNKNOWN_ACTION: no type, or a type that is not in Actions
  • INVALID_PLACEMENT: wrong ships, off the board, on top of each other, or touching. Also when you send place_fleet a second time.
  • INVALID_MOVE: not 0-99, or you already shot there
  • NOT_YOUR_TURN: also when you send move while ships are still being placed