menu Andy's Blog
more_vert
返校前夕的Python元胞自动机
2020-04-26 | 科学,日常 | 暂无评论 | 1676 次阅读 | 8字
Andy
author

emm,不说废话,上源码,看。

import time

def print_seq(seq, speed=0.5):
    for item in seq:
        if item:
            print('*', end='')
        else:
            print('-', end='')
    print('')
    time.sleep(speed)


class Cell:
    def __init__(self, deepth=31):
        self.ca = [0 if i != 31 else 1 for i in range(64)]
        self.ca_new = []
        self.deepth = deepth

    def process(self):
        print_seq(self.ca)
        for i in range(self.deepth):
            self._rule()
            print_seq(self.ca_new)
            self.ca = self.ca_new
            self.ca_new = []

    def _rule(self):
        for i in range(64):
            if 0 < i < 63:
                if self.ca[i - 1] == self.ca[i + 1]:
                    self.ca_new.append(0)
                else:
                    self.ca_new.append(1)
            elif i == 0:
                if self.ca[1]:
                    self.ca_new.append(1)
                else:
                    self.ca_new.append(0)
            else:
                if self.ca[62]:
                    self.ca_new.append(1)
                else:
                    self.ca_new.append(0)

def main():
    cell = Cell()
    cell.process()


if name == '__main__':
    main()
发表评论
暂无评论
textsms
account_circle
email
link