zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    linux ioctl
    pkg-config用法和gcc cflags
    boost noncopyable类
    google protobuf使用2
    跨平台编译CMake使用
    Linux epoll
    docker安装
    python 脚本转成exe可执行程序
    shell相关知识
    tcpdump使用
  • 原文地址:https://www.cnblogs.com/shitianfang/p/12616951.html
Copyright © 2011-2022 走看看