康威的生命游戏Python版

"""
生存:周围存活数为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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值