【问题标题】:Trying to move turtle object to random location [closed]试图将海龟对象移动到随机位置[关闭]
【发布时间】:2017-03-26 23:28:28
【问题描述】:

我正在尝试切换此方法,以允许鱼尝试多达 4 个随机位置以进行下一步。我已经尝试了一些破解,但我还没有想出一个好的方法。

def tryToMove(self):
        offsetList = [(-1, 1), (0, 1), (1, 1),
                  (-1, 0)        , (1, 0),
                  (-1, -1), (0, -1), (1, -1)]
        randomOffsetIndex = random.randrange(len(offsetList))
        randomOffset = offsetList[randomOffsetIndex]
        nextx = self.xpos + randomOffset[0]
        nexty = self.ypos + randomOffset[1]
        while not(0 <= nextx < self.world.getMaxX() and 0 <= nexty < self.world.getMaxY()):
            randomOffsetIndex = random.randrange(len(offsetList))
            randomOffset = offsetList[randomOffsetIndex]
            nextx = self.xpos + randomOffset[0]
            nexty = self.ypos + randomOffset[1]

        if self.world.emptyLocation(nextx, nexty):
            self.move(nextx, nexty)

【问题讨论】:

  • 顺便说一句randomOffset = random.choice(offsetList)
  • 有什么问题?你收到错误信息吗?显示有问题?或者创建最小的工作示例,以便我们可以运行它并查看问题。

标签: python random methods turtle-graphics


【解决方案1】:

我通常不会评论我无法运行的代码,但我会尝试解决这个问题。下面是我如何设计这种方法。

offsetList = [(-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)]

def tryToMove(self):

    maxX = self.world.getMaxX()
    maxY = self.world.getMaxY()

    possibleOffsets = offsetList[:]  # make a copy we can change locally

    while possibleOffsets:
        possibleOffset = random.choice(possibleOffsets)  # ala @furas

        nextx = self.xpos + possibleOffset[0]
        nexty = self.ypos + possibleOffset[1]

        if (0 <= nextx < maxX() and 0 <= nexty < maxY()) and self.world.emptyLocation(nextx, nexty):
            self.move(nextx, nexty)
            return True  # valid move possible and made

        possibleOffsets.remove(possibleOffset)  # remove invalid choice

    return False  # no valid move possible for various reasons

我不明白你的意思:

为下一步尝试最多 4 个随机位置

因为最多有 8 种可能性,但如果你真的想限制它,那么这里有一种替代方法,可以找到所有 possibleOffsets,你可以根据需要对其进行修剪:

offsetList = [(-1, 1), (0, 1), (1, 1), (-1, 0), (1, 0), (-1, -1), (0, -1), (1, -1)]

def tryToMove(self):

    maxX = self.world.getMaxX()
    maxY = self.world.getMaxY()

    possibleOffsets = []

    for possibleOffset in offsetList:
        nextx = self.xpos + possibleOffset[0]
        nexty = self.ypos + possibleOffset[1]

        if 0 <= nextx < maxX and 0 <= nexty < maxY and self.world.emptyLocation(nextx, nexty):
            possibleOffsets.append(possibleOffset)

    if possibleOffsets:
        offsetX, offsetY = random.choice(possibleOffsets)  # ala @furas
        nextx = self.xpos + offsetX
        nexty = self.ypos + offsetY

        self.move(nextx, nexty)
        return True  # valid move possible and made

    return False  # no valid move possible for various reasons

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2021-12-31
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2017-07-11
    相关资源
    最近更新 更多