Prequel
primitive recursive driven programming


Maze

Find the key. Escape from the labirinth.

0 ← moves (min moves: 0)
Shuffle Show
Run Pause Stop
Runtime Error: DIVISION BY ZERO
0
0
0
← calls
← instructions
← memory (max use: 0)

Prequel program

Edit Save Load Debug
Parser Error: UNEXPECTED CHAR

Module: random_walk

if maze.key_needed >= 0 and maze.has_key == 1
maze.move(maze.key_needed)
endif
options = 0
n = maze.can_move.length()
repeat n
if n <> maze.from and n <> maze.key_needed and maze.can_move[n] == 1
options += 1
endif
endrep
if options == 0 # dead end
to_cardinal = maze.from
maze.move(to_cardinal)
endif
random_option ~= options
n = maze.can_move.length()
repeat n
to_cardinal = (maze.from+maze.can_move.length()-n) % maze.can_move.length()
if maze.can_move[to_cardinal] == 1 and to_cardinal <> maze.key_needed
if random_option == 0
maze.move(to_cardinal)
endif
random_option--
endif
endrep

Input:

maze.index_north
maze.index_east
maze.index_south
maze.index_west
 Index of cardinal points.
 -> 0 north-index
 -> 1 east-index
 -> 2 south-index
 -> 3 west-index
 Constants.
maze.cell_current
 Current position in 2D coordinates.
 -> e.g. [0, 0]
maze.from
 Cardinal 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 west
maze.can_move[cardinal_index]
 Check whether movement is possible
  from the current position
  to each cardinal_index.
 -> 1 if it is
 -> 0 if it is not
maze.has_key
 Check whether key is being carried.
 -> 1 if it is
 -> 0 if it is not
maze.key_needed
 Check 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 west
maze.cell[cardinal_index]
 Adjacent 2D coordinates
  from the current position
  to cardinal_index.
 -> e.g. [1, 0]
maze.cells_visited
 List of cells already visited.
 -> e.g. [[0, 0], [1, 0]]

Functions:

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.