Python芽甘蓝游戏

2024-06-26 13:01:09 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是python中的代码,其中一个或两个老鼠吃球芽甘蓝。它包含一个老鼠类和迷宫类:

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的位置来初始化类。在

^{pr2}$

字符“#”代表一堵墙,“.”代表走廊或小路,“@”代表每个球芽甘蓝。。。在

现在我如何确保布尔值是真的,如果一个墙(‘#’)在rat遇到的特定集合位置,而如果在这个特定的集合位置没有墙,那么返回False呢?在这种情况下,不是走廊还是球芽甘蓝?在

附言。。下面是老鼠和迷宫课之前的定义

# 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

Tags: oftheindefcolresultrow老鼠
1条回答
网友
1楼 · 发布于 2024-06-26 13:01:09
def is_wall(self, row, col): return self.content[row][col] == '#'

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

当你学习一门语言时,一定要在构建更大的程序之前尝试编写和执行小程序(在本例中,是一个包含单个类的程序)。在

相关问题 更多 >