我尝试通过 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))
知道发生了什么吗?这以前可以工作,但现在已经坏了。您可能会说,这个程序是一个非常非常简单的培养皿模拟器,由一些非常不智能的细胞组成。
额外问题:无限循环对您的计算机有害吗?我已经击中了其中一些,我不想冒险以任何方式、形状或形式伤害我的机器。