#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Question Help with rat race game


    How would I make a function that:
    Moves the rat unless there is a wall
    If there is a brussels sprout eat it and:
    Increase the rat's num_sprouts_eaten variable by one
    Decrease the maze's num_sprouts_left variable by one
    Change the SPROUT to a HALL
    Thanks for helping! Here's the code:

    Code:
    # Constants for the contents of the maze.
    
    # The visual representation of a wall.
    WALL = '#'
    
    # The visual representation of a hallway.
    HALL = '.'
    
    # The visual representation of a brussels sprout.
    SPROUT = '@'
    
    # Constants for the directions. Use these to make Rats move.
    
    # The left direction.
    LEFT = -1
    
    # The right direction.
    RIGHT = 1
    
    # No change in direction.
    NO_CHANGE = 0
    
    # The up direction.
    UP = -1
    
    # The down direction.
    DOWN = 1
    
    # The letters for rat_1 and rat_2 in the maze.
    RAT_1_CHAR = 'J'
    RAT_2_CHAR = 'P'
    
    
    class Rat:
        """ A rat caught in a maze. """
    
        def __init__(self, symbol, row, col):
            ''' (Rat, str, int, int) -> NoneType
    
            A rat with symbol, row, column, and number of Brussel Sprouts eaten.
            '''
    
            self.symbol = symbol
            self.row = row
            self.col = col
            self.num_sprouts_eaten = 0
    
        def set_location(self, row, col):
            ''' (Rat, int, int) -> NoneType
    
            Set the rat's row and col instance variables to the given row and column
    
            >>> rat = Rat('k', 3, 1)
            >>> Rat.set_location(rat, 2, 4)
            >>> rat.row
            2
            >>> rat.col
            4
            '''
    
            self.row = row
            self.col = col
            
        def eat_sprout(self):
            ''' (Rat) -> NoneType
    
            Add one to the rats instance variable num_sprouts_eaten.
    
            >>> r = Rat('k', 4, 5)
            >>> r.num_sprouts_eaten
            0
            >>> Rat.eat_sprout(r)
            >>> r.num_sprouts_eaten
            1
            '''
    
            self.num_sprouts_eaten = self.num_sprouts_eaten + 1
    
        def __str__(self):
            ''' (Rat) -> str
    
            Return a string representation of the rat, in this format: symbol at (row, col) ate num_sprouts_eaten sprouts.
    
            >>> r = Rat('j', 4, 3)
            >>> str(r)
            'j at (4, 3) ate 0 sprouts.'
            '''
             
            return "{self.symbol} at ({self.row}, {self.col}) ate {self.num_sprouts_eaten} sprouts.".format(self=self)
            
        # Write your Rat methods here.
    
    class Maze:
        """ A 2D maze. """
    
        def __init__(self, maze, rat_1, rat_2):
            ''' (Maze, list of list of str, Rat, Rat)
    
            A rat maze with Brussel Sprouts.
            '''
            self.maze = maze
            self.rat_1 = rat_1
            self.rat_2 = rat_2
            num_sprouts_left = 0
            for lst in maze[:]:
                for ch in lst:
                    if ch == SPROUT:
                        num_sprouts_left = num_sprouts_left + 1
            self.num_sprouts_left = num_sprouts_left
        
        def is_wall(self, row, col):
            ''' (Maze, int, int) -> bool
    
            Check to see if there is a wall at given coordinates.
    
            >>> maze = Maze([['#', '#', '#', '#', '#', '#', '#'], \
                ['#', '.', '.', '.', '.', '.', '#'], \
                ['#', '.', '#', '#', '#', '.', '#'], \
                ['#', '.', '.', '@', '#', '.', '#'], \
                ['#', '@', '#', '.', '@', '.', '#'], \
                ['#', '#', '#', '#', '#', '#', '#']], \
                Rat('J', 1, 1), \
                Rat('P', 1, 4))
            >>> Maze.is_wall(maze, 0, 0)
            True
            >>> Maze.is_wall(maze, 3, 2)
            False
            '''
            return self.maze[row][col] == WALL
    
        def get_character(self, row, col):
            ''' (Maze, int, int) -> str
    
            Return the character in the maze at the given row and column. If there is a rat at that location, then its character is returned.
    
            >>> maze = Maze([['#', '#', '#', '#', '#', '#', '#'], \
                ['#', '.', '.', '.', '.', '.', '#'], \
                ['#', '.', '#', '#', '#', '.', '#'], \
                ['#', '.', '.', '@', '#', '.', '#'], \
                ['#', '@', '#', '.', '@', '.', '#'], \
                ['#', '#', '#', '#', '#', '#', '#']], \
                Rat('J', 1, 1), \
                Rat('P', 1, 4))
            >>> Maze.get_character(maze, 4, 5)
            'HALL'
            >>> Maze.get_character(maze, 3, 4)
            'WALL'
            >>> Maze.get_character(maze, 3, 3)
            'SPROUT'
            '''
    
            if self.maze[row][col] == self.rat_1.symbol:
                return self.rat_1.symbol
            elif self.maze[row][col] == self.rat_2.symbol:
                return self.rat_2.symbol
            elif self.maze[row][col] == '.':
                return 'HALL'
            elif self.maze[row][col] == '#':
                return 'WALL'
            elif self.maze[row][col] == '@':
                return 'SPROUT'
    
    
            
                            
                        
                
    
    
        def __str__(self):
            """ (Maze) -> str
    
            Return a string representation of the maze.
            """
            
            return '{self.maze}  {self.rat_1} {self.rat_2}'.format(self=self)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    I think you're asking someone to solve a homework problem for you. I've rewritten the framework in simplified form. By and by, the get_character method does not do what is described, and if there is an invalid character in the maze what does it return? hint
    Code:
    # Constants for visual representation of the contents of the maze.
    WALL, HALL, SPROUT = '#.@'
    
    # Constants for the directions. Use these to make Rats move.
    LEFT, RIGHT, NO_CHANGE, UP, DOWN = -1, 1, 0, -1, 1
    
    # The letters for rat_1 and rat_2 in the maze.
    (RAT_1_CHAR, RAT_2_CHAR,) = 'JP'
    
    class Rat:
        """ A rat caught in a maze. """
    
        def __init__(self, symbol, row, col):
            ''' (Rat, str, int, int) -> NoneType
    
            A rat with symbol, row, column, and number of Brussel Sprouts eaten.
            '''
            self.symbol = symbol
            self.row = row
            self.col = col
            self.num_sprouts_eaten = 0
    
        def set_location(self, row, col):
            ''' (Rat, int, int) -> NoneType
    
            Set the rat\'s row and col instance variables to the given row and column
    
            >>> rat = Rat('k', 3, 1)
            >>> Rat.set_location(rat, 2, 4)
            >>> rat.row
            2
            >>> rat.col
            4
            '''
            self.row = row
            self.col = col
    
        def eat_sprout(self):
            ''' (Rat) -> NoneType
    
            Add one to the rats instance variable num_sprouts_eaten.
    
            >>> r = Rat('k', 4, 5)
            >>> r.num_sprouts_eaten
            0
            >>> Rat.eat_sprout(r)
            >>> r.num_sprouts_eaten
            1
            '''
    
            self.num_sprouts_eaten += 1
    
        def __str__(self):
            ''' (Rat) -> str
    
            Return a string representation of the rat, in this format: symbol at (row, col) ate num_sprouts_eaten sprouts.
    
            >>> r = Rat('j', 4, 3)
            >>> str(r)
            'j at (4, 3) ate 0 sprouts.'
            '''
    
            return "{self.symbol} at ({self.row}, {self.col}) ate {self.num_sprouts_eaten} sprouts.".format(self=self)
    
        # Write your Rat methods here. ################ homework.
    
    class Maze:
        """ A 2D maze.
    
            ################ the maze need not be a list of lists.  Much easier to type as strings.
            ################ The individial doctests use the same maze.
            ################ Lumping them together makes sense to me.
            >>> maze = Maze(['#######', \
                             '#.....#', \
                             '#.###.#', \
                             '#..@#.#', \
                             '#@#.@.#', \
                             '#######'], \
                             Rat('J', 1, 1), \
                             Rat('P', 1, 4))
            >>> #################################################################
            >>> maze.is_wall(0, 0)
            True
            >>> maze.is_wall(3, 2)
            False
            >>> #################################################################
            >>> maze.get_character(4, 5)
            'HALL'
            >>> maze.get_character(3, 4)
            'WALL'
            >>> maze.get_character(3, 3)
            'SPROUT'
            >>> print(str(maze))
            #######
            #.....#
            #.###.#
            #..@#.#
            #@#.@.#
            #######
              J at (1, 1) ate 0 sprouts. P at (1, 4) ate 0 sprouts.
        """
    
        def __init__(self, maze, rat_1, rat_2):
            ''' (Maze, list of list of str, Rat, Rat)
    
            A rat maze with Brussel Sprouts.
            '''
            self.maze = maze
            self.rat_1 = rat_1
            self.rat_2 = rat_2
            num_sprouts_left = 0
            for lst in maze[:]:
                for ch in lst:
                    if ch == SPROUT:
                        num_sprouts_left += 1
            self.num_sprouts_left = num_sprouts_left
    
        def is_wall(self, row, col):
            ''' (Maze, int, int) -> bool
    
            Check to see if there is a wall at given coordinates.
    
            See class doctest
            '''
            return self.maze[row][col] == WALL
    
        def get_character(self, row, col):
            ''' (Maze, int, int) -> str
    
            Return the character in the maze at the given row and column. If there is a rat at that location, then its character is returned.
    
            See class doctest
            '''
            if self.maze[row][col] == self.rat_1.symbol:
                return self.rat_1.symbol
            elif self.maze[row][col] == self.rat_2.symbol:
                return self.rat_2.symbol
            elif self.maze[row][col] == HALL: ################ You defined the constants.  Use them.
                return 'HALL'
            elif self.maze[row][col] == WALL:
                return 'WALL'
            elif self.maze[row][col] == SPROUT:
                return 'SPROUT'
    
        def __str__(self):
            """ (Maze) -> str
    
            Return a string representation of the maze.
            """
            return '{}  {self.rat_1} {self.rat_2}'.format(   # much prettier this way.
                '\n'.join(self.maze)+'\n',self=self)
    Last edited by b49P23TIvg; April 24th, 2013 at 02:56 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I think you're asking someone to solve a homework problem for you. I've rewritten the framework in simplified form. By and by, the get_character method does not do what is described, and if there is an invalid character in the maze what does it return? hint
    Code:
    # Constants for visual representation of the contents of the maze.
    WALL, HALL, SPROUT = '#.@'
    
    # Constants for the directions. Use these to make Rats move.
    LEFT, RIGHT, NO_CHANGE, UP, DOWN = -1, 1, 0, -1, 1
    
    # The letters for rat_1 and rat_2 in the maze.
    (RAT_1_CHAR, RAT_2_CHAR,) = 'JP'
    
    class Rat:
        """ A rat caught in a maze. """
    
        def __init__(self, symbol, row, col):
            ''' (Rat, str, int, int) -> NoneType
    
            A rat with symbol, row, column, and number of Brussel Sprouts eaten.
            '''
            self.symbol = symbol
            self.row = row
            self.col = col
            self.num_sprouts_eaten = 0
    
        def set_location(self, row, col):
            ''' (Rat, int, int) -> NoneType
    
            Set the rat\'s row and col instance variables to the given row and column
    
            >>> rat = Rat('k', 3, 1)
            >>> Rat.set_location(rat, 2, 4)
            >>> rat.row
            2
            >>> rat.col
            4
            '''
            self.row = row
            self.col = col
    
        def eat_sprout(self):
            ''' (Rat) -> NoneType
    
            Add one to the rats instance variable num_sprouts_eaten.
    
            >>> r = Rat('k', 4, 5)
            >>> r.num_sprouts_eaten
            0
            >>> Rat.eat_sprout(r)
            >>> r.num_sprouts_eaten
            1
            '''
    
            self.num_sprouts_eaten += 1
    
        def __str__(self):
            ''' (Rat) -> str
    
            Return a string representation of the rat, in this format: symbol at (row, col) ate num_sprouts_eaten sprouts.
    
            >>> r = Rat('j', 4, 3)
            >>> str(r)
            'j at (4, 3) ate 0 sprouts.'
            '''
    
            return "{self.symbol} at ({self.row}, {self.col}) ate {self.num_sprouts_eaten} sprouts.".format(self=self)
    
        # Write your Rat methods here. ################ homework.
    
    class Maze:
        """ A 2D maze.
    
            ################ the maze need not be a list of lists.  Much easier to type as strings.
            ################ The individial doctests use the same maze.
            ################ Lumping them together makes sense to me.
            >>> maze = Maze(['#######', \
                             '#.....#', \
                             '#.###.#', \
                             '#..@#.#', \
                             '#@#.@.#', \
                             '#######'], \
                             Rat('J', 1, 1), \
                             Rat('P', 1, 4))
            >>> #################################################################
            >>> maze.is_wall(0, 0)
            True
            >>> maze.is_wall(3, 2)
            False
            >>> #################################################################
            >>> maze.get_character(4, 5)
            'HALL'
            >>> maze.get_character(3, 4)
            'WALL'
            >>> maze.get_character(3, 3)
            'SPROUT'
            >>> print(str(maze))
            #######
            #.....#
            #.###.#
            #..@#.#
            #@#.@.#
            #######
              J at (1, 1) ate 0 sprouts. P at (1, 4) ate 0 sprouts.
        """
    
        def __init__(self, maze, rat_1, rat_2):
            ''' (Maze, list of list of str, Rat, Rat)
    
            A rat maze with Brussel Sprouts.
            '''
            self.maze = maze
            self.rat_1 = rat_1
            self.rat_2 = rat_2
            num_sprouts_left = 0
            for lst in maze[:]:
                for ch in lst:
                    if ch == SPROUT:
                        num_sprouts_left += 1
            self.num_sprouts_left = num_sprouts_left
    
        def is_wall(self, row, col):
            ''' (Maze, int, int) -> bool
    
            Check to see if there is a wall at given coordinates.
    
            See class doctest
            '''
            return self.maze[row][col] == WALL
    
        def get_character(self, row, col):
            ''' (Maze, int, int) -> str
    
            Return the character in the maze at the given row and column. If there is a rat at that location, then its character is returned.
    
            See class doctest
            '''
            if self.maze[row][col] == self.rat_1.symbol:
                return self.rat_1.symbol
            elif self.maze[row][col] == self.rat_2.symbol:
                return self.rat_2.symbol
            elif self.maze[row][col] == HALL: ################ You defined the constants.  Use them.
                return 'HALL'
            elif self.maze[row][col] == WALL:
                return 'WALL'
            elif self.maze[row][col] == SPROUT:
                return 'SPROUT'
    
        def __str__(self):
            """ (Maze) -> str
    
            Return a string representation of the maze.
            """
            return '{}  {self.rat_1} {self.rat_2}'.format(   # much prettier this way.
                '\n'.join(self.maze)+'\n',self=self)
    I have been trying for two weeks to solve this problem. I don't need the answer, I just need some help.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Which part did you write?
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Which part did you write?
    The entire thing except for the constants
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    mmm. You wrote
    # Write your Rat methods here.
    ?

    Do the rats move by random choice?
    Do you consider user input?
    Can rats share a site in the maze?
    If the move is -1 and both LEFT and UP are available which direction has precedence?
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    mmm. You wrote
    # Write your Rat methods here.
    ?

    Do the rats move by random choice?
    Do you consider user input?
    Can rats share a site in the maze?
    If the move is -1 and both LEFT and UP are available which direction has precedence?

    Well not write your rat methods here, but everything else
    The rats move by user input
    Rats can share a site
    Rats cannot move diagonally

IMN logo majestic logo threadwatch logo seochat tools logo