C 如何更新world以获得下一次死活细胞迭代
我正在为《生命的游戏》写代码。进展甚微 这个问题对我来说不是很明显,但我怀疑为什么我不能迭代我的世界 描述我的代码应该执行的操作: 在我的main函数中,我声明了数组/世界的维度 一个字符数组C 如何更新world以获得下一次死活细胞迭代,c,conways-game-of-life,C,Conways Game Of Life,我正在为《生命的游戏》写代码。进展甚微 这个问题对我来说不是很明显,但我怀疑为什么我不能迭代我的世界 描述我的代码应该执行的操作: 在我的main函数中,我声明了数组/世界的维度 一个字符数组字段,其中我可以将每个元素存储为死或活 声明的数组计数,并将其传递给函数PrintWorld,其中数组存储每个单元格的邻域数量 函数intitField(行、列、字段)用于初始化我的字段数组 最后,我有一个函数printWorld(行、列、字段、计数),它应该执行必要的操作以更新我的世界 关于printWo
字段
,其中我可以将每个元素存储为死
或活
声明的数组计数
,并将其传递给函数PrintWorld
,其中数组存储每个单元格的邻域数量
函数intitField(行、列、字段)
用于初始化我的字段
数组
最后,我有一个函数printWorld(行、列、字段、计数)
,它应该执行必要的操作以更新我的世界
关于printWorld
的功能及其与其他功能的交互方式的摘要:
printWorld
所做的是:
打印当前初始化的字段。当用户按下enter键时,While循环强制执行此操作
while循环中有一个函数evolve
,它应该在用户每次按enter键时更新世界
在evolve
函数中,我们将找到一个函数cellnighbor
cellnearch
使用在我的主函数中声明的数组对[rows][cols]进行计数
在CellNeighbor
内部,我们存储每个单元在数组中的活动邻居数量计数
当所有活动的邻居都存储在计数中时
我们继续的其余部分
现在我可以用这个事实来检验生活游戏所强加的条件
正如您在evolve
中所看到的,正在通过检查条件准备单元的下一次迭代
然后确定下一个细胞是死是活
确定下一代的所有单元后,我们继续执行函数evolve
现在我们进入函数updateWorld
updateWorld所做的一切是,它为下一代获取所有新存储的单元格,并将其存储在字符数组字段[行][cols]中。当前
因此,当我们返回函数printWorld
时,我应该为while循环中的字段[I][j]获取新值。新一代的细胞应该被印刷出来
不幸的是,我找不到我做错了什么。但是,我怀疑调用函数时,某些数组会丢失其存储的信息。但我无法证实这一点
您可以在下面找到我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Constants, representation of states */
#define ALIVE 'X'
#define DEAD '.'
/* Declaration of data structure */
typedef struct{
char current;
char next;
} cell;
/* Declaration of functions */
void initField(const int rows, const int cols, cell field[rows][cols]);
void loadGlider(const int rows, const int cols, cell field[rows][cols]);
void loadSemaphore(const int rows, const int cols, cell field[rows][cols]);
void printWorld(const int rows, const int cols, cell field[rows][cols],int counts[rows][cols]);
void CellNeighbour(const int rows, const int cols, cell field[rows][cols], int counts[rows][cols]);
void evolve(const int rows,const int cols,cell field[rows][cols], int counts[rows][cols]);
void updateWorld(int const rows, int const cols, cell field[rows][cols]);
/* Function: main
* Description: Start and run games, interact with the user.
* Input: About what initial structure and whether to step or exit.
* Output: Information to the user, and the game field in each step.
*/
int main(void) {
const int rows = 20;
const int cols = 20;
cell field[rows][cols];
int counts[rows][cols];
initField(rows,cols, field);
printWorld(rows,cols,field,counts);
//CellNeighbour(rows,cols,field,counts);//test
/*Used for testing*/
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
printf("%d ", counts[i][j]);// updated in CellNeighbour
}
printf("\n");
}
return 0;
}
/* Function: initField
* Description: Initialize all the cells to dead, then asks the user about
* which structure to load, and finally load the structure.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void initField(const int rows, const int cols, cell field[rows][cols]) {
for (int r = 0 ; r < rows ; r++) {
for (int c = 0 ; c < cols ; c++) {
field[r][c].current = DEAD;
}
}
printf("Select field spec to load ([G]lider, [S]emaphore)");
int ch = getchar();
/* Ignore following newline */
if (ch != '\n') {
getchar();
}
switch (ch) {
case 'g':
case 'G':
loadGlider(rows, cols, field);
break;
case 's':
case 'S':
loadSemaphore(rows, cols, field);
}
}
/* Function: loadGlider
* Description: Inserts a glider into the field.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void loadGlider(const int rows, const int cols, cell field[rows][cols]) {
field[0][1].current = ALIVE;
field[1][2].current = ALIVE;
field[2][0].current = ALIVE;
field[2][1].current = ALIVE;
field[2][2].current = ALIVE;
}
/* Function: loadSemaphore
* Description: Inserts a semaphore into the field.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void loadSemaphore(const int rows, const int cols, cell field[rows][cols]) {
field[8][1].current = ALIVE;
field[8][2].current = ALIVE;
field[8][3].current = ALIVE;
}
/* Function: printWorld
* Description: Prints the current field
* Input: The field array and its size.
* Output: The field array is updated.
*/
void printWorld(const int rows, const int cols, cell field[rows][cols], int counts[rows][cols]){
char c = '\n';
while(c == '\n'){
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%c ", field[i][j].current);
}
printf("\n");
}
evolve(rows,cols,field,counts);
printf("EXIT PROGRAM PRESS ANY CHARACTERS\n");
printf("TO GET NEXT ITERATION PRESS ENTER\n");
c = getchar();
if(c != '\n'){ //exits the loop
break;
}
}
}
void evolve(const int rows,const int cols,cell field[rows][cols], int counts[rows][cols]){
CellNeighbour(rows, cols,field,counts);
for(int i = 0;i<rows;i++){
for(int j =0;j<cols;j++){
if (field[i][j].current == ALIVE && counts[i][j] == 2){
field[i][j].next = ALIVE;
}
else if (counts[i][j] == 3){
field[i][j].next = ALIVE;
}
else{
field[i][j].next = DEAD;
}
}
//Updating the next iteration, below:
updateWorld(rows, cols, field);
}
}
void CellNeighbour(const int rows, const int cols, cell field[rows][cols], int counts[rows][cols]){
int i,j;
for(i = 0;i<rows;i++){
for(j =0;j<cols;j++){
counts[i][j] =0;
}
}
for( i =0;i<rows;i++){
for( j = 0;j<cols;j++){
if(i>0 && j>0){
if(field[i-1][j-1].current == ALIVE){
counts[i][j]++;
}
}
if(j>0){
if(field[i][j-1].current == ALIVE){
counts[i][j]++;
}
}
if(j>0){
if(field[i+1][j-1].current == ALIVE){
counts[i][j]++;
}
}
if(i<rows-1){
if(field[i+1][j].current == ALIVE){
counts[i][j]++;
}
}
if(i <rows-1 && j <cols-1){
if(field[i+1][j+1].current == ALIVE){
counts[i][j]++;
}
}
if(j<cols-1){
if(field[i][j+1].current == ALIVE){
counts[i][j]++;
}
}
if(i>0 && j<cols-1){
if(field[i-1][j+1].current == ALIVE){
counts[i][j]++;
}
}
if(i>0){
if(field[i-1][j].current == ALIVE){
counts[i][j]++;
}
}
}
}
}
void updateWorld(int const rows, int const cols, cell field[rows][cols]){
for(int i = 0;i<rows;i++){
for(int j =0;j<cols;j++){
field[i][j].current = field[i][j].next;
}
}
}
我得到:
. X . . . . . . . . . . . . . . . . . .
. . X . . . . . . . . . . . . . . . . .
X X X . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
所以在细胞进化(更新)后,我应该得到:
. . x . . . . . . . . . . . . . . . . . .
X . X . . . . . . . . . . . . . . . . .
. X X . . . . . . . . . . . . . . . . .
. X . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
以下是我用滑翔机规格初始化场地时得到的视频:
但是,它应该从屏幕的左上角“滑动”到右下角
你知道为什么会这样吗 在initField中,尝试将下一个值初始化为死。或者(回想起来,是一种更好的方法),在evolve中,设置激活的标准,然后简单地将一个大的else
catchall设置为DEAD。但是对于assignment,我得到了一个代码模板,其中包括intitField
和里面的所有函数。这些是我唯一不允许修改的函数。不幸的是,然后继续我在评论中添加的方法(回想起来更好):)问题很可能是目前没有设置next
值-即,它们不是活着或死了。要修复此问题,Evolve必须显式地将每个单元格的next
值设置为ALIVE或DEAD.Hmm。因此,我添加了:else{field[rows][cols].next=DEAD;}
在evolve
的嵌套for循环中,但输出保持不变:/OK,您是否可以更新问题中的代码:)在initField中,尝试将next
值初始化为DEAD。或者(回想起来,是一种更好的方法),在evolve中,设置激活的标准,然后简单地将一个大的else
catchall设置为DEAD。但是对于assignment,我得到了一个代码模板,其中包括intitField
和里面的所有函数。这些是我唯一不允许修改的函数。不幸的是,然后继续我在评论中添加的方法(回想起来更好):)问题很可能是目前没有设置next
值-即,它们不是活着或死了。要修复此问题,Evolve必须显式地将每个单元格的next
值设置为ALIVE或DEAD.Hmm。因此我添加了:else{field[rows][cols].next=DEAD;}
在evolve
的嵌套for循环中,但输出保持不变:/OK,您可以更新问题中的代码吗:)
. . x . . . . . . . . . . . . . . . . . .
X . X . . . . . . . . . . . . . . . . .
. X X . . . . . . . . . . . . . . . . .
. X . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .