Prequel
primitive recursive driven programming


Battleship

(This example requires web workers.)

Setup the fleet and play!

0
0
0
← shots fired
← remaining hits
← hidden cells

Play Setup Hide Show Shuffle

Run Pause Stop

0
0
0
← calls
← instructions
← memory (max use: 0)


Prequel program

Edit Save Debug

Module: BATTLESHIP

randomIndex ~= BATTLESHIP.HIDDEN.LENGTH
row = BATTLESHIP.HIDDEN[randomIndex][0]
col = BATTLESHIP.HIDDEN[randomIndex][1]
BATTLESHIP.SHOOT(row, col)

Input:

BATTLESHIP.GRID
 List of rows where each row is a list containing the BATTLESHIP.GRID.ID of each column.
 -> e.g. [[5, 5, 5, 0, -2, -1, 1, -1], ...]
BATTLESHIP.GRID.ID.HIDDEN
BATTLESHIP.GRID.ID.WATER.DEDUCED
BATTLESHIP.GRID.ID.WATER.FOUND
BATTLESHIP.GRID.ID.SHIP1
BATTLESHIP.GRID.ID.SHIP2
BATTLESHIP.GRID.ID.SHIP3
BATTLESHIP.GRID.ID.SHIP4
BATTLESHIP.GRID.ID.SHIP5
 Possible integer values in BATTLESHIP.GRID.
 -> -2 grid cell is hidden
 -> -1 grid cell has water and is adjacent to a sunken ship
 ->  0 grid cell has water and has been shot at
 ->  1 grid cell has ship of length 1
 ->  2 grid cell has ship of length 2
 ->  3 grid cell has ship of length 3
 ->  4 grid cell has ship of length 4
 ->  5 grid cell has ship of length 5
BATTLESHIP.HIDDEN
 List of all cell coordinates that are hidden.
 -> e.g. [[0, 4], [1, 0], ...]
BATTLESHIP.WATER.DEDUCED
BATTLESHIP.WATER.FOUND
BATTLESHIP.WATER
 List of all cell coordinates that are known to have water.
 -> e.g. [[0, 3], [0, 5], [0, 7], ...]
BATTLESHIP.SHIP1
BATTLESHIP.SHIP2
BATTLESHIP.SHIP3
BATTLESHIP.SHIP4
BATTLESHIP.SHIP5
 List of all cell coordinates that are known to have a ship of the corresponding length.
 -> e.g. [[0, 0], [0, 1], [0, 2], ...]
CALL.ARGS
 Holds the list of arguments passed with CALL.
 -> [arg1, ..., argN]

Procedures:

BATTLESHIP.SHOOT(rowIndex, colIndex)
 Shoot grid cell at coordinates (rowIndex, colIndex).
 -> rowIndex: integer between zero and number of rows minus one
 -> colIndex: integer between zero and number of columns minus one
 Restart program counter.
CALL(&retvar1, ..., &retvarM, @modname, arg1, ..., argN)
 Start executing module modname, copying N arguments and expecting M return values.
 -> retvar: name of variable to hold the corresponding return value retval
 -> modname: name of module to start executing
 -> arg: value to be copied to the list CALL.ARGS
 Alternative syntax: retvar1, ..., retvarM = CALL(@modname, arg1, ..., argN)
 Note that recursive module calls are not allowed.
RETURN(retval1, ..., retvalM)
 Return M values from the current module.
 Resume execution at the next instruction of the corresponding CALL.
 -> retval: value to be copied to the corresponding variable retvar

Additional examples can be found on GitHub.