Prequel
primitive recursive driven programming
Find the key. Escape from the labirinth.
if maze.key_needed >= 0 and maze.has_key == 1maze.move(maze.key_needed)endifoptions = 0n = maze.can_move.length()repeat 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 ~= optionsn = maze.can_move.length()repeat nto_cardinal = (maze.from+maze.can_move.length()-n) % maze.can_move.length()if maze.can_move[to_cardinal] == 1 and to_cardinal <> maze.key_neededif random_option == 0maze.move(to_cardinal)endifrandom_option--endifendrep
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]]
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.