游戏介绍
康威生命游戏 Conway's Game of Life,又称康威生命棋,是英国数学家约翰·何顿·康威 John Horton Conway在1970年发明的元胞自动机。可以用来模拟细胞的自然进化
比较经典的一版游戏规则概述如下:
1. 每个细胞的状态由该细胞及周围八个细胞上一次的状态所决定;
2. 如果一个细胞周围有3个细胞为生,则该细胞由死转生或保持生;
3. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
4. 在其它情况下,该细胞由生转死或保持死
参照Python编程快速上手的程序1
import copy
import random
import time
WIDTH = 60
HEIGHT = 20
# 创建于一个列表的列表数据结构,用来存储代表活细胞和死细胞的’#‘和’ ‘字符串,他们在列表中的位置反映了他们在屏幕上的位置
nextCells = [] # 创建空列表
for x in range(WIDTH): # 列数据
column = []
for y in range(HEIGHT): # 行数据
if random.randint(0, 1) == 0:
column.append('#') # 添加一个活细胞
else:
column.append(' ') # 添加一个死细胞
nextCells.append(column)
stage = 0
while True:
print(f'-------------------------------------------------------------{stage}')
currentCells = copy.deepcopy(nextCells) # 复制列表(使用copy.deepcopy()可以复制列表中的列表)
for y in range(HEIGHT): # 遍历每一行数据(y坐标方向)
for x in range(WIDTH): # 遍历每一列数据(x坐标方向)
print(currentCells[x][y], end='') # 不换行打印每个位置的细胞生死情况
print() # 开启新的一行,不打印任何内容
for y in range(HEIGHT):
for x in range(WIDTH):
leftCoord = (x - 1) % WIDTH # 左侧
rightCoord = (x + 1) % WIDTH # 右侧
aboveCoord = (y - 1) % HEIGHT # 上侧
belowCoord = (y + 1) % HEIGHT # 下侧
'''
对周围8个位置进行计算时使用 % 取模运算
% 取模运算符实现环绕,最左边的0列中的细胞,左邻居是0-1,即-1(最后一个索引),要将它环绕到最右边一列的索引59上,我们计算(0-1)% WIDTH
由于WIFTH 为60,因此该表达式的计算结果为59
同理,将最上行数据和最下行数据进行连接,最左边列数据和最右列数据进行连接
'''
# 对周围8个位置的状态进行判断并统计有多少个细胞是活的
numNeighbors = 0 # 统计周围活细胞的个数
if currentCells[leftCoord][aboveCoord] == '#': # 左上细胞状态
numNeighbors += 1
if currentCells[x][aboveCoord] == '#': # 上细胞状态
numNeighbors += 1
if currentCells[rightCoord][aboveCoord] == '#': # 右上细胞状态
numNeighbors += 1
if currentCells[leftCoord][y] == '#': # 左细胞状态
numNeighbors += 1
if currentCells[leftCoord][belowCoord] == '#': # 左下细胞状态
numNeighbors += 1
if currentCells[x][belowCoord] == '#': # 下细胞状态
numNeighbors += 1
if currentCells[rightCoord][belowCoord] == '#': # 右下细胞状态
numNeighbors += 1
# 细胞的下一步状态进行判断
if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
nextCells[x][y] = '#'
elif currentCells[x][y] == ' ' and numNeighbors == 3:
nextCells[x][y] = '#'
else:
nextCells[x][y] = ' '
stage += 1 # 标记进化次数
time.sleep(2) # 等待2s
修改程序2:
实现每个位置的单独输出
# Conway的生命游戏
import copy # 导入模块
import random
import time
WIDTH = 60
HEIGHT = 20
# 创建于一个列表的列表数据结构,用来存储代表活细胞和死细胞的’#‘和’ ‘字符串,他们在列表中的位置反映了他们在屏幕上的位置
nextCells = [] # 创建空列表
for x in range(WIDTH): # 列数据
column = []
for y in range(HEIGHT): # 行数据
if random.randint(0, 1) == 0:
column.append('#') # 添加一个活细胞
else:
column.append(' ') # 添加一个死细胞
nextCells.append(column)
stage = 0
print(f'-------------------------------------------------------------{stage}')
for y in range(HEIGHT): # 遍历每一行数据(y坐标方向)
for x in range(WIDTH): # 遍历每一列数据(x坐标方向)
print(nextCells[x][y], end='') # 不换行打印每个位置的细胞生死情况
print() # 开启新的一行,不打印任何内容
while True:
stage += 1 # 标记进化次数
print()
print(f'-------------------------------------------------------------{stage}')
currentCells = copy.deepcopy(nextCells) # 复制列表(使用copy.deepcopy()可以复制列表中的列表)
for y in range(HEIGHT):
print()
for x in range(WIDTH):
leftCoord = (x - 1) % WIDTH # 左侧
rightCoord = (x + 1) % WIDTH # 右侧
aboveCoord = (y - 1) % HEIGHT # 上侧
belowCoord = (y + 1) % HEIGHT # 下侧
'''
对周围8个位置进行计算时使用 % 取模运算
% 取模运算符实现环绕,最左边的0列中的细胞,左邻居是0-1,即-1(最后一个索引),要将它环绕到最右边一列的索引59上,我们计算(0-1)% WIDTH
由于WIFTH 为60,因此该表达式的计算结果为59
同理,将最上行数据和最下行数据进行连接,最左边列数据和最右列数据进行连接
'''
# 对周围8个位置的状态进行判断并统计有多少个细胞是活的
numNeighbors = 0 # 统计活细胞的个数
if currentCells[leftCoord][aboveCoord] == '#': # 左上细胞状态
numNeighbors += 1
if currentCells[x][aboveCoord] == '#': # 上细胞状态
numNeighbors += 1
if currentCells[rightCoord][aboveCoord] == '#': # 右上细胞状态
numNeighbors += 1
if currentCells[leftCoord][y] == '#': # 左细胞状态
numNeighbors += 1
if currentCells[leftCoord][belowCoord] == '#': # 左下细胞状态
numNeighbors += 1
if currentCells[x][belowCoord] == '#': # 下细胞状态
numNeighbors += 1
if currentCells[rightCoord][belowCoord] == '#': # 右下细胞状态
numNeighbors += 1
# 细胞的下一步状态进行判断
if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
nextCells[x][y] = '#'
elif currentCells[x][y] == ' ' and numNeighbors == 3:
nextCells[x][y] = '#'
else:
nextCells[x][y] = ' '
print(nextCells[x][y], end='')
time.sleep(0.01)