[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创代码] 加入矩阵读取功能的Python细胞自动机 经测无误

代码:
  1. import random
  2. import os
  3. import time
  4. dict={' ':0,'*':1}
  5. cells=[[' ' for x in range(50)] for y in range(50)]
  6. grid=[[' ' for x in range(50)] for y in range(50)]
  7. while True:
  8.     choice=input('1 = read grid,2 = random generate:')
  9.     if choice=='1' or choice=='2':break
  10. if choice=='1':
  11.     while True:
  12.         f=input('file name ( max range:48*48 "." = dead "*" = live ):')
  13.         try:
  14.             txt=open(f,'r')
  15.             break
  16.         except:print('file not found')
  17.     y=0
  18.     for line in txt.readlines():
  19.         if y==48:
  20.             print('err:too much lines')
  21.             break
  22.         x=0
  23.         while True:
  24.             if x==48:
  25.                 print('err:too much cols')
  26.                 break
  27.             try:
  28.                 if line[x]=='*':cells[y+1][x+1]='*'
  29.                 x+=1
  30.             except:
  31.                 y+=1
  32.                 break
  33. if choice=='2':
  34.     while True:
  35.         level=input('larger number,less cells ( >0 ):')
  36.         try:
  37.             if int(level)>0:
  38.                 for y in range(1,49):
  39.                     for x in range(1,49):
  40.                         r=random.randint(0,int(level))
  41.                         if r==0:cells[y][x]='*'
  42.                 break
  43.         except:pass
  44. for y in cells:print(' '.join(y))
  45. print("press key to begin...")
  46. os.system('pause >nul')
  47. i=0
  48. while True:
  49.     for y in range(1,49):
  50.         for x in range(1,49):
  51.             num=dict[cells[y-1][x-1]]+\
  52.                 dict[cells[y-1][x+1]]+\
  53. dict[cells[y-1][x]]+\
  54. dict[cells[y+1][x-1]]+\
  55. dict[cells[y+1][x+1]]+\
  56. dict[cells[y+1][x]]+\
  57. dict[cells[y][x-1]]+\
  58. dict[cells[y][x+1]]
  59.             if num==3:grid[y][x]='*'
  60.             elif num==2:grid[y][x]=cells[y][x]
  61.             else:grid[y][x]=' '
  62.     for y in range(1,49):
  63.         for x in range(1,49):
  64.             cells[y][x]=grid[y][x]
  65.     time.sleep(0.01)
  66.     os.system('cls')
  67.     i+=1
  68.     print('time: '+str(i))
  69.     num=0
  70.     for y in range(1,49):
  71.         for x in range(1,49):
  72.             num+=dict[cells[y][x]]
  73.     if num==0:
  74.         print('cells all dead,press key to exit...')
  75.         break
  76.     else:
  77.         for y in cells:print(' '.join(y))
  78. os.system('pause >nul')
复制代码
要正常显示的话,需要设置cmd,这是改自3d球的
  1. @echo off & setlocal enabledelayedexpansion & color 0a & title PYTHON CELL
  2. if "%1"=="" (
  3. for %%a in (FontSize:000c0006  WindowSize:003c0064  ScreenColors:0000000f  CodePage:000003a8  ScreenBufferSize:003d0064) do for /f "tokens=1,2 delims=:" %%b in ("%%a") do (
  4. >nul reg add HKCU\Console\PYTHON_CELL /v %%b /t reg_dword /d 0x%%c /f)
  5. start "PYTHON_CELL" /max "%ComSpec%" /c "%~s0" 1&goto:eof
  6. ) else (
  7. >nul reg delete HKCU\Console\PYTHON_CELL /f
  8. )
  9. python cell.py
复制代码
本来早早就写好了,一直被 line[x]:index out of range困扰,估计读入的行最后并不是'\n',没办法用了异常处理终于成功
有兴趣的可以试试随机生成功能,观察下初始状态不同导致的结果差异,数字输的太大的话第2次就会死光光哦~
!scripting!

返回列表