Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Game of Life with python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report