在Python的布鲁塞尔芽菜比赛

时间:2013-05-01 16:25:10

标签: python class location boolean symbols

这是python游戏中的代码,其中一只或两只老鼠吃布鲁塞尔豆芽。它包含一个Rat类和Maze类:

class Rat:
""" A rat caught in a maze. """
    # Write your Rat methods here.
    def __init__(Rat, symbol, row, col):
        Rat.symbol = symbol
        Rat.row = row
        Rat.col = col

        num_sprouts_eaten = 0

    def set_location(Rat, row, col):

        Rat.row = row
        Rat.col = col

    def eat_sprout(Rat):
        num_sprouts_eaten += 1        

    def __str__(Rat):
        """ (Contact) -> str

        Return a string representation of this contact.
        """
        result = ''

        result = result + '{0} '.format(Rat.symbol) + 'at '

        result = result + '('+ '{0}'.format(Rat.row) + ', '
        result = result + '{0}'.format(Rat.col) + ') ate '
        result = result + str(num_sprouts_eaten) + ' sprouts.'
        return result


class Maze:
    """ A 2D maze. """

    # Write your Maze methods here.
    def __init__(Maze, content, rat_1, rat_2):
        Maze.content= [content]

        Maze.rat_1 = RAT_1_CHAR
        Maze.rat_2 = RAT_2_CHAR

    def is_wall(Maze, row,col):
        walls = False

        if WALL in Maze.content[row*col]:
            walls = True
        return walls

现在,如果我通过调用大鼠1和大鼠2的迷宫和位置来初始化该类。

Maze([['#', '#', '#', '#', '#', '#', '#'], 
      ['#', '.', '.', '.', '.', '.', '#'], 
      ['#', '.', '#', '#', '#', '.', '#'], 
      ['#', '.', '.', '@', '#', '.', '#'], 
      ['#', '@', '#', '.', '@', '.', '#'], 
      ['#', '#', '#', '#', '#', '#', '#']], 
      Rat('J', 1, 1),
      Rat('P', 1, 4))

字符'#'代表墙,'。'代表走廊或路径,'@'代表每个布鲁塞尔芽...

现在,如果墙('#')位于老鼠遇到的特定设置位置,如果墙上没有墙,那么如何确保布尔值为True?如果墙上没有墙,则返回False?在这种情况下,走廊还是布鲁塞尔芽?

P.S ..这里是大鼠和迷宫课程之前RAT_1_CHAR ='J'RAT_2_CHAR ='P'的定义...... thnx

# 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'
num_sprouts_eaten = 0

1 个答案:

答案 0 :(得分:3)

def is_wall(self, row, col): return self.content[row][col] == '#'

您访问列表项的语法错误。您定义成员函数的语法也是如此。没有任何方法可以运行。

当你学习一门语言时,一定要确保在构建较大的程序之前尝试编写和执行小程序(在这种情况下,包含单个类的程序)。