黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

現(xiàn)代算法可視化(一)、python實(shí)現(xiàn)生命游戲(元胞自動(dòng)機(jī))

系統(tǒng) 1907 0

效果展示

規(guī)則介紹

基于二維元胞自動(dòng)機(jī)的生命游戲可視化實(shí)現(xiàn)
規(guī)則:藍(lán)色方塊代表生命

  1. 有顏色的方塊代表生命,無(wú)顏色的方塊代表死亡
  2. 一個(gè)細(xì)胞周圍有八個(gè)細(xì)胞,對(duì)于一個(gè)活細(xì)胞來(lái)說(shuō),如果它周圍的八個(gè)細(xì)胞中:
    如果只有一個(gè)或沒(méi)有一個(gè)是活的,那這個(gè)細(xì)胞就會(huì)死亡
    如果其中兩個(gè)或者三個(gè)細(xì)胞是活的,那這個(gè)細(xì)胞就能維持生命
    如果超過(guò)3個(gè)細(xì)胞是活著的,那這個(gè)細(xì)胞就會(huì)因?yàn)檫^(guò)于擁擠而死亡
  3. 對(duì)于一個(gè)死細(xì)胞來(lái)說(shuō),如果這個(gè)細(xì)胞周圍如果有三個(gè)細(xì)胞存活,該將獲得新生。

附:所有規(guī)則都可以修改,種群初始密度也可以修改。本實(shí)驗(yàn)采用初始化人口隨機(jī)分布,也可以自定義初始化人口分布以及網(wǎng)格大小得到滑翔機(jī),播種機(jī)等復(fù)雜美觀圖形。

源碼

            
              
                ''' demoName:life simulation game principle: cellular automata (two dimensional) the rule of the game : 1. colored cell represents life, uncolored cell represents death 2. a cell has eight cells around it, to a live cell: 2.1 if only one or none of them are live, the cell will die 2.2 if two or three of them are live, the cell will sustain life 2.4 if more than three of them are live, the cell will die 3. to a dead cell, if there are three alive around it ,the cell will gain life '''
              
              
                import
              
               pygame

              
                import
              
               sys 

              
                import
              
               math 

              
                import
              
               random

              
                import
              
               copy

pygame
              
                .
              
              init
              
                (
              
              
                )
              
              
chess_number
              
                =
              
              
                60
              
              
                # 設(shè)置棋盤大小
              
              
LIVEDENSITY
              
                =
              
              
                0.3
              
              
                #生命密度
              
              
TICK
              
                =
              
              
                10
              
              
                #幀數(shù)
              
              
BG
              
                =
              
              
                (
              
              
                20
              
              
                ,
              
              
                20
              
              
                ,
              
              
                20
              
              
                )
              
              
                #背景色
              
              
LINECOLOR
              
                =
              
              
                (
              
              
                52
              
              
                ,
              
              
                53
              
              
                ,
              
              
                46
              
              
                )
              
              
                #網(wǎng)格色
              
              
LIFECOLOR
              
                =
              
              
                (
              
              
                31
              
              
                ,
              
              
                97
              
              
                ,
              
              
                189
              
              
                )
              
              
                #活細(xì)胞的顏色
              
              
CELL_LENGTH
              
                =
              
              
                int
              
              
                (
              
              
                600
              
              
                /
              
              chess_number
              
                )
              
              
                #每個(gè)格子的像素大小
              
              
LINE_WIDTH
              
                =
              
              
                4
              
              
                #線的寬度
              
              
START_POSX
              
                =
              
              
                50
              
              
START_POSY
              
                =
              
              
                50
              
              
                #239,60,57 紅色 79,167,47綠色 188,69,229 紫色
              
              
                #224,90,9 橙色 252,61,63大紅
              
              
                #188,69,229 紫色 239,60,57 紅
              
              
                #39,202,149 天綠 31,97,189藍(lán)色 22,178,243 天藍(lán)
              
              
                # 設(shè)置背景框大小
              
              
size 
              
                =
              
               width
              
                ,
              
               height 
              
                =
              
              
                2
              
              
                *
              
              START_POSX
              
                +
              
              chess_number
              
                *
              
              CELL_LENGTH
              
                ,
              
              
                2
              
              
                *
              
              START_POSY
              
                +
              
              chess_number
              
                *
              
              CELL_LENGTH

              
                # 設(shè)置幀率,返回clock 類
              
              
clock 
              
                =
              
               pygame
              
                .
              
              time
              
                .
              
              Clock
              
                (
              
              
                )
              
              
screen 
              
                =
              
               pygame
              
                .
              
              display
              
                .
              
              set_mode
              
                (
              
              size
              
                )
              
              
pygame
              
                .
              
              display
              
                .
              
              set_caption
              
                (
              
              
                "Accelerator made"
              
              
                )
              
              
                #畫
              
              
                def
              
              
                draw
              
              
                (
              
              livcell
              
                )
              
              
                :
              
              
                ''' 進(jìn)行一次繪畫操作,可以理解為進(jìn)行一幀操作所更新的畫面 param:操作前活細(xì)胞圖 return:操作后活細(xì)胞圖 '''
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              chess_number
              
                +
              
              
                1
              
              
                )
              
              
                :
              
              
		pygame
              
                .
              
              draw
              
                .
              
              line
              
                (
              
              screen
              
                ,
              
               LINECOLOR
              
                ,
              
              
                (
              
              START_POSX
              
                ,
              
              START_POSY
              
                +
              
              i
              
                *
              
              CELL_LENGTH
              
                )
              
              
                ,
              
              
                (
              
              START_POSX
              
                +
              
              chess_number
              
                *
              
              CELL_LENGTH
              
                ,
              
              START_POSY
              
                +
              
              i
              
                *
              
              CELL_LENGTH
              
                )
              
              
                ,
              
               LINE_WIDTH
              
                )
              
              
                #橫線
              
              
		pygame
              
                .
              
              draw
              
                .
              
              line
              
                (
              
              screen
              
                ,
              
               LINECOLOR
              
                ,
              
              
                (
              
              START_POSX
              
                +
              
              i
              
                *
              
              CELL_LENGTH
              
                ,
              
              START_POSY
              
                )
              
              
                ,
              
              
                (
              
              START_POSX
              
                +
              
              i
              
                *
              
              CELL_LENGTH
              
                ,
              
              START_POSY
              
                +
              
              chess_number
              
                *
              
              CELL_LENGTH
              
                )
              
              
                ,
              
               LINE_WIDTH
              
                )
              
              
                #豎線#
              
              
                #畫活細(xì)胞
              
              
	livcell
              
                =
              
              rule
              
                (
              
              livcell
              
                )
              
              
                print
              
              
                (
              
              
                'drawnew'
              
              
                ,
              
              livcell
              
                )
              
              
                return
              
               livcell


              
                def
              
              
                drawcell
              
              
                (
              
              i
              
                ,
              
              j
              
                ,
              
              cellkind
              
                )
              
              
                :
              
              
                ''' 畫出一個(gè)具體的方塊 param:行,列,方塊顏色種類 '''
              
              
	pygame
              
                .
              
              draw
              
                .
              
              rect
              
                (
              
              screen
              
                ,
              
              cellkind
              
                ,
              
              
                [
              
              START_POSX
              
                +
              
              CELL_LENGTH
              
                *
              
              j
              
                +
              
              
                (
              
              LINE_WIDTH
              
                -
              
              
                1
              
              
                )
              
              
                ,
              
              START_POSY
              
                +
              
              CELL_LENGTH
              
                *
              
              i
              
                +
              
              
                (
              
              LINE_WIDTH
              
                -
              
              
                1
              
              
                )
              
              
                ,
              
              CELL_LENGTH
              
                -
              
              LINE_WIDTH
              
                ,
              
              CELL_LENGTH
              
                -
              
              LINE_WIDTH
              
                ]
              
              
                ,
              
              
                0
              
              
                )
              
              
                #終點(diǎn). Rect(left,top,width,height)
              
              
                def
              
              
                creatlife
              
              
                (
              
              density
              
                )
              
              
                :
              
              
                ''' 在初始狀態(tài)下創(chuàng)造生命 param:所要求生成生命細(xì)胞的密度 return:初始生命細(xì)胞的位置圖 '''
              
              
	livcell
              
                =
              
              
                [
              
              
                [
              
              
                0
              
              
                ]
              
              
                *
              
              chess_number 
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              chess_number
              
                )
              
              
                ]
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              chess_number
              
                )
              
              
                :
              
              
                for
              
               j 
              
                in
              
              
                range
              
              
                (
              
              chess_number
              
                )
              
              
                :
              
              
			pwall
              
                =
              
              random
              
                .
              
              random
              
                (
              
              
                )
              
              
                if
              
               pwall
              
                <
              
              density
              
                :
              
              
				livcell
              
                [
              
              i
              
                ]
              
              
                [
              
              j
              
                ]
              
              
                =
              
              
                1
              
              
                return
              
               livcell
	

              
                def
              
              
                neighborcell
              
              
                (
              
              pos
              
                )
              
              
                :
              
              
                ''' 獲得一個(gè)細(xì)胞周圍的細(xì)胞位置,并且存入數(shù)組 param:細(xì)胞的位置 return:這個(gè)細(xì)胞所有的鄰居細(xì)胞 '''
              
              
	neighborList
              
                =
              
              
                [
              
              
                ]
              
              
	x
              
                =
              
              pos
              
                [
              
              
                0
              
              
                ]
              
              
	y
              
                =
              
              pos
              
                [
              
              
                1
              
              
                ]
              
              
	neighborList
              
                =
              
              
                [
              
              
                [
              
              x
              
                -
              
              
                1
              
              
                ,
              
              y
              
                -
              
              
                1
              
              
                ]
              
              
                ,
              
              
                [
              
              x
              
                -
              
              
                1
              
              
                ,
              
              y
              
                ]
              
              
                ,
              
              
                [
              
              x
              
                -
              
              
                1
              
              
                ,
              
              y
              
                +
              
              
                1
              
              
                ]
              
              
                ,
              
              
                [
              
              x
              
                ,
              
              y
              
                -
              
              
                1
              
              
                ]
              
              
                ,
              
              
                [
              
              x
              
                ,
              
              y
              
                +
              
              
                1
              
              
                ]
              
              
                ,
              
              
                [
              
              x
              
                +
              
              
                1
              
              
                ,
              
              y
              
                -
              
              
                1
              
              
                ]
              
              
                ,
              
              
                [
              
              x
              
                +
              
              
                1
              
              
                ,
              
              y
              
                ]
              
              
                ,
              
              
                [
              
              x
              
                +
              
              
                1
              
              
                ,
              
              y
              
                +
              
              
                1
              
              
                ]
              
              
                ]
              
              
	realnList
              
                =
              
              copy
              
                .
              
              deepcopy
              
                (
              
              neighborList
              
                )
              
              
                for
              
               i 
              
                in
              
               neighborList
              
                :
              
              
                if
              
               i
              
                [
              
              
                0
              
              
                ]
              
              
                <
              
              
                0
              
              
                or
              
               i
              
                [
              
              
                0
              
              
                ]
              
              
                >
              
              chess_number
              
                -
              
              
                1
              
              
                or
              
               i
              
                [
              
              
                1
              
              
                ]
              
              
                <
              
              
                0
              
              
                or
              
               i
              
                [
              
              
                1
              
              
                ]
              
              
                >
              
              chess_number
              
                -
              
              
                1
              
              
                :
              
              
			realnList
              
                .
              
              remove
              
                (
              
              i
              
                )
              
              
                return
              
               realnList
	

              
                def
              
              
                rule
              
              
                (
              
              livcell
              
                )
              
              
                :
              
              
                ''' 制定生命游戲的游戲規(guī)則 param:一次操作前所有活著細(xì)胞的位置 return:一次操作后所有活著細(xì)胞的位置 '''
              
              
	newlivcell
              
                =
              
              copy
              
                .
              
              deepcopy
              
                (
              
              livcell
              
                )
              
              
                print
              
              
                (
              
              
                'livcell'
              
              
                ,
              
              livcell
              
                )
              
              
                for
              
               i 
              
                in
              
              
                range
              
              
                (
              
              chess_number
              
                )
              
              
                :
              
              
                for
              
               j 
              
                in
              
              
                range
              
              
                (
              
              chess_number
              
                )
              
              
                :
              
              
                if
              
               livcell
              
                [
              
              i
              
                ]
              
              
                [
              
              j
              
                ]
              
              
                ==
              
              
                1
              
              
                :
              
              
					drawcell
              
                (
              
              i
              
                ,
              
              j
              
                ,
              
              LIFECOLOR
              
                )
              
              
					alive
              
                =
              
              
                0
              
              
                for
              
               cell 
              
                in
              
               neighborcell
              
                (
              
              
                [
              
              i
              
                ,
              
              j
              
                ]
              
              
                )
              
              
                :
              
              
                if
              
               livcell
              
                [
              
              cell
              
                [
              
              
                0
              
              
                ]
              
              
                ]
              
              
                [
              
              cell
              
                [
              
              
                1
              
              
                ]
              
              
                ]
              
              
                ==
              
              
                1
              
              
                :
              
              
							alive
              
                +=
              
              
                1
              
              
                if
              
               alive
              
                ==
              
              
                0
              
              
                or
              
               alive
              
                ==
              
              
                1
              
              
                :
              
              
						newlivcell
              
                [
              
              i
              
                ]
              
              
                [
              
              j
              
                ]
              
              
                =
              
              
                0
              
              
                elif
              
               alive
              
                ==
              
              
                2
              
              
                or
              
               alive
              
                ==
              
              
                3
              
              
                :
              
              
						newlivcell
              
                [
              
              i
              
                ]
              
              
                [
              
              j
              
                ]
              
              
                =
              
              
                1
              
              
                else
              
              
                :
              
              
						newlivcell
              
                [
              
              i
              
                ]
              
              
                [
              
              j
              
                ]
              
              
                =
              
              
                0
              
              
                else
              
              
                :
              
              
					alive
              
                =
              
              
                0
              
              
                for
              
               cell 
              
                in
              
               neighborcell
              
                (
              
              
                [
              
              i
              
                ,
              
              j
              
                ]
              
              
                )
              
              
                :
              
              
                if
              
               livcell
              
                [
              
              cell
              
                [
              
              
                0
              
              
                ]
              
              
                ]
              
              
                [
              
              cell
              
                [
              
              
                1
              
              
                ]
              
              
                ]
              
              
                ==
              
              
                1
              
              
                :
              
              
							alive
              
                +=
              
              
                1
              
              
                if
              
               alive
              
                ==
              
              
                3
              
              
                :
              
              
						newlivcell
              
                [
              
              i
              
                ]
              
              
                [
              
              j
              
                ]
              
              
                =
              
              
                1
              
              
                return
              
               newlivcell
					

              
                def
              
              
                main
              
              
                (
              
              
                )
              
              
                :
              
              
	livcell
              
                =
              
              creatlife
              
                (
              
              LIVEDENSITY
              
                )
              
              
                while
              
              
                True
              
              
                :
              
              
                for
              
               event 
              
                in
              
               pygame
              
                .
              
              event
              
                .
              
              get
              
                (
              
              
                )
              
              
                :
              
              
                # 查找關(guān)閉窗口事件
              
              
                if
              
               event
              
                .
              
              
                type
              
              
                ==
              
               pygame
              
                .
              
              QUIT
              
                :
              
              
				sys
              
                .
              
              exit
              
                (
              
              
                )
              
              
                # 填充背景色
              
              
		screen
              
                .
              
              fill
              
                (
              
              BG
              
                )
              
              
		livcell
              
                =
              
              draw
              
                (
              
              livcell
              
                )
              
              
                # 刷新圖s
              
              
		pygame
              
                .
              
              display
              
                .
              
              flip
              
                (
              
              
                )
              
              
		clock
              
                .
              
              tick
              
                (
              
              TICK
              
                )
              
              
                if
              
               __name__
              
                ==
              
              
                "__main__"
              
              
                :
              
              
	main
              
                (
              
              
                )
              
            
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!??!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論