这是一个游戏的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))
字符“#”代表墙“。”。代表走廊或小径,“ @”代表每个球芽甘蓝...
现在,如果墙壁('#')位于老鼠要进入的特定设置位置,并且如果在该特定设置位置没有墙壁,则返回布尔值,如何确定布尔值为True?在这种情况下,是走廊还是抱子甘蓝?
P.S ..这是RAT_1_CHAR ='J'RAT_2_CHAR ='P'的定义,在Rats和Maze类之前……
# 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 - 布鲁塞尔新芽游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16322163/