私はPythonが初めてで、迷路のネズミが芽キャベツを食べようとするゲームをやろうとしています. これまでのところ、Rat クラスのすべての関数が機能しています。そして、Maze クラスの最後の関数に行き詰まっています。両方のクラスが絡み合っています。Maze クラスの move メソッドの修正に問題があります。以下の 2 つのクラスがあります。
# 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
>>>
エラーメッセージを修正し、ラットを迷路内の任意の場所に移動するのを手伝ってください (廊下にある限り)!