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 stdoutargv
from maze_obj import MazeObject
####################################################################################################


##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
## MAZE NAVIGATOR
##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
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__ (selfmz_row=0_col=0_direction=East):
        self.mz = mz
        self.curr_row = _row
        self.curr_col = _col
        self.direction = _direction

    #----------------
    # Current Cell...
    #----------------
    def set_curr_cell (selfval):
        self.mz.set_cell(self.curr_row,self.curr_colval)

    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

    #-------------
    # Direction...
    #-------------

    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

    #---------------------
    # Move to Next Cell...
    #---------------------

    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_cellmove_to_east_cellmove_to_south_cellmove_to_west_cell]

    def move_to_next_cell (self):
        return self.move_to_cell[self.direction](self)

    # --------------------
    # Get Neighbor Cell...
    # --------------------

    def get_north_cell (self):
        # special north edge test...
        if self.curr_row == 0:
            return -1
        return self.mz.cell(self.curr_row-1self.curr_col)

    def get_south_cell (self):
        # special south edge test...
        if self.curr_row == (self.mz.dims[0] - 1):
            return -1
        return self.mz.cell(self.curr_row+1self.curr_col)

    def get_west_cell (self):
        # special west edge test...
        if self.curr_col == 0:
            return -1
        return self.mz.cell(self.curr_rowself.curr_col-1)

    def get_east_cell (self):
        # special east edge test...
        if self.curr_col == (self.mz.dims[1] - 1):
            return -1
        return self.mz.cell(self.curr_rowself.curr_col+1)

    get_cell = [get_north_cellget_east_cellget_south_cellget_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)

    # -----------
    # Get Wall...
    # -----------

    def get_north_wall (self):
        return self.mz.wall_n(self.curr_rowself.curr_col)

    def get_south_wall (self):
        return self.mz.wall_s(self.curr_rowself.curr_col)

    def get_west_wall (self):
        return self.mz.wall_w(self.curr_rowself.curr_col)

    def get_east_wall (self):
        return self.mz.wall_e(self.curr_rowself.curr_col)

    get_wall = [get_north_wallget_east_wallget_south_wallget_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)

    # -----------
    # Set Wall...
    # -----------

    def set_north_wall (selfval):
        self.mz.set_wall_n(self.curr_rowself.curr_colval)

    def set_south_wall (selfval):
        self.mz.set_wall_s(self.curr_rowself.curr_colval)

    def set_west_wall (selfval):
        self.mz.set_wall_w(self.curr_rowself.curr_colval)

    def set_east_wall (selfval):
        self.mz.set_wall_e(self.curr_rowself.curr_colval)

    set_wall = [set_north_wallset_east_wallset_south_wallset_west_wall]

    def set_next_wall (selfval):
        return self.set_wall[self.direction](selfval)




####################################################################################################
if __name__ == '__main__':
    print('autorun: %s' % argv[0])

    mz = MazeObject(810)
    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'''