battleship

Safe HaskellSafe

Player

Description

This untrusted module implements the server side of a player, which communicates with its client side via TCP. When the server prompts the client for input, it uses "resign" as the quit message (see Command).

Synopsis

Documentation

data PlacingHandle

Placing handle for player -- handle for player during the placing phase of game.

data ShootingHandle

Shooting handle for player -- handle for player during the shooting phase of game.

placing :: Handle -> Bool -> DC PlacingHandle

placing clnt goFirst returns a DC action that, when run with current label dcPublic, starts up a new player, returning its placing handle, and leaving the current label and clearance unchanged. The player will use clnt (which must have label dcPublic) to communicate with its client side, using the protocol of Command, and will go (shoot) first iff goFirst.

Note: all DC actions derived from the placing handle via the following functions must be run with current label dcPublic. They all leave the current label and clearance unchanged. In particular, this applies to the DC actions returned by the reply functions produced by the DC actions returned by the shoot function -- see below.

getComplete :: PlacingHandle -> DC (Maybe (Complete, ShootingHandle))

getComplete ph returns a DC action that, when run, waits for the player corresponding to the placing handle ph to finish placing its ships. If the player resigns or terminates before fully placing its ships, then the DC action returns Nothing; otherwise, the DC action returns Just(compl, sh), where compl is the complete placing chosen by the player, and sh is the shooting handle for the player. The DC action returned by calling getComplete on a given placing handle may only be run once.

data ShotResult

Shot results communicated to shooting player.

Constructors

Repeat

An attempt to shoot a position already shot.

Miss

Miss -- no ship hit.

Hit

Hit an unspecified ship.

Sank Ship

Sank a specified ship -- last of its cells hit.

shoot :: ShootingHandle -> DC (Maybe (Pos, ShotResult -> DC ()))

shoot sh returns a DC action that, when run, waits for the player with shooting handle sh to supply a value of type Maybe(Pos, ShotResult -> DC()), and then returns this value.

  • If Nothing is supplied, this means the player is resigning.
  • If Just(pos, shotFun) is returned, this means the player has chosen to shoot cell pos of the opponent's board. The caller of shoot is then responsible for calling shotFun with the value of type ShotResult describing the outcome of this shot, and for then running the DC action returned by shotFun, thus communicating this outcome back to the player. (This must be done before any other DC actions associated with sh are run.)

opponentShot :: ShootingHandle -> Pos -> DC ()

opponentShot sh pos returns a DC action that, when run, tells the player with shooting handle sh that its opponent has shot cell pos of the player's board.

won :: ShootingHandle -> DC ()

won sh returns a DC action that, when run, tells the player with shooting handle sh that it has won the game. Once this DC action has been run, no DC actions derived from shooting handle sh may be made.

lost :: ShootingHandle -> DC ()

lost sh returns a DC action that, when run, tells the player with shooting handle sh that it has lost the game. Once this DC action has been run, no DC actions derived from shooting handle sh may be made.