Quantcast
Viewing all articles
Browse latest Browse all 768

Learn to Program: Crafting Quality Code

# Do not import any modules. If you do, the tester may reject your submission.

# 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. """

    # Write your Rat methods here.
    def __init__(self, symbol, row, col):
    """(Rat, str, int, int) -> NoneType"""
        self.symbol = symbol
        self.row = row
        self.col = col
        self.num_sprouts_eaten = 0
    def set_location(self, row, col):
    """(Rat, int, int) -> NoneType"""
        self.row = row
        self.col = col
    def eat_sprout(self):
    """(Rat) -> NoneType"""
        self.num_sprouts_eaten = self.num_sprouts_eaten + 1
    def __str__(self):
    """(Rat) -> str"""
         return self.symbol + ' at ('+ self.row +', '+ self.col +') ate ' + self.num_sprouts_eaten + ' sprouts.'
class Maze:
    """ A 2D maze. """
    def __init__(self, maze, rat_1, rat_2):
    """(Maze, list of list of str, Rat, Rat) -> NoneType"""
        self.maze = maze
        self.rat_1 = rat_1
        self.rat_2 = rat_2
        self.num_sprouts_left = 0
        for i in range(maze):
             for j in range(len(maze[i])):
                 if (maze[i][j] == SPROUT):
                     self.num_sprouts_left = self.num_sprouts_left + 1

    def is_wall(self, row, col):
    """(Maze, int, int) -> bool"""
        return maze[row][col] == WALL
    def get_character(self, row, col):
    """(Maze, int, int) -> str"""
        return maze[row][col]
    def move(self, rat, vDir, hDir):
    """(Maze, Rat, int, int) -> bool"""
        row = rat.row + vDir
        col = rat.col + hDir
        if (maze[row][col] == WALL):
            return False
        else:
            if (maze[row][col] == SPROUT):
                rat.eat_sprout()
                maze[row][col] = HALL
                self.num_sprouts_left = self.num_sprouts_left - 1
            rat.set_location(row, col)
            return True
    def __str__(self):
    """(Maze) -> str"""          
         maze[rat_1.row][rat_1.col] == rat_1.symbol
         maze[rat_2.row][rat_2.col] == rat_2.symbol
         result = str(maze) + str(rat_1) + str(rat_2)
         maze[rat_1.row][rat_1.col] == HALL
         maze[rat_2.row][rat_2.col] == HALL
         return result
    # Write your Maze methods here.

Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 768

Trending Articles