maze_nav.py
'''Maze Navigator.
Tools for moving around the maze. Also a base class for generator and solver tools.
Coder@Sonnack.com
September 16, 2014
'''
from sys import stdout, argv
from maze_obj import MazeObject
class MazeNavigator (object):
'''\
Maze Navigator class.
properties:
mz - Maze Object
curr_row - Current Location Row
curr_col - Current Location Column
direction - Current Location Direction [0-3]
methods:
left_direction ()
right_direction ()
turn_left ()
turn_right ()
move_to_next_cell ()
move_to_north_cell ()
move_to_south_cell ()
move_to_west_cell ()
move_to_east_cell ()
set_curr_cell (val)
get_curr_cell ()
get_next_cell ()
get_left_cell ()
get_right_cell ()
get_north_cell ()
get_south_cell ()
get_west_cell ()
get_east_cell ()
get_next_wall ()
get_left_wall ()
get_right_wall ()
get_north_wall ()
get_south_wall ()
get_west_wall ()
get_east_wall ()
set_next_wall (val)
set_north_wall (val)
set_south_wall (val)
set_west_wall (val)
set_east_wall (val)
'''
Abbrev = ['N', 'E', 'S', 'W']
North = 0
East = 1
South = 2
West = 3
def __init__ (self, mz, _row=0, _col=0, _direction=East):
self.mz = mz
self.curr_row = _row
self.curr_col = _col
self.direction = _direction
def set_curr_cell (self, val):
self.mz.set_cell(self.curr_row,self.curr_col, val)
def get_curr_cell (self):
return self.mz.cell(self.curr_row,self.curr_col)
def reset_curr_cell (self):
self.curr_row = self.mz.entry_cell[0]
self.curr_col = self.mz.entry_cell[1]
self.direction = MazeNavigator.East
def left_direction (self):
return (self.direction - 1) % 4
def right_direction (self):
return (self.direction + 1) % 4
def turn_left (self):
self.direction = self.left_direction()
return self.direction
def turn_right (self):
self.direction = self.right_direction()
return self.direction
def move_to_north_cell (self):
self.curr_row = self.curr_row - 1
if self.curr_row < 0:
raise Exception('Moved beyond the North Wall!')
def move_to_south_cell (self):
self.curr_row = self.curr_row + 1
if self.mz.dims[0] <= self.curr_row:
raise Exception('Moved beyond the South Wall!')
def move_to_west_cell (self):
self.curr_col = self.curr_col - 1
if self.curr_col < 0:
raise Exception('Moved beyond the West Wall!')
def move_to_east_cell (self):
self.curr_col = self.curr_col + 1
if self.mz.dims[1] <= self.curr_col:
raise Exception('Moved beyond the East Wall!')
move_to_cell = [move_to_north_cell, move_to_east_cell, move_to_south_cell, move_to_west_cell]
def move_to_next_cell (self):
return self.move_to_cell[self.direction](self)
def get_north_cell (self):
if self.curr_row == 0:
return -1
return self.mz.cell(self.curr_row-1, self.curr_col)
def get_south_cell (self):
if self.curr_row == (self.mz.dims[0] - 1):
return -1
return self.mz.cell(self.curr_row+1, self.curr_col)
def get_west_cell (self):
if self.curr_col == 0:
return -1
return self.mz.cell(self.curr_row, self.curr_col-1)
def get_east_cell (self):
if self.curr_col == (self.mz.dims[1] - 1):
return -1
return self.mz.cell(self.curr_row, self.curr_col+1)
get_cell = [get_north_cell, get_east_cell, get_south_cell, get_west_cell]
def get_next_cell (self):
return self.get_cell[self.direction](self)
def get_left_cell (self):
d = self.left_direction()
return self.get_cell[d](self)
def get_right_cell (self):
d = self.right_direction()
return self.get_cell[d](self)
def get_north_wall (self):
return self.mz.wall_n(self.curr_row, self.curr_col)
def get_south_wall (self):
return self.mz.wall_s(self.curr_row, self.curr_col)
def get_west_wall (self):
return self.mz.wall_w(self.curr_row, self.curr_col)
def get_east_wall (self):
return self.mz.wall_e(self.curr_row, self.curr_col)
get_wall = [get_north_wall, get_east_wall, get_south_wall, get_west_wall]
def get_next_wall (self):
return self.get_wall[self.direction](self)
def get_left_wall (self):
d = self.left_direction()
return self.get_wall[d](self)
def get_right_wall (self):
d = self.right_direction()
return self.get_wall[d](self)
def set_north_wall (self, val):
self.mz.set_wall_n(self.curr_row, self.curr_col, val)
def set_south_wall (self, val):
self.mz.set_wall_s(self.curr_row, self.curr_col, val)
def set_west_wall (self, val):
self.mz.set_wall_w(self.curr_row, self.curr_col, val)
def set_east_wall (self, val):
self.mz.set_wall_e(self.curr_row, self.curr_col, val)
set_wall = [set_north_wall, set_east_wall, set_south_wall, set_west_wall]
def set_next_wall (self, val):
return self.set_wall[self.direction](self, val)
if __name__ == '__main__':
print('autorun: %s' % argv[0])
mz = MazeObject(8, 10)
mz.set_all_walls(MazeObject.MazeWall)
mz.set_entry_and_exit()
nav = MazeNavigator(mz)
nav.reset_curr_cell()
for steps in range(4):
nav.set_next_wall(MazeObject.NoWall)
nav.move_to_next_cell()
nav.turn_right()
for steps in range(3):
nav.set_next_wall(MazeObject.NoWall)
nav.move_to_next_cell()
mz.print_maze(stdout)
'''eof'''