c - 如何检查邻居的条件?
问题描述
我固执地试图自己解决尽可能多的问题。但是,我认为我已经陷入僵局。
我必须为 20x20 网格上的生活游戏的简单版本编写代码。条件是:
- 具有 0 或 1 个活着的邻居的细胞会在下一代死亡。
- 一个有 2 或 3 个活着的邻居的细胞生活在下一代。
- 具有 4 个或更多存活邻居的细胞会在下一代死亡。
- 一个恰好有 3 个活邻居的空细胞成为下一代活细胞。
我的特殊问题是如何编写执行上述操作的算法。
因为我没有想法,所以我没有尝试太多。我确实希望得到一些想法,这些想法可能会给我额外的推动力来完成我更新世界/领域的功能。
#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 loadRandom(const int rows, const int cols, cell field[rows][cols]);
void loadCustom(const int rows, const int cols, cell field[rows][cols]);
void printWorld(const int rows, const int cols, cell field[rows][cols]);
void evolve(const int rows,const int 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];
initField(rows,cols, field);
printWorld(rows,cols,field);
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, [R]andom ");
printf("or [C]ustom): ");
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);
break;
case 'r':
case 'R':
loadRandom(rows, cols, field);
break;
case 'c':
case 'C':
default:
loadCustom(rows, cols, field);
break;
}
}
/* 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: loadRandom
* Description: Inserts a random structure into the field.
* Input: The field array and its size.
* Output: The field array is updated. There is a 50 % chance that a cell
* is alive.
*/
void loadRandom(const int rows, const int cols, cell field[rows][cols]) {
}
/* Function: loadCustom
* Description: Lets the user specify a structure that then is inserted into
* the field.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void loadCustom(const int rows, const int cols, cell field[rows][cols]) {
printf("Give custom format string: ");
do {
int r, c;
scanf("%d,%d", &r, &c);
field[r][c].current = ALIVE;
} while (getchar() != '\n');
}
/* 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]){
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");
}
c = getchar();
if(c != '\n'){
break;
}
}
void evolve(const int rows,const int cols,cell field[rows][cols]){
for(int i = 0;i<rows;i++){
for(int j =0;j<cols;j++){
if()
}
}
}
您可以首先看到当前的所有进展。
printWorld()
除了和之外的所有功能evolve()
都是预制的,应该保持原样。
这是我目前的进步evolve
,还不算多。
void evolve(const int rows,const int cols,cell field[rows][cols]){
for(int i = 0;i<rows;i++){
for(int j =0;j<cols;j++){
if()
}
}
}
我所做的就是编写两个嵌套的 for 循环,确保检查每个单元格。
但是我不确定如何进行和实施上述条件。关于如何检查每个单元格的邻居的任何想法?
英语不是我的第一语言。所以我提前为任何语法错误道歉。如果您无法理解我想要什么,请询问,我会澄清。
我还要添加一个免责声明,即 function:printWorld
尚未完成,因为它仍然需要 function evolve
。
解决方案
我所做的就是编写两个嵌套的 for 循环,确保检查每个单元格。
嗯,这是一个开始。
但是我不确定如何进行和实施上述条件。关于如何检查每个单元格的邻居的任何想法?
该evolve()
函数接收field
,显然描述了板的当前状态和下一个状态。看起来带有索引的单元格的数据i
将j
在field[i][j]
. 所以主要问题是:哪些细胞是那个细胞的邻居?但这应该不难。它们是除 ( i
, ) 之外的八个单元格,每个单元格的索引分别与或j
最多相差 1 。也就是说,( , ), ( , ), ( , ), ( , )等等。如果你需要用实际的单元格索引来为一个单元格做一个例子。i
j
i - 1
j - 1
i - 1
j
i - 1
j + 1
i
j - 1
因此,您似乎会计算所有相邻细胞的活人口,并将其与当前细胞是否活着结合起来,以确定并记录该细胞将如何进化。
请注意边缘和角落是特殊情况:它们至少在一侧没有邻居,并且您不能尝试检查不存在的邻居。您应该能够通过在尝试访问相邻小区索引之前检查它们是否在边界内来实现这一点。
推荐阅读
- r - 给出了一个 IMDB_dataset,我需要找到-
- mongodb - 从使用 pymongo 存储数据的 mongodb 数据库中访问数据
- python - 如何在无头环境中正确初始化 Chrome 上的 WebGL 以使用 MapBox?
- java - 使用 Jackson 解析 XML 时出现 MismatchedInputException
- automationanywhere - 在 Web Recorder 命令中管理 Web 控件和对象克隆对象属性之间的区别
- dart - Flutter中关闭应用程序时如何接收本地通知?
- swift - 为什么 Int.random() 比 arc4random_uniform() 慢?
- java - 如何在黄瓜测试中部署 Localstack
- vapor - 如何确定传入帖子数据的类型?
- teradata - 为什么加载到 Teradata 时 abinitio 会出现此错误?“输入端口在活动时关闭(未读数据保留)”