"""
生存:周围存活数为2或3,本格存活
死亡:周围存活数大于3,或周围存活数小于2,本格死亡
诞生:周围存活数为3,本格诞生
"""
import time
# 小数点是边框,英文逗号是没有,中文顿号是有
location = ['.............................',
'.,,,,,,,,,,,,,,,,,,,,,,,,,,,.',
'.,,,,,,,,,,,,,,,,,,,,,,,,,,,.',
'.,,,,,,,,,,,,,,,,,,,,,,,,,,,.',
'.,,,,,,,,,,,,···,,,,,,,,,,,,.',
'.,,,,,,,,,,,,·,,,,,,,,,,,,,,.',
'.,,,,,,,,,,,,··,,,,,,,,,,,,,.',
'.,,,,,,,,,,,,,,,,,,,,,,,,,,,.',
'.,,,,,,,,,,,,,,,,,,,,,,,,,,,.',
'.............................']
while True:
location_next = location.copy()
for y in range(len(location)):
for x in range(len(location[0])):
index_list = []
if location[y][x] == '.':
continue
index_list.extend([i for i in location[y - 1][x - 1:x + 2]]) # 上三个
index_list.extend([i for i in location[y + 1][x - 1:x + 2]]) # 下三个
index_list.append(location[y][x - 1]) # 左一个
index_list.append(location[y][x + 1]) # 右一个
if index_list.count('·') > 3 or index_list.count('·') < 2:
location_next[y] = location_next[y][0:x] + ',' + location_next[y][x+1:]
elif index_list.count('·') in (2, 3):
location_next[y] = location_next[y][0:x] + '·' + location_next[y][x+1:]
location = location_next.copy()
for i in location:
for j in i:
if j == '·':
print('\033[0;32m' + j + '\033[0m', end='')
else:
print(' ', end='')
print()
print()
time.sleep(1)
测试环境:python3.10,pycharm