用户:
红石小蝈查看:0 回复:2 评论:0 创建时间:2019-05-17T22:23:24
import time
m={True:'■',False:'□'}
class bool_grid:
def __init__(self,x=30,y=30,obj=False):
self.grid=[]
for a in range(x):
self.grid.append([])
for b in range(y):
self.grid[-1].append(obj)
def life_game(self):
grid=self.grid
l=len(grid)
w=len(grid[0])
next_grid=[]
for _x in grid:
next_grid.append([])
for y in _x:
next_grid[-1].append(y)
for x in range(l):
for y in range(w):
score=0
for xn in range(x-1,x+2):
for yn in range(y-1,y+2):
score+=grid[xn%l][yn%w]
if grid[x][y]:
if score>4 or score<3:next_grid[x][y]=False
elif score==3:next_grid[x][y]=True
self.grid=next_grid
def __repr__(self,tf=True):
a=''
for x in self.grid:
a+='\n'
for y in x:
if tf:
a+=m[y]
else:a+=str(y)+' '
return a
def _change(self,x,y):
self.grid[x][y]=not self.grid[x][y]
def change(self,*l):
for x in l:
self._change(x[0],x[1])
g=bool_grid()
g.change((10,12),#u
(11, 8),#can
( 9,10),#change
( 8,12),#here
( 9,14),#into
( 9, 9),#positions
(11,13),#u
(12,10),#wanna
(10,10) #set
)
for x in range(10):
print(g)
g.life_game()
time.sleep(0.1)
print()
本人在B站有号,名字一样