康威的人生游戏(C)-错误检查存活的邻居(计数过多)

时间:2019-12-07 06:21:43

标签: c conditional-statements conways-game-of-life

我会事先说,是的,这是家庭作业。我花了大量时间试图弄清楚如何使用C代码和2d数组访问逻辑来迭代conway的生活游戏。我可以从stdin(分配要求)中读取一个板,以及运行update_board的步骤/代数。就目前而言,我目前遇到的唯一问题是我的两个函数countWhite和countBlue(它们包含检查相邻细胞中是否存在白色和蓝色活细胞的条件)。特定的问题是在条件whiteAmount和blueAmount内的某些点迭代次数超出了应有的程度。

--xmax;
--ymax;
int xtemp = xmax - 1;
int ytemp = ymax - 1;
int whiteAmount = 0;
if(!(y+1>ymax)){
 if(current[x][y+1] == 'w'){
   printf("[x][y+1] activated white at: %d, %d\n", x,y);
   ++whiteAmount;
 }
}else{
 if(current[x][0] == 'w'){
   printf("[x][0] activated white at: %d, %d\n", x,y);
   ++whiteAmount;
 }
}

if(!(y-1<0)){
 if(current[x][y-1] == 'w'){
   printf("[x][y-1] activated white at: %d, %d\n", x,y);
   ++whiteAmount;
 }
}else{
 if(current[x][ytemp]=='w'){
   printf("[x][ytemp] activated white at: %d, %d\n", x,y);
   ++whiteAmount;
 }
}

if(!(x-1<0)){
 if(!(y+1>ymax)){
   if(current[x-1][y+1] == 'w'){
     printf("[x-1][y+1] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }
 }else{
   if(current[x-1][0]=='w'){
     printf("[x-1][0] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }
 }
 if(!(y-1<0)){
   if(current[x-1][y-1]=='w'){
     printf("[x-1][y-1] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }
 }else{
   if(current[x-1][ytemp]=='w'){
     printf("[x-1][ytemp] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }
 }

 if(current[x-1][y] == 'w'){
   printf("[x-1][y] activated white at: %d, %d\n", x,y);
   ++whiteAmount;
 }
}else{
 if(y+1>ymax){
   if(current[xtemp][0]=='w'){
     printf("[xtemp][0] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }
 }else{
   if(current[xtemp][y+1]=='w'){
     printf("[xtemp][y+1] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }
 }
 if(y-1<0){
   if(current[xtemp][ytemp]=='w'){
     printf("[xtemp][ytemp] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }else{
     if(current[xtemp][y-1]=='w'){
       printf("[xtemp][y-1] activated white at: %d, %d\n", x,y);
       ++whiteAmount;
     }
   }
 }
 if(current[xtemp][y]=='w'){
   printf("[xtemp][y] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }
}


if(!(x+1>xmax)){
 if(current[x+1][y] == 'w'){
   printf("[x+1][y] activated white at: %d, %d\n", x,y);
   ++whiteAmount;
 }
 if(!(y+1>ymax)){
   if(current[x+1][y+1]=='w'){
     printf("[x+1][y+1] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
 }else{
   if(current[x+1][0]=='w'){
     printf("[x+1][0] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }
 }
}
 if(!(y-1<0)){
   if(current[x+1][y-1]=='w'){
     printf("[x+1][y-1] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
 }else{
   if(current[x+1][ytemp]=='w'){
     printf("[x+1][ytemp] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }
 }
}
}else{
if(y+1>ymax){
 if(current[0][0]=='w'){
   printf("[0][0] activated white at: %d, %d\n", x,y);
   ++whiteAmount;
 }else{
   if(current[0][y+1]=='w'){
     printf("[0][y+1] activated white at: %d, %d\n", x,y);
     ++whiteAmount;
   }
 }
if(y-1<0){
 if(current[0][ytemp]=='w'){
   printf("[0][ytemp] activated white at: %d, %d\n", x,y);
   ++whiteAmount;
 }
}else{
 if(current[0][y-1]=='w'){
   printf("[0][y+1] activated white at: %d, %d\n", x,y);
   ++whiteAmount;
 }
}
 if(current[0][y]=='w'){
   printf("[0][y] activated white at: %d, %d\n", x,y);
   ++whiteAmount;
 }
}
}








return whiteAmount;

}

我也可以发布countblue,但这与countwhite相同,其中'w'和++ whiteAmount为'b'和++ blueAmount。

  for(x=0;x<rows;++x){
    for(y=0;y<cols;y++){
      tempGrid[x][y] = grid[x][y];
      int blueAM = 0;
      int whiteAM = 0;
      storeLiving=0;
      blueAM = countBlue(grid,x,y,rows,cols);
      whiteAM = countWhite(grid,x,y,rows,cols);
      storeLiving = (whiteAM+blueAM);
      printf("%d %d\n",blueAM,whiteAM);

      if((grid[x][y] == 'w') || (grid[x][y] == 'b'))
      {
        if(storeLiving>3 || storeLiving<2){
              tempGrid[x][y] = 'x';
            }
        if(storeLiving==2 || storeLiving==3){
          tempGrid[x][y] = grid[x][y];
        }
      }

      if((grid[x][y] == 'x'))
      {
        if(storeLiving==3){
          if(blueAM>whiteAM){
            tempGrid[x][y] = 'b';
          }
          if(whiteAM>blueAM){
            tempGrid[x][y] = 'w';
          }
        }
      }
    }
  } 

这就是我使用countblue和countwhite(这似乎在起作用)

我已经多次检查并更新了这些条件,并且对于解决此问题需要做什么一无所知。

是否有明显的方法可以使我疲惫的大脑丢失?还是有一种更好的简单方法。

0 个答案:

没有答案