Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the Game of Life with python

Shulou Source: shulou.com Published: 2022-05-31 11:19:56 10月01日 Update

This article mainly explains "how to realize the Game of Life in python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to realize the Game of Life with python".

The code is as follows:

#-*-coding:utf-8-*-

Fromtimeimportsleep

Fromccopyimportdeepcopy

World _ height = 2mm World length

World range = 4mm World width

Alive _ CON=3# resurrection conditions

KEEP_CON=2# retention condition

Class unit (object):

'Cellular object'

Def__init__ (self,pos):

"its own coordinates x ~ (th) y, whether it is still alive or not"

Egos, ego is alive = position, false

Self.x,self.y=self.point

DefsetAlive (self):

The self is alive = true

DefsetDied (self):

Self.is_alive=False

Defdisplay (self):

Ifself.is_alive:

Return'*'

Return ""

DefdisplayLinux (self):

Black and white blocks can be printed under the Linux operating system.

Ifself.is_alive:

Return '033 [0trit37ter47m\ 033 [0m']

Return '033 [0trit30ter40m\ 033 [0m']

Class Game Manager (object):

Def__init__ (self):

Self.world=self.initWorld ()

Self.initAliveCell ()

Define the network (self):

World= []

Pos _ xinxrange (WORld _ WIDE):

Column = []

Suitable for operating system scope (world high):

Column.append (cell ((pos_x,pos_y)

World.append (column)

Return to the world

DefinitAliveCell (self):

Fromrandomimportchoice

have a good opinion of oneself. World 3:

Forcellinhigh:

Ifchoice ((0jue 1)) = = 0:

Go ahead

Cell.setAlive ()

Defget neighbor (self,cell_obj):

Live_count=0

Forx _ ofexrange (- 1Pol 2):

Fory _ of exrange (- 1Pol 2):

Cymbals obj.y cells obj.x xbooks of the cell _ cymbal y _ of

If ((cymx.centery) = = cell_obj.point) or\

(c_x0orc_x=WORLD_WIDE) or\

(c_y0orc_y=WORLD_HIGH):

"exclude yourself and the points that cross the line."

Go ahead

Ifself.world[c _ x] [c_y] .is _ alive:

Live_count=1

Returnalive _ count

Defdisplay (self):

Print'='* World wide Web # equal sign division line

For dexinxrange (World _ High):

Print "". Add ([high [index]). Displays Linux () for highleinself. World])

Print'='* World wide Web

DefgameStart (self):

WhileTrue:

Self.display ()

New_world=deepcopy (self.world)

Forp_x,wide _ listinenumerate (self. World):

Forp_y,_ in numerate (wide _ list):

Current _ cell=new _ world [p _ x] [p _ y]

Nei _ num=self . Get neighbor (current cell)

Ifnei_num==ALIVE_CON:

Current _ cell.setAlive ()

Elifnei_num! = KEEP_CON:

Current_cell.setDied ()

Self.world=new_world

Sleep (0.2)

If__name__=='__main__':

World=GameManager ()

Try:

World.gameStart ()

Keyboard interrupt: exception

'prevent copy exit from reporting an error'

At this point, I believe you have a deeper understanding of "python how to achieve the game of life". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: World Life operating system Unit object system World wide Web content condition scope neighbor World wide Web Learning practicality Deep self-esteem Code location interest Segmentation Line Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux OPPO Reno vpn Shulou Technology Redmi