- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
看上了康威的《生命游戏》,开始尝试用python写。目前我还没有为程序的边界编写任何代码,所以我只是在寻求帮助我现在拥有的东西。我似乎在初始化“信号灯”编队时遇到了麻烦。它没有像它应该的那样振荡,而是似乎把自己变成了一个立方体。
#File: gameoflife.py
#Description: This is a basic "life" simulation that follows three distinct rules:
#1.Any live cell with fewer than two live neighbours dies
#2.Any live cell with two or three live neighbours lives
#3.Any live cell with more than three live neighbours dies
#4.Any dead cell with exactly three live neighbours becomes a live cell
#A neighbor is deemed as any cell directly horizantal/vertical/diagonal
#meaning there are 9 neighbors to a cell at any time
from graphics import *
import random
from time import sleep
def initial(D,M,win):
#Creates the white board background
for i in range (11):
m = [] # rectangle list
for j in range (11):
rec = Rectangle(Point(6 + 4 * i, 6 + 4 * j), Point(10 + 4 * i, 10 + 4 * j))
D[i][j] = 0
rec.setFill("white")
rec.draw(win)
m.append(rec)
M.append(m)
def check(x,y,D):
#Checks all 9 adjacent neihbors to see if "alive" and checks this number
#means the cell should stay alive(1), die(0), or if already dead come
#back alive(2)
counter = 0
if D[x+1][y] == 1:
counter += 1
if D[x-1][y] == 1:
counter += 1
if D[x][y+1] == 1:
counter += 1
if D[x][y-1] == 1:
counter +=1
if D[x+1][y+1] == 1:
counter+=1
if D[x+1][y-1] == 1:
counter+= 1
if D[x-1][y-1] == 1:
counter += 1
if D[x-1][y+1] == 1:
counter +=1
if counter<2 or counter>3:
return 0
if counter == 2:
return 1
if counter == 3:
return 2
def main():
win = GraphWin("Game of Life", 700, 600)
win. setCoords(0, 0, 70, 60)
#Initialize two dimesion arrays.
#D records color of grids, M records rectangles
D = []
M = []
C = []
#initialize the grids to create all "white"
for i in range(11):
d = []
c = []
for j in range(11):
d.append(0)
c.append(0)
D.append(d)
C.append(c)
initial(D,M,win)
#Initialzes three "alive" units
D[5][5],D[4][5] ,D[6][5]= 1,1,1
C[5][5],C[4][5] ,C[6][5]= 1,1,1
M[5][5].setFill("Black")
M[4][5].setFill("Black")
M[6][5].setFill("Black")
#Contiually checking
while True:
#Purposfully not checking the "Borders" of the array
for i in range (len(D)-1):
for j in range(len(D[i])-1):
#If the cell is alive
if D[i][j] == 1:
#If the cell should die
if check(i,j,D) == 0:
sleep(1)
#Sets a temporary list to white
C[i][j] = 0
#Fills the cell white
M[i][j].setFill("White")
#if the cell is dead
if D[i][j] == 0:
#If the cell should be revived
if check(i,j,D) == 2:
sleep(1)
#Sets a temporary list to black
C[i][j] = 1
#Fills the cell black
M[i][j].setFill("Black")
#Sets the main list = to the temporary list
D = C
main()
最佳答案
您需要交换 D 和 C,而不仅仅是将 C 分配给 D。就目前而言,D 和 C 将在第一次迭代后引用同一个列表。
关于python - Conway 的 Python 人生游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8484802/
我正在为一个学校项目开发康威的生命游戏。我不是直接寻找代码。我想找出我的代码有什么问题。 在康威的生命游戏中,如果一个细胞有 3 个存活的邻居,它就会从死亡状态变为存活状态。如果它有两个或三个活着的邻
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我构建了 Conways 生命游戏,但算法运行不正常。我为此混合使用了 Js 和 Jquery。我的程序所做的是逐个单元格地遍历整个棋盘,检查单元格的邻居,并通过检查它的邻居将游戏规则应用于每个单元格
这是一个有点理论性的问题。在编程作业中,约翰康威告诉我们要实现生命游戏。作为一项附加任务,我们被要求修改程序,以便它可以检测最多四代的模式重复。例如,给定这个特定的游戏“种子”,程序应该像这样: -
我目前正在使用 C++ 开发 Conway 的 Game of Life 的控制台版本。 Repo Link 问题是我的 Draw() 方法没有尽可能快地绘制下一代。 我记得在某处读到过,与获取控制台
我用 Java 编写了康威生命游戏的代码,现在我正在尝试将其转换为 C。代码非常简单,但是我在尝试创建适当的 结构时遇到了问题。 在 Java 中,如果我有实例变量: private boolean
我正在尝试制作康威生命游戏的简单版本,其中计算机生成矩形网格并填充代表“活”细胞的矩形。我遇到的问题是我无法在第一个图案之后清除网格,因此所有图案都在同一个网格上生成,它看起来像一大团彩色矩形。 这是
我的查询真的很难描述,所以我会尽量简洁地解释它。 在康威的生命游戏中,假设我有一张这样的 map : _ _ _ _ _ _ _ _ _ _ U _ R Y
我已经研究这段代码很长时间了,但我似乎无法弄清楚。我的代码的顶部部分用于预填充网格。但由于某种原因,我无法让我的形象按照预期移动和成长。请帮助我!! import java.util.Random;
所以我阅读了 Conways Game of Life 并尝试使用 PyGame 来实现它。 我试着让它面向对象。它的工作方式是我有一个细胞实例列表,然后检查它们有多少邻居,然后根据它们的邻居存活或死
这是康威用 Python 制作的生命游戏模拟的代码片段。出于检查它是否有效的目的,它非常简单 - 但它没有。无法弄清楚为什么,但据我所知,它与更新有关。 感谢任何关于它为什么更新它的方式的输入: py
我正在尝试完成 Hofstadter-Conway $10,000 sequence Scala 中的任务 Rosetta Code .我想尽可能地使用惯用的 Scala,但是我在不使用原始递归的情况
我在使用 C++ 为 Conway 的生命游戏编码时遇到了一些问题。我觉得我有一个好的开始,我只需要一些方向。首先,当我尝试运行该程序时,我总是得到一个空白的控制台。而且我也不太确定为板上没有完整八个
看上了康威的《生命游戏》,开始尝试用python写。目前我还没有为程序的边界编写任何代码,所以我只是在寻求帮助我现在拥有的东西。我似乎在初始化“信号灯”编队时遇到了麻烦。它没有像它应该的那样振荡,而是
我已经编写了 Conway's Game of Life 的简单实现使用 Store comonad(见下面的代码)。我的问题是网格生成从第五次迭代开始明显变慢。我的问题是否与我正在使用 Store
public void actionPerformed(ActionEvent e){ grid=new JButton[length+20][width+20];
我正在尝试用 Java 编写康威生活游戏,但它没有按预期运行。我的意思是,静物有效,但眼罩和轮船不有效。 这是我的算法的代码,播种由一个单独的鼠标监听器处理(按预期工作)。 int[][] s
I am trying to write a program that implements Conway's game of life on a 20x60 cell board. The grid
我正在使用在 involuntaryexercise.com 上找到的 reni2D 库构建这个程序我创建了一个由二维行列组成的网格,单击其中的每个方 block 即可打开和关闭。现在,我正在尝试制作
我正在尝试为 Conway 的人生游戏编写一个计算邻居的方法。如果一个死细胞与 2 或 3 个活细胞相邻,它应该会复活。但是,我的代码没有正确计算所有邻居。如果我给出输入坐标 (10, 10), (1
我是一名优秀的程序员,十分优秀!