码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-04-02 01:30:00      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:query   协程   system   pen   sleep   tuple   instance   column   tick   

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)+"\n" 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案例(一):协程实现康威生命游戏,元胞自动机

标签:query   协程   system   pen   sleep   tuple   instance   column   tick   

原文地址:https://www.cnblogs.com/shitianfang/p/12616951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!