首页 > 解决方案 > 如何检查邻居的条件?

问题描述

我固执地试图自己解决尽可能多的问题。但是,我认为我已经陷入僵局。

我必须为 20x20 网格上的生活游戏的简单版本编写代码。条件是:

我的特殊问题是如何编写执行上述操作的算法。

因为我没有想法,所以我没有尝试太多。我确实希望得到一些想法,这些想法可能会给我额外的推动力来完成我更新世界/领域的功能。

#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

标签: cconways-game-of-life

解决方案


我所做的就是编写两个嵌套的 for 循环,确保检查每个单元格。

嗯,这是一个开始。

但是我不确定如何进行和实施上述条件。关于如何检查每个单元格的邻居的任何想法?

evolve()函数接收field,显然描述了板的当前状态和下一个状态。看起来带有索引的单元格的数据ijfield[i][j]. 所以主要问题是:哪些细胞是那个细胞的邻居?但这应该不难。它们是除 ( i, ) 之外的八个单元格,每个单元格的索引分别与或j最多相差 1 。也就是说,( , ), ( , ), ( , ), ( , )等等。如果你需要用实际的单元格索引来为一个单元格做一个例子。iji - 1j - 1i - 1ji - 1j + 1ij - 1

因此,您似乎会计算所有相邻细胞的活人口,并将其与当前细胞是否活着结合起来,以确定并记录该细胞将如何进化。

请注意边缘和角落是特殊情况:它们至少在一侧没有邻居,并且您不能尝试检查不存在的邻居。您应该能够通过在尝试访问相邻小区索引之前检查它们是否在边界内来实现这一点。


推荐阅读