python - Python 中的布鲁塞尔芽菜游戏

标签 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

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

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

字符“#”代表墙,“.”代表走廊或小路,“@”代表球芽甘蓝...

现在,如果墙壁('#')位于老鼠遇到的特定设置位置,我如何确保 boolean 值为 True,如果该特定设置位置没有墙壁,则返回 False?在这种情况下,要么是走廊,要么是球芽甘蓝?

P.S.. 这是 RAT_1_CHAR = 'J' RAT_2_CHAR = 'P' 的定义,位于 Rats 和 Maze 类之前...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

最佳答案

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

您访问列表项的语法是错误的。定义成员函数的语法也是如此。这些都是不可能运行的。

当您学习一门语言时,请始终确保在构建较大的程序之前尝试编写和执行小程序(在本例中为包含单个类的程序)。

关于python - Python 中的布鲁塞尔芽菜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16322163/

相关文章:

objective-c - 用作全局变量的 AppDelegate 变量不起作用

android - GPS 和 NETWORK_PROVIDER 在真实设备上不工作

python - 使用 Python libtorrent 创建守护进程以获取 100k+ 种子的元数据

python - 使用 cqlengine 在 cassandra 中插入和更新大量行的最快和最有效的方法

javascript - 在 Javascript 类中从另一个方法调用一个方法

python - 在python中是否有类似js提升类定义的东西?

python - 替换序列中的 1's with 0'

python - while循环python问题

android - Google Play 服务位置 API 有时会返回错误的位置

Android 位置监听器不起作用