运行没有出错,就是最后出来的图形和预想的不一样,应该是逻辑错误,哪位大神帮我看一下源码
有很多应该消失的元胞没有消失
import random
import time
import os
class Worm(object):
def __init__(self,posx,posy):
self.posx = posx
self.posy = posy
self.stat = random.randint(0,1)
def birth(self):
self.stat = 1
def death(self):
self.stat = 0
def envir(self):
self.life_num = 0
for x in range(self.posx-1,self.posx+2):
for y in range(self.posy-1,self.posy+2):
if(x >= 0 and x < xmax and y >= 0 and y < ymax):
if(x != self.posx and y != self.posy):
self.life_num += worm_list[y][x].stat
def update(self):
if(self.stat):
if(self.life_num < 2):
self.death()
elif(self.life_num > 3):
self.death()
else:
pass
else:
if(self.life_num == 3):
self.birth()
def show(self):
if(self.stat):
print("▇▇",end='')
else:
print(" ",end='')
xmax = int(input("input the width for the game:"))
ymax = int(input("\ninput the height for the game:"))
worm_list = [[0 for lie in range(xmax)] for row in range(ymax)]
for y in range(0,ymax):
for x in range(0,xmax):
worm_list[y][x] = Worm(x,y)
while 1:
for y in range(0,ymax):
for x in range(0,xmax):
worm_list[y][x].show()
worm_list[y][x].envir()
print('')
print('')
for y in range(0,ymax):
for x in range(0,xmax):
worm_list[y][x].update()
#worm_list[random.randint(0,ymax-1)][random.randint(0,xmax-1)].stat = random.randint(0,1)
time.sleep(0.5)
i = os.system('clear')


import random
import time
import os
class Worm(object):
def __init__(self,posx,posy):
self.posx = posx
self.posy = posy
self.stat = random.randint(0,1)
def birth(self):
self.stat = 1
def death(self):
self.stat = 0
def envir(self):
self.life_num = 0
for x in range(self.posx-1,self.posx+2):
for y in range(self.posy-1,self.posy+2):
if(x >= 0 and x < xmax and y >= 0 and y < ymax):
if(x != self.posx and y != self.posy):
self.life_num += worm_list[y][x].stat
def update(self):
if(self.stat):
if(self.life_num < 2):
self.death()
elif(self.life_num > 3):
self.death()
else:
pass
else:
if(self.life_num == 3):
self.birth()
def show(self):
if(self.stat):
print("▇▇",end='')
else:
print(" ",end='')
xmax = int(input("input the width for the game:"))
ymax = int(input("\ninput the height for the game:"))
worm_list = [[0 for lie in range(xmax)] for row in range(ymax)]
for y in range(0,ymax):
for x in range(0,xmax):
worm_list[y][x] = Worm(x,y)
while 1:
for y in range(0,ymax):
for x in range(0,xmax):
worm_list[y][x].show()
worm_list[y][x].envir()
print('')
print('')
for y in range(0,ymax):
for x in range(0,xmax):
worm_list[y][x].update()
#worm_list[random.randint(0,ymax-1)][random.randint(0,xmax-1)].stat = random.randint(0,1)
time.sleep(0.5)
i = os.system('clear')