python實(shí)現(xiàn)生命游戲的示例代碼(Game of Life)
生命游戲的算法就不多解釋了,百度一下介紹隨處可見(jiàn)。
因?yàn)榫W(wǎng)上大多數(shù)版本都是基于pygame,matlab等外部庫(kù)實(shí)現(xiàn)的,二維數(shù)組大多是用numpy,使用起來(lái)學(xué)習(xí)成本比較高,所以閑暇之余寫(xiě)一個(gè)不用外部依賴庫(kù),console輸出的版本。
# -*- coding: utf-8 -*- from time import sleep from copy import deepcopy WORLD_HIGH = 20 #世界長(zhǎng)度 WORLD_WIDE = 40 #世界寬度 ALIVE_CON = 3 #復(fù)活條件 KEEP_CON = 2 #保有條件 class Cell(object): '''''細(xì)胞對(duì)象''' def __init__(self, pos): '''''自身坐標(biāo)x,y, 已經(jīng)是否還存活''' 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環(huán)境下可以打印黑白塊''' 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): '''''排除自身和越界的點(diǎn)''' continue if self.world[c_x][c_y].is_alive: alive_count += 1 return alive_count def display(self): print '='*WORLD_WIDE #等號(hào)分割線 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退出報(bào)錯(cuò)''' pass
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python神經(jīng)網(wǎng)絡(luò)TensorFlow基于CNN卷積識(shí)別手寫(xiě)數(shù)字
這篇文章主要介紹了Python神經(jīng)網(wǎng)絡(luò)TensorFlow基于CNN卷積識(shí)別手寫(xiě)數(shù)字的實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10解決Python訪問(wèn)MySQL數(shù)據(jù)庫(kù)速度慢的問(wèn)題
這篇文章主要介紹了解決Python訪問(wèn)MySQL數(shù)據(jù)庫(kù)速度慢的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04使用Python操作MySQL數(shù)據(jù)庫(kù)的教程詳解
在這篇文章中,主要為大家詳細(xì)介紹如何在Python中使用pymysql模塊來(lái)操作MySQL數(shù)據(jù)庫(kù),文中的示例代碼簡(jiǎn)介易懂,需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-07-07tensorflow中tf.slice和tf.gather切片函數(shù)的使用
今天小編就為大家分享一篇tensorflow中tf.slice和tf.gather切片函數(shù)的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01python模塊itsdangerous簡(jiǎn)單介紹
這篇文章主要介紹了python模塊itsdangerous簡(jiǎn)單介紹,本文通過(guò)案例分析給大家詳細(xì)講解,對(duì)python模塊itsdangerous相關(guān)知識(shí)感興趣的朋友一起看看吧2022-11-11Python可視化Matplotlib散點(diǎn)圖scatter()用法詳解
這篇文章主要介紹了Python可視化中Matplotlib散點(diǎn)圖scatter()的用法詳解,文中附含詳細(xì)示例代碼,有需要得朋友可以借鑒參考下,希望能夠有所幫助2021-09-09pytorch中tensor張量數(shù)據(jù)類型的轉(zhuǎn)化方式
今天小編就為大家分享一篇pytorch中tensor張量數(shù)據(jù)類型的轉(zhuǎn)化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python利用itchat庫(kù)向好友或者公眾號(hào)發(fā)消息的實(shí)例
今天小編就為大家分享一篇Python利用itchat庫(kù)向好友或者公眾號(hào)發(fā)消息的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02