Prequel
primitive recursive driven programming
(This example requires web workers.)
Find the key. Escape from the labirinth.
←0 ← moves (min moves: 0)
←0
←0
←0
← calls
← instructions
← memory (max use: 0)
IF MAZE.KEY_NEEDED >= 0 AND MAZE.HAS_KEY == 1MAZE.MOVE(MAZE.KEY_NEEDED)ENDIFoptions = 0n = MAZE.CAN_MOVE.LENGTHREPEAT nIF n <> MAZE.FROM AND n <> MAZE.KEY_NEEDED AND MAZE.CAN_MOVE[n] == 1options += 1ENDIFENDREPIF options == 0 # dead endto_cardinal = MAZE.FROMMAZE.MOVE(to_cardinal)ENDIFrandom_option ~= optionsto_cardinal = (MAZE.FROM+1)%4IF MAZE.CAN_MOVE[to_cardinal] == 1 AND to_cardinal <> MAZE.KEY_NEEDEDIF random_option == 0MAZE.MOVE(to_cardinal)ENDIFrandom_option--ENDIFto_cardinal = (MAZE.FROM+3)%4IF MAZE.CAN_MOVE[to_cardinal] == 1 AND to_cardinal <> MAZE.KEY_NEEDEDIF random_option == 0MAZE.MOVE(to_cardinal)ENDIFENDIFto_cardinal = (MAZE.FROM+2)%4MAZE.MOVE(to_cardinal)
MAZE.INDEX_NORTHMAZE.INDEX_EASTMAZE.INDEX_SOUTHMAZE.INDEX_WESTIndex of cardinal points.-> 0 north-index-> 1 east-index-> 2 south-index-> 3 west-indexConstants.MAZE.CELL_CURRENTCurrent position in 2D coordinates.-> e.g. [0, 0]MAZE.FROMCardinal index of previous position.-> -1 if it is still at the starting position-> MAZE.INDEX_NORTH if it has arrived from north-> MAZE.INDEX_EAST if it has arrived from east-> MAZE.INDEX_SOUTH if it has arrived from south-> MAZE.INDEX_WEST if it has arrived from westMAZE.CAN_MOVE[cardinal_index]Check whether movement is possiblefrom the current positionto each cardinal_index.-> 1 if it is-> 0 if it is notMAZE.HAS_KEYCheck whether key is being carried.-> 1 if it is-> 0 if it is notMAZE.KEY_NEEDEDCheck whether the key is needed to make the next move.-> -1 if the key is not needed-> MAZE.INDEX_NORTH if the key is needed to move north-> MAZE.INDEX_EAST if the key is needed to move east-> MAZE.INDEX_SOUTH if the key is needed to move south-> MAZE.INDEX_WEST if the key is needed to move westMAZE.CELL[cardinal_index]Adjacent 2D coordinatesfrom the current positionto cardinal_index.-> e.g. [1, 0]MAZE.CELLS_VISITEDList of cells already visited.-> e.g. [[0, 0], [1, 0]]CALL.ARGSHolds the list of arguments passed with CALL.-> [arg1, ..., argN]
MAZE.MOVE(cardinal_index)Try to move to MAZE.CELL[cardinal_index].Restart program counter.Note that it increments the movement counter even if it cannot move.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.ARGSAlternative 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