| import random | | import os | | import time | | def init(level): | | return random.randint(0,level) | | dict={' ':0,'●':1} | | cells=[[' ' for x in range(32)] for y in range(22)] | | grid=[[' ' for x in range(32)] for y in range(22)] | | while True: | | level=input('larger number,less cells (>0):') | | try: | | if int(level)>0: | | for y in range(1,21): | | for x in range(1,31): | | r=random.randint(0,int(level)) | | if r==0:cells[y][x]='●' | | break | | except:pass | | | | for y in cells:print(' '.join(y)) | | print("press key to begin...") | | os.system('pause >nul') | | i=0 | | while True: | | for y in range(1,21): | | for x in range(1,31): | | num=dict[cells[y-1][x-1]]+\ | | dict[cells[y-1][x+1]]+\ | | dict[cells[y-1][x]]+\ | | dict[cells[y+1][x-1]]+\ | | dict[cells[y+1][x+1]]+\ | | dict[cells[y+1][x]]+\ | | dict[cells[y][x-1]]+\ | | dict[cells[y][x+1]] | | if num==3:grid[y][x]='●' | | elif num==2:grid[y][x]=cells[y][x] | | elif num :grid[y][x]=' ' | | for y in range(1,21): | | for x in range(1,31): | | cells[y][x]=grid[y][x] | | time.sleep(0.05) | | os.system('cls') | | i+=1 | | print('time: '+str(i)) | | for y in cells:print(' '.join(y))COPY |
这是出生概率为33%时的稳定状态,基本不变了,个别在动
概率50%的
如果要把这个脚本用迭代函数或者生成器写出来,该怎么改啊,加上是二维数组我更不会写了 |