代码: | import random | | import os | | import time | | dict={' ':0,'*':1} | | cells=[[' ' for x in range(50)] for y in range(50)] | | grid=[[' ' for x in range(50)] for y in range(50)] | | while True: | | choice=input('1 = read grid,2 = random generate:') | | if choice=='1' or choice=='2':break | | if choice=='1': | | while True: | | f=input('file name ( max range:48*48 "." = dead "*" = live ):') | | try: | | txt=open(f,'r') | | break | | except:print('file not found') | | y=0 | | for line in txt.readlines(): | | if y==48: | | print('err:too much lines') | | break | | x=0 | | while True: | | if x==48: | | print('err:too much cols') | | break | | try: | | if line[x]=='*':cells[y+1][x+1]='*' | | x+=1 | | except: | | y+=1 | | break | | | | if choice=='2': | | while True: | | level=input('larger number,less cells ( >0 ):') | | try: | | if int(level)>0: | | for y in range(1,49): | | for x in range(1,49): | | 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,49): | | for x in range(1,49): | | 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] | | else:grid[y][x]=' ' | | for y in range(1,49): | | for x in range(1,49): | | cells[y][x]=grid[y][x] | | time.sleep(0.01) | | os.system('cls') | | i+=1 | | print('time: '+str(i)) | | num=0 | | for y in range(1,49): | | for x in range(1,49): | | num+=dict[cells[y][x]] | | if num==0: | | print('cells all dead,press key to exit...') | | break | | else: | | for y in cells:print(' '.join(y)) | | os.system('pause >nul')COPY |
要正常显示的话,需要设置cmd,这是改自3d球的 | @echo off & setlocal enabledelayedexpansion & color 0a & title PYTHON CELL | | | | if "%1"=="" ( | | | | for %%a in (FontSize:000c0006 WindowSize:003c0064 ScreenColors:0000000f CodePage:000003a8 ScreenBufferSize:003d0064) do for /f "tokens=1,2 delims=:" %%b in ("%%a") do ( | | | | >nul reg add HKCU\Console\PYTHON_CELL /v %%b /t reg_dword /d 0x%%c /f) | | | | start "PYTHON_CELL" /max "%ComSpec%" /c "%~s0" 1&goto:eof | | | | ) else ( | | | | >nul reg delete HKCU\Console\PYTHON_CELL /f | | | | ) | | python cell.pyCOPY |
本来早早就写好了,一直被 line[x]:index out of range困扰,估计读入的行最后并不是'\n',没办法用了异常处理终于成功
有兴趣的可以试试随机生成功能,观察下初始状态不同导致的结果差异,数字输的太大的话第2次就会死光光哦~ |