生命游戏二号

好久没有碰到有意思的计算机题了,昨天鑫哥提了一嘴,就去实现了

这是一个名叫"生命游戏"的小实验,每个方格就是个小细胞,如果细胞周围的伙伴有一个或没有,他们会因为过于孤独而死,如果周围的细胞大于3,则会因为过于拥挤而死亡;此外的情况细胞都能存活,甚至在三个伙伴时还能迎来新生。

不同的初始情况会有不同的迭代过程和结果,但是同样的初始情况一定会有相同的迭代过程和结果

在这样严苛的规则下有无数有意思的图案和方案等待探究

python 复制代码
import numpy as np
import matplotlib.pyplot as plt
import time

def initialize_grid(rows, cols, pattern='complex_pattern', position=None):
    """初始化网格,使用预定义的模式填充"""
    grid = np.zeros((rows, cols), dtype=int)

    def place_pattern(pattern, pos):
        patterns = {
            'block': [[1, 1], [1, 1]],
            'blinker': [[1, 1, 1]],
            'toad': [[0, 1, 1, 1], [1, 1, 1, 0]],
            'beacon': [[1, 1, 0, 0], [1, 1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 1]],
            'glider': [[0, 1, 0], [0, 0, 1], [1, 1, 1]],
            'symmetric': [
                [0, 1, 0, 1, 0],
                [1, 0, 1, 0, 1],
                [0, 1, 0, 1, 0],
                [1, 0, 1, 0, 1],
                [0, 1, 0, 1, 0]
            ],
            'complex_pattern': [
                [1, 1, 0, 0, 1, 1],
                [1, 0, 0, 0, 0, 1],
                [0, 0, 1, 1, 0, 0],
                [0, 0, 1, 1, 0, 0],
                [1, 0, 0, 0, 0, 1],
                [1, 1, 0, 0, 1, 1]
            ],
            'pulsar': [
                [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1],
                [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
                [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1],
                [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0],
            ]
        }

        if pattern in patterns:
            pat = np.array(patterns[pattern])
            r_offset, c_offset = pos[0] - pat.shape[0] // 2, pos[1] - pat.shape[1] // 2
            grid[r_offset:r_offset + pat.shape[0], c_offset:c_offset + pat.shape[1]] = pat

    if position is None:
        # 如果没有提供位置,则选择一个中心点
        position = (rows // 2, cols // 2)

    place_pattern(pattern, position)

    return grid

def update_grid(grid):
    """更新网格状态"""
    rows, cols = grid.shape
    new_grid = np.zeros((rows, cols), dtype=int)
    for r in range(rows):
        for c in range(cols):
            # 计算当前细胞周围的活细胞数
            neighbors = sum([
                grid[(r-1)%rows, (c-1)%cols], grid[(r-1)%rows, c], grid[(r-1)%rows, (c+1)%cols],
                grid[r, (c-1)%cols],                             grid[r, (c+1)%cols],
                grid[(r+1)%rows, (c-1)%cols], grid[(r+1)%rows, c], grid[(r+1)%rows, (c+1)%cols]
            ])
            # 生命游戏规则
            if grid[r, c] == 1 and (neighbors == 2 or neighbors == 3):
                new_grid[r, c] = 1
            elif grid[r, c] == 0 and neighbors == 3:
                new_grid[r, c] = 1
    return new_grid

def display_grid(grid):
    """显示网格状态"""
    plt.imshow(grid, cmap='binary')
    plt.axis('off')
    plt.show(block=False)
    plt.pause(0.2)
    plt.clf()

def run_game(rows=30, cols=30, generations=500):
    """运行生命游戏"""
    grid = initialize_grid(rows, cols, pattern='pulsar')
    for _ in range(generations):
        display_grid(grid)
        grid = update_grid(grid)

if __name__ == "__main__":
    run_game()
相关推荐
007_rbq3 小时前
XUnity.AutoTranslator-Gemini——调用Google的Gemini API, 实现Unity游戏中日文文本的自动翻译
人工智能·python·游戏·机器学习·unity·github·机器翻译
Sui_Network4 小时前
Sui 如何支持各种类型的 Web3 游戏
大数据·数据库·人工智能·游戏·web3·区块链
晴空了无痕6 小时前
游戏客户端架构设计与实战:从模块化到性能优化
游戏·性能优化
软件黑马王子12 小时前
Unity游戏制作中的C#基础(5)条件语句和循环语句知识点全解析
游戏·unity·c#
韩仔搭建2 天前
七星棋牌顶级运营产品全开源修复版源码教程:6端支持,200+子游戏玩法,完整搭建指南(含代码解析)
游戏·开源
Igallta_8136222 天前
【小游戏】C++控制台版本俄罗斯轮盘赌
c语言·开发语言·c++·windows·游戏·游戏程序
zsyzClb2 天前
nim游戏及其进阶 [SDOI2011] 黑白棋 [SDOI2019] 移动金币
游戏
BingLin-Liu2 天前
蓝桥杯备考:贪心算法之矩阵消除游戏
游戏·贪心算法·矩阵
264玫瑰资源库2 天前
七星棋牌源码高阶技术指南:6端互通、200+子游戏玩法深度剖析与企业级搭建实战(完全开源)
游戏·开源
TheFirst0084 天前
The First项目报告:重塑链上游戏生态,解读B3 Base的双赢局面
游戏