Skip to content

Instantly share code, notes, and snippets.

@vic511
Created December 4, 2018 12:48
Show Gist options
  • Select an option

  • Save vic511/dc5a9fa0b6cf8948bae232764ee1ab5d to your computer and use it in GitHub Desktop.

Select an option

Save vic511/dc5a9fa0b6cf8948bae232764ee1ab5d to your computer and use it in GitHub Desktop.
class Labyrinth:
MSG_WALL = 'STOMP ! You hit a wall, you stay at your last position'
MSG_OK = 'OK - your new position is'
DIRECTIONS = {
'N' : (0, 1),
'S' : (0, -1),
'W' : (-1, 0),
'E' : (1, 0)
}
INVERTED = {
'N' : 'S',
'S' : 'N',
'W' : 'E',
'E' : 'W'
}
CHARS = {
True : ' ', # Empty cell
False : '#', # Wall
None : '?' # Unknown content
}
def goto(self, command):
new_pos = self.pos + self.DIRECTIONS[command]
# We move
p.sendline(command)
result = p.recvline()
self.grid[new_pos] = result.startswith(self.MSG_OK)
# If cell is empty, we can move
if self.grid[new_pos]:
self.pos += self.DIRECTIONS[command]
return self.grid[new_pos]
def discover(self):
# Simple backtracking
# self.grid is a wrapper of a 2D list
for command, direction in self.DIRECTIONS.items():
new_pos = self.pos + direction
if self.grid[new_pos] is None:
if self.goto(command):
# If we are able to move, discover the new cell
self.discover()
# Then, go back
self.goto(self.INVERTED[command])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment