Conway的生命游戏

主要内容

一.Conway的“生命游戏”

1.玩法

Conway的“生命游戏”是细胞自动机的一个例子:一组规则控制由离散细胞组成的区域的行为。在实践中,它会创建一个漂亮的动画以供观看。你可以用方块作为细胞在方格纸上绘制每个步骤。实心方块是“活”的,空心方块是“死”的。 如果一个活的方块与两个或3个活的方块为邻,它在下一步将还是活的。如果一个死的方块正 好有3个活的邻居,那么下一步它就是活的。所有其他方块在下一步都会死亡或保持死亡。

代码如下(示例):
#Conway's Game of Life
import random,time,copy
WIDTH=60
HEIGHT=20

#Create a list of list for the cells:
nextCells=[]
for x in range(WIDTH):
	column=[] #Create a new column.
	for y in range(HEIGHT):
		if random.randint(0,1)==0:
			column.append('#') #Add a living cell.
		else:
			column.append(' ') #Add a dead cell.
	nextCells.append(column) #nextCells is a list of column lists.
	
while True: #Main program loop.
	print('\n\n\n\n\n') #Separate eath step with newlines.
	currentCells=copy.deepcopy(nextCells)
	#print currentCells on the screen:
	for y in range(HEIGHT):
		for x in range(WIDTH):
			print(currentCells[x][y], end='') #print the # or space.
		print() #print a newline at the end of the row.
		
#Calculate the next step's cells based on current step's cell:
for x in range(WIDTH):
	for y in range(HEIGHT):
		#get neighboring coordinates:
		#'% WIDTH' ensures leftCoord is always between 0 and WIDTH -1
		leftCoord=(x-1)%WIDTH
		rightCoord=(x+1)%WIDTH
		aboveCoord=(y-1)%HEIGHT
		belowCoord=(y+1)%HEIGHT
		
		#Count number of living neighbors:
		numNeighbors=0
		if currentCells[leftCoord][aboveCoord]=='#':
			numNeighbors+=1 #top-left neighbor is alive.
		if currentCells[x][aboveCoord]=='#':
			numNeighbors+=1 #top neighbor is alive.
		if currentCells[rightCoord][aboveCoord]=='#':
			numNeighbors+=1 #top-right neighbor is alive.
		if currentCells[leftCoord][y]=='#':
			numNeighbors+=1 #left neighbor is alive.
		if currentCells[rightCoord][y]=='#':
			numNeighbors+=1 #right neighbor is alive.
		if currentCells[leftCoord][belowCoord]=='#':
			numNeighbors+=1 #bottom-left neighbor is alive.
		if currentCells[x][belowCoord]=='#':
			numNeighbors+=1 #bottom neighbor is alive.
		if currentCells[rightCoord][belowCoord]=='#':
			numNeighbors+=1 #bottom-right neighbor is alive.

		#set cell based on Conway's game of life rules:
		if currentCells[x][y]=='#' and (numNeighbors==2 or numNeighbors==3):
			#living cells with 2 or 3 neighbors stay alive:
			nextCells[x][y]='#'
		elif currentCells[x][y]=='' and numNeighbors==3:
			#dead cells with 3 neighbors become alive:
			nextCells[x][y]='#'
		else:
			#everything else dies or stays dead:
			nextCells[x][y]=''
	time.sleep(1) #add a 1-second pause to reduse flickering.

		

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


总结

以上是今天要讲的内容,练习了Conway生命小游戏。

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

K要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值