Dict vs %

In the previous version of left-hand maze traversal the heavy lifting was done by %. It guarantees that when we turn left or right, our direction (number 0,1,2,3 stored in dc) stays in the 0-3 range. One can use a dict to straightly map a current direction to the next after CV or CCV turn.

Let's compare:

old

dc = 0
while get_entity_type() != Entities.Treasure:
  dc = (dc + 1) % 4
  while not can_move(d[dc]):
    dc = (dc - 1) % 4
  move(d[dc])

new

dc = East
while get_entity_type() != Entities.Treasure:
  dc = l[dc]
  while not move(dc):
    dc = r[dc]

One more trick - move does nothing (and return False) when the wall is in front of us. So we can combine can_move and move.

Of course, it works because we introduced dictionaries:

l = {East:North, North:West, West:South, South:East}
r = {East:South, South:West, West:North, North:East}

code