国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現生命游戲的示例代碼(Game of Life)

2020-02-22 22:58:38
字體:
來源:轉載
供稿:網友

生命游戲的算法就不多解釋了,百度一下介紹隨處可見。

因為網上大多數版本都是基于pygame,matlab等外部庫實現的,二維數組大多是用numpy,使用起來學習成本比較高,所以閑暇之余寫一個不用外部依賴庫,console輸出的版本。

# -*- coding: utf-8 -*- from time import sleep from copy import deepcopy  WORLD_HIGH = 20 #世界長度 WORLD_WIDE = 40 #世界寬度 ALIVE_CON = 3 #復活條件 KEEP_CON = 2 #保有條件  class Cell(object):   '''''細胞對象'''   def __init__(self, pos):     '''''自身坐標x,y, 已經是否還存活'''     self.point, self.is_alive = pos, False     self.x, self.y = self.point      def setAlive(self):     self.is_alive = True        def setDied(self):     self.is_alive = False        def display(self):     if self.is_alive:       return '*'     return ' '        def displayLinux(self):     '''''在linux環境下可以打印黑白塊'''     if self.is_alive:       return '/033[0;37;47m /033[0m'     return '/033[0;30;40m /033[0m'      class GameManager(object):   def __init__(self):     self.world = self.initWorld()     self.initAliveCell()       def initWorld(self):     world = []     for pos_x in xrange(WORLD_WIDE):       column = []       for pos_y in xrange(WORLD_HIGH):         column.append(Cell((pos_x, pos_y)))       world.append(column)     return world      def initAliveCell(self):     from random import choice     for high in self.world:       for cell in high:         if choice((0, 1)) == 0:           continue         cell.setAlive()      def getNeighbours(self, cell_obj):     alive_count = 0     for x_of in xrange(-1, 2):       for y_of in xrange(-1, 2):         c_x, c_y = cell_obj.x + x_of, cell_obj.y + y_of         if ((c_x, c_y) == cell_obj.point) or /           (c_x < 0 or c_x >= WORLD_WIDE) or /           (c_y < 0 or c_y >= WORLD_HIGH):           '''''排除自身和越界的點'''           continue         if self.world[c_x][c_y].is_alive:           alive_count += 1     return alive_count           def display(self):     print '='*WORLD_WIDE #等號分割線     for index in xrange(WORLD_HIGH):       print ''.join([high[index].displayLinux() for high in self.world])     print '='*WORLD_WIDE    def gameStart(self):     while True:       self.display()       new_world = deepcopy(self.world)       for p_x, wide_list in enumerate(self.world):         for p_y, _ in enumerate(wide_list):           current_cell = new_world[p_x][p_y]           nei_num = self.getNeighbours(current_cell)           if nei_num == ALIVE_CON:             current_cell.setAlive()           elif nei_num != KEEP_CON:             current_cell.setDied()              self.world = new_world       sleep(0.2)  if __name__ == '__main__':   world = GameManager()   try:     world.gameStart()   except KeyboardInterrupt:     '''''防止ctrl+c退出報錯'''     pass 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 马边| 镇平县| 开阳县| 会理县| 凤庆县| 尼木县| 长宁县| 定远县| 成都市| 玛曲县| 体育| 武夷山市| 贡山| 江孜县| 新邵县| 土默特左旗| 南投县| 黄骅市| 怀集县| 阜宁县| 晋中市| 河源市| 江永县| 静海县| 唐河县| 海门市| 潼南县| 工布江达县| 余庆县| 皮山县| 南投县| 井研县| 岳阳县| 明水县| 长汀县| 阿克苏市| 邵武市| 正安县| 德阳市| 西林县| 青田县|