用户:PTS_Ron暂退查看:0 回复:1 评论:0 创建时间:2020-04-26T09:51:51
#!/usr/bin/env python3 # -*- coding: utf-8 -*-
''' Game of life World author: Pleiades '''
#import os import random
LIVE = '#' DEAD = ' '
class GameOfLifeWorld:
width = 100 height = 100 cells = []
def__init__(self, width, height): self.width = width self.height = height
def InitRandom(self): self.cells = [[LIVE if random.random() > 0.8 else DEAD for i in range(self.width)] for j inrange(self.height)]
def TryGetCell(self, h, w): return self.cells[min(h, self.height - 1)][min(w, self.width - 1)]
def GetNearbyCellsCount(self, h, w): nearby = [self.TryGetCell(h + dy, w + dx) for dx in [-1, 0, 1] for dy in[-1,0,1]ifnot(dx == 0and dy == 0)] returnlen(list(filter(lambda x: x == LIVE, nearby)))
def GetNewCell(self, h, w): count = self.GetNearbyCellsCount(h, w) return LIVE if count == 3else(DEAD if count < 2or count > 3else self.cells[h][w])
def Update(self): self.cells = [[self.GetNewCell(h, w) for w in range(self.width)] for h inrange(self.height)]
if __name__ == "__main__": w = 10 h = 10 world = GameOfLifeWorld(w, h) world.InitRandom() print('\n'.join(' '.join(line)for line in world.cells)) world.Update() print('\n'.join(' '.join(line)for line in world.cells))