【问题标题】:Brussels Sprout game in pythonpython中的球芽甘蓝游戏
【发布时间】:2013-04-25 16:30:39
【问题描述】:

这是一个游戏的 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,如果该特定设置位置没有墙壁,则返回 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

【问题讨论】:

  • RAT_1_CHARRAT_2_CHAR 定义在哪里?为什么要使用它们而不是函数接受作为参数的rat_1rat_2
  • 按照惯例,类方法定义中的第一个参数应该是self。你有充分的理由改用RatMaze 吗?
  • 我觉得 Rat 和 Maze 更容易定义......我改变了一些东西,RAT_1_CHAR 和 RAT_2_CHAR 分别代表字母 J 和 P
  • 在什么方面使用Maze 作为参数名称比使用self 更容易?如果有的话,Maze更难输入,因为它有一个大写字母。
  • 好吧,我现在很困惑......因为我已经完成了超过 50% 的工作,使用 Rat 和 Maze 让后者输入任何迷宫!!!

标签: python class location boolean symbols


【解决方案1】:
def is_wall(self, row, col): return self.content[row][col] == '#'

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

当您学习一门语言时,请务必先尝试编写和执行小程序(在这种情况下,是包含单个类的程序),然后再构建较大的程序。

【讨论】:

  • 它是一个布尔值...如果它是一个布尔值,我该如何解决这个问题? is_wall 方法是一个布尔值!
  • 是的,这个函数返回一个布尔值(真/假)。
  • Anubhav 定义的函数确实返回一个布尔值。 self.content[row][col] 返回 self.content 的 'row-th' 项中的 'col-th' 项 - 例如, self.content[2][0] 将返回第 0 项(请记住,计算机从零开始计数)self.content 的第 2 项(更简单地说,是第 2 行的第 0 项)。如果 a 等于 b,a == b 返回 True,否则返回 False。因此,self.content[row][col] == "#" 如果 self.content 的第 'row-th' 项的第 'col-th' 项等于 "#",则返回 True,否则返回 False。
  • 我在尝试时收到此错误消息 - >>> is_wall(1,1) Traceback(最近一次调用最后一次):文件“”,第 1 行,在 is_wall(1,1) NameError: name 'is_wall' 没有定义
  • @BenStein:如果is_wallMaze的方法,你不能只写is_wall(1, 1);你必须有一个Maze 实例才能调用它。 (如果你想一想,你试图问的问题在英语中甚至没有意义。“迷宫中的位置 (1, 1) 是一堵墙吗?”答案是“这取决于有问题的特定迷宫。哪个迷宫?”)例如,如果你已经完成了my_maze = Maze(my_content, my_rat1, my_rat2),那么你可以写my_maze.is_wall(1, 1)
猜你喜欢
  • 1970-01-01
  • 2020-05-22
  • 2014-02-17
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
  • 2015-01-06
  • 1970-01-01
相关资源
最近更新 更多