I've tried running the following code through Spyder and an online IDE, but neither one fully completes the program. It either times out or just refuses to run.
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))
Any idea as to what's going on? This was working earlier, but now it's broken. As you can probably tell, this program is a really, really simple petri dish simulator populated by some remarkably unintelligent cells.
Bonus Question: Are infinite loops bad for your computer? I've hit a few of them and I don't want to risk harming my machine in any way, shape, or form.