Conway's Game of Life 是生命游戏中的一类, 规则可以简单描述成 23/3 或者 B3/S23, 即邻居是2或3个时继续存 ...
neorobin 发表于 2014-9-28 10:52
大神发的这个链接好多好看的矩阵,我把代码改了下,用把.lif文件拖到importGrid.cmd上就可以看矩阵的效果了
也可以直接运行cell.py,用随机矩阵玩
cell.py- import sys
- import os
- import random
- import time
- cols=100
- rows=80
- dict={' ':0,'*':1}
- cells=[[' ' for x in range(cols)] for y in range(rows)]
- grid=[[' ' for x in range(cols)] for y in range(rows)]
- if len(sys.argv)>1:
- f=sys.argv[1]
- try:
- txt=open(f,'r')
- except:
- print('file not found')
- os.system('pause >nul')
- exit()
- y=1
- for line in txt.readlines():
- if y==rows-1:
- print('err:too much lines')
- break
- if line[0]=='#':continue
- for x in range(cols-2):
- try:
- if line[x]=='*':cells[y][x+1]='*'
- except:
- y+=1
- break
- else:
- print('cmdline:cell.py *.lif')
- while True:
- level=input('larger number,less cells ( >0 ):')
- try:
- if int(level)>0:
- for y in range(1,rows-1):
- for x in range(1,cols-1):
- 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,rows-1):
- for x in range(1,cols-1):
- 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,rows-1):
- for x in range(1,cols-1):
- 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,rows-1):
- for x in range(1,cols-1):
- 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')
复制代码 importGrid.cmd:- @echo off & setlocal enabledelayedexpansion & color 0a & title PYTHON CELL
-
- if "%2"=="" (
-
- for %%a in (FontSize:00080005 WindowSize:005c00c8 ScreenColors:0000000f CodePage:000003a8 ScreenBufferSize::005c00c8) 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 1&goto:eof
-
- ) else (
-
- >nul reg delete HKCU\Console\PYTHON_CELL /f
-
- )
- python %~dp0cell.py %1
复制代码
|