我对 Python 真的很陌生,我正在尝试做一个游戏,让迷宫中的老鼠尝试吃布鲁塞尔豆芽 - 所以我有 2 只老鼠 - 'J' 和 'P' 和 2 个班级 - 老鼠和迷宫。到目前为止,Rat 类的所有功能都有效 - 我被 Maze 类的最后一个功能困住了!这两个类是交织在一起的。我在修复 Maze 类的 move 方法时遇到问题 - 这是下面的两个类。
# 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
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: """ 一个 2D 迷宫。"""
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):
return (Maze.content[row][col] == '#')
def get_character(Maze,row, col):
chars = ''
if 'J' in Maze.content[row][col]:
chars = 'J'
elif 'P' in Maze.content[row][col]:
chars = 'P'
elif '#' in Maze.content[row][col]:
chars = WALL
else:
chars = HALL
return chars
def move(Maze, Rat, hor, ver):
num_sprouts_left = sum(x.count('@') for x in Maze.content[row][col])
nowalls = False
if Rat in Maze.content[row][col] and Maze.is_wall(row, col) == True:
NO_CHANGE = Rat.set_location(row+0,col+0)
if Rat in Maze.content[row][col] and Maze.is_wall(row, col) == False:
UP = Rat.set_location(row,col+1)
if UP == SPROUT:
Rat.eat_sprout(Rat)
num_sprouts_left -= 1
SPROUT=HALL
if Rat in Maze.content[row][col] and Maze.is_wall(row, col) == False:
DOWN = Rat.set_location(row,col-1)
if DOWN == SPROUT:
Rat.eat_sprout(Rat)
num_sprouts_left -= 1
SPROUT=HALL
if Rat in Maze.content[row][col] and Maze.is_wall(row, col) == False:
LEFT = Rat.set_location(row-1,col)
if LEFT == SPROUT:
Rat.eat_sprout(Rat)
num_sprouts_left -= 1
SPROUT=HALL
if Rat in Maze.content[row][col] and Maze.is_wall(row, col) == False:
RIGHT = Rat.set_location(row+1,col)
if RIGHT == SPROUT:
Rat.eat_sprout(Rat)
num_sprouts_left -= 1
SPROUT=HALL
nowalls = True
return nowalls
所以当我通过 Maze 对象调用 move 方法时 - 我收到一条错误消息!
>>> d = Maze([['#', '#', '#', '#', '#', '#', '#'],
['#', '.', '.', '.', '.', '.', '#'],
['#', '.', '#', '#', '#', '.', '#'],
['#', '.', '.', '@', '#', '.', '#'],
['#', '@', '#', '.', '@', '.', '#'],
['#', '#', '#', '#', '#', '#', '#']],
Rat('J', 1, 1),
Rat('P', 1, 4))
>>> d.move('J',2,2)
Traceback (most recent call last):
File "<pyshell#167>", line 1, in <module>
d.move('J',2,2)
File "C:\Users\gijoe\Downloads\a2.py", line 96, in move
num_sprouts_left = sum(x.count('@') for x in Maze.content[row][col])
NameError: global name 'row' is not defined
>>>
请帮我修复错误信息并将老鼠移动到迷宫中的任何一点(只要它在走廊里)!