0

我尝试通过 Spyder 和在线 IDE 运行以下代码,但没有人完全完成该程序。它要么超时,要么只是拒绝运行。

import random
from pprint import pprint

petri_dish = []

class Species:
    def __init__(self,total,name,life,attack,defense,move,location):
        area = 1000
        self.total = 100
        self.name = name
        self.life = self.total - (random.randint(1,100))
        self.attack = self.total - (random.randint(1,100))
        self.defense = self.total - (random.randint(1,100))
        self.move = self.total - (random.randint(1,100))
        self.location = [random.randint(1,area),random.randint(1,area)]
    def relocate(self):
        x_move_add = random.randint(self.location[0], self.location[0] + self.move)
        x_move_minus = random.randint(self.location[0] - self.move,self.location[0])
        y_move_add = random.randint(self.location[1], self.location[1] + self.move)
        y_move_minus = random.randint(self.location[1] - self.move,self.location[1])
        self.location = [random.randint(x_move_minus,x_move_add),random.randint(y_move_minus,y_move_add)]
        for n in range(2):
            if self.location[n] > 1000:
                self.location[n] = 1000
            elif self.location[n] < 0:
                self.location[n] = 0
    def fight(self,enemy):
        while self.life > 0 and enemy.life > 0:
            self.life = (self.life + self.defense) - enemy.attack
            enemy.life = (enemy.life + enemy.defense) - self.attack
        else:
            if self.life > enemy.life:
                print 'Species #' + str(enemy.name) + ' was eaten!'
                self.attack = self.attack + enemy.attack
                self.life = 100
                petri_dish.remove(enemy)
            else:
                print 'Species #' + str(self.name) + ' was eaten.'
                enemy.attack = enemy.attack + self.attack
                enemy.life = 100
                petri_dish.remove(self)
    def target(self):
        for z in petri_dish:
            if z.location != self.location:
                if (z.location[0] in range(self.location[0] - self.move, self.location[0] + self.move)) and (z.location[1] in range(self.location[1] - self.move, self.location[1] + self.move)):
                    self.fight(z)

for n in range(20):
    petri_dish.append(Species(0,n,0,0,0,0,0))

def show():
    for z in petri_dish:
        print z.location,z.move

def move_around():
    for x in petri_dish:
        x.relocate()
        x.target()

while len(petri_dish) > 1:
    move_around()

for x in petri_dish:
    pprint(vars(x))

知道发生了什么吗?这以前可以工作,但现在已经坏了。您可能会说,这个程序是一个非常非常简单的培养皿模拟器,由一些非常不智能的细胞组成。

额外问题:无限循环对您的计算机有害吗?我已经击中了其中一​​些,我不想冒险以任何方式、形状或形式伤害我的机器。

4

2 回答 2

2

大多数情况下,这是因为您的算法可以生成不受您选择的算法约束的输入。

首先,random.randint(1,100)会产生一个介于 1 和 100 之间的数字。不过,您确实在使用100 - randint(1,100)它,但偶尔会产生 0。如果你得到了两个 move=0 的项目,那么任何一个都不能真正移动来吸引另一个,所以你的循环永远不会退出。也许只是使用self.move = random.randint(1,100)等(生活和其他事情也是如此)。

还有其他无效的约束 - 采取这些行:

self.life = (self.life + self.defense) - enemy.attack
enemy.life = (enemy.life + enemy.defense) - self.attack

这有两个问题。一,如果 x.defense > y.attack,你实际上是在给物体增加生命。您可能希望在 self.life 的初始值(如果您真的想要治愈,则为 100)使其饱和。

第二,即使你这样做,你也可以有这样的情况:self.attack = 20 self.defense = 30enemy.attack = 20enemy.defense = 30

这基本上是一场枕头大战 :) 因为攻击总是少于防御,所以生命实际上都不会倒下,这个循环将永远运行。你可能想在这里引入一个随机元素。

于 2013-10-02T14:09:12.797 回答
0

您应该更具体地了解“拒绝运行”和“超时”的含义。

我的理解是“超时”意味着你有一个无限循环。如果单元格没有遇到彼此以具有fight().

我会对程序进行一些更改:

  • 使用一个(或两个)参数将 移动target()为普通函数。这样,Species该类将不依赖于全局人口数组。
  • 在每次迭代之后做 a pprint,而不是在最后。如果上述(随机运行的单元格)成立,则最后的 pprint 将不会被执行。
  • while条件更改为在len() == 1最大迭代次数处或之后结束

高温下,

于 2013-10-02T14:07:13.380 回答