Python案例:协程实现康威生命游戏,元胞自动机

Posted 石天放

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python案例:协程实现康威生命游戏,元胞自动机相关的知识,希望对你有一定的参考价值。

from collections import namedtuple
import os,time
import random

Query=namedtuple(‘Query‘,(‘y‘,‘x‘))
Transition=namedtuple(‘Transition‘,(‘y‘,‘x‘,‘state‘))

ALIVE="*"
EMPTY="-"

def count_neighbors(y,x):
    n_=yield Query(y+1,x+0)
    ne=yield Query(y+1,x+1)
    e_=yield Query(y+0,x+1)
    se=yield Query(y-1,x+1)
    s_=yield Query(y-1,x+0)
    sw=yield Query(y-1,x-1)
    w_=yield Query(y+0,x-1)
    nw=yield Query(y+1,x-1)

    neighbor_states=[n_,ne,e_,se,s_,sw,w_,nw]
    count=0
    
    for state in neighbor_states:
        if state==ALIVE:
            count+=1
    return count

def game_logic(state,neighbors):
    if state==ALIVE:
        if neighbors<2:
            return EMPTY
        elif neighbors>3:
            return EMPTY
    else:
        if neighbors==3:
            return ALIVE
    return state

def step_cell(y,x):
    state=yield Query(y,x)
    neighbors=yield from count_neighbors(y,x)
    next_state=game_logic(state,neighbors)
    yield Transition(y,x,next_state)

TICK=object()

def simulate(height,width):
    while True:
        for y in range(height):
            for x in range(width):
                #推进每个格子的演化
                yield from step_cell(y,x)
        yield TICK

class Grid(object):
    def __init__(self,height,width):
        self.height=height
        self.width=width
        self.rows=[]
        for _ in range(self.height):
            self.rows.append([EMPTY]*self.width)

    def __str__(self):
        return "".join(["".join(row)+"
" for row in self.rows])

    def query(self,y,x):
        return self.rows[y%self.height][x%self.width]
    
    def assgin(self,y,x,state):
        self.rows[y%self.height][x%self.width]=state

def live_a_generation(grid,sim):
    progeny=Grid(grid.height,grid.width)
    item=next(sim)
    while item is not TICK:
        if isinstance(item,Query):
            state=grid.query(item.y,item.x)
            item=sim.send(state)
        else:
            progeny.assgin(item.y,item.x,item.state)
            item=next(sim)
    return progeny

grid=Grid(50,50)
# grid.assgin(0,3,ALIVE)
# grid.assgin(1,5,ALIVE)
# grid.assgin(2,3,ALIVE)
# grid.assgin(2,4,ALIVE)
# grid.assgin(2,5,ALIVE)

#随机生成地图
for i in range(random.randint(20,30)):
    y=random.randint(0,49)
    x=random.randint(0,49)
    if random.randint(0,1):
        for i in range(random.randint(1,3)):
            rX=random.randint(-1,1)
            rY=random.randint(-1,1)
            grid.assgin(y+rY,x+rX,ALIVE)
    grid.assgin(y,x,ALIVE)

# class ColumnPrinter(object):
#     def __init__():
        

#columns=ColumnPrinter()
sim=simulate(grid.height,grid.width)
for i in range(30):
    print(str(grid))
    time.sleep(1)
    os.system(‘cls‘)
    grid=live_a_generation(grid,sim)
#print(columns)

以上是关于Python案例:协程实现康威生命游戏,元胞自动机的主要内容,如果未能解决你的问题,请参考以下文章

生命游戏的三种玩法

元胞自动机基于matlab元胞自动机双通道交通含Matlab源码 1657期

元胞自动机基于matlab元胞自动机双边教室疏散含Matlab源码 1208期

元胞自动机基于matlab元胞自动机单边教室疏散含Matlab源码 1207期

元胞自动机基于matlab界面聚合元胞自动机模拟含Matlab源码 2004期

元胞自动机基于matlab元胞自动机交通事故通行含Matlab源码 175期