鱼C论坛

 找回密码
 立即注册
查看: 2386|回复: 2

C语言生命游戏的小问题

[复制链接]
发表于 2019-10-16 19:10:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include <time.h>
  6. #define High 25
  7. #define Width 50

  8. int cells[High][Width];

  9. void gotoxy(int, int);
  10. void starup();
  11. void show();
  12. void updateWithoutInput();
  13. void updateWithInput();

  14. int main()
  15. {
  16.         starup();
  17.         while(1)
  18.         {
  19.                 show();
  20.                 updateWithoutInput();
  21.                 updateWithInput();
  22.         }
  23.         return 0;
  24. }

  25. void gotoxy(int x, int y)
  26. {
  27.         HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  28.     COORD pos;
  29.     pos.X = x;
  30.     pos.Y = y;
  31.     SetConsoleCursorPosition(handle, pos);
  32. }

  33. void starup()
  34. {
  35.         int i, j;
  36.         for(i = 0; i < High; i++)
  37.                 for(j = 0; j < Width; j++)
  38.                         cells[i][j] = 1;
  39. }

  40. void show()
  41. {
  42.         gotoxy(0, 0);
  43.         int i, j;
  44.         for(i = 0; i <= High; i++)
  45.         {
  46.                 for(j = 0; j <= Width; j++)
  47.                 {
  48.                         if(cells[i][j] == 1)
  49.                                 printf("*");
  50.                         else
  51.                                 printf(" ");
  52.                 }
  53.                 printf("\n");
  54.         }
  55.         Sleep(50);
  56. }

  57. void updateWithoutInput()
  58. {
  59.         int NewCells[High][Width];         //问题所在
  60.         int NeibourNumber;
  61.         int i, j;
  62.        
  63.         for(i = 0; i <= High - 1; i++)
  64.         {
  65.                 for(j = 0; j <= Width - 1; j++)
  66.                 {
  67.                         NeibourNumber = cells[i - 1][j - 1] + cells[i - 1][j] + cells[i - 1][j + 1] + cells[i][j - 1] + cells[i][j + 1] +
  68.                         cells[i + 1][j - 1] + cells[i + 1][j] + cells[i + 1][j + 1];
  69.                         if(NeibourNumber == 3)
  70.                                 NewCells[i][j] = 1;
  71.                         else if(NeibourNumber == 2)
  72.                                 NewCells[i][j] = cells[i][j];
  73.                         else
  74.                                 NewCells[i][j] = 0;
  75.                 }
  76.         }
  77.        
  78.         for(i = 1; i <= High - 1; i++)
  79.                 for(j = 1; j <= Width - 1; j++)
  80.                 cells[i][j] = NewCells[i][j];
  81. }

  82. void updateWithInput()
  83. {
  84. }

复制代码


求助!为什么需要再设一个数组来保存下一帧的数据呢?为什么不能直接给原数组赋值呢?比如
  1. void updateWithoutInput()
  2. {
  3.         for(i = 0; i < High; i++)
  4.                 for(j = 0; j < Width; j++)
  5.                 {
  6.                         k = cells[i - 1][j] + cells[i + 1][j] + cells[i][j - 1] + cells[i][j + 1] + cells[i + 1][j + 1] +
  7.                         cells[i + 1][j - 1] + cells[i - 1][j + 1] + cells[i - 1][j - 1];                       
  8.                        
  9.                         if(k == 3)
  10.                                 cells[i][j] = 1;
  11.                         if(k == 2)
  12.                                 ;
  13.                         else
  14.                                 cells[i][j] = 0;
  15.                 }

  16. }
复制代码


求大佬解答,发现自己写的代码达不到游戏效果
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-17 12:04:33 | 显示全部楼层
  1. k = cells[i - 1][j] + cells[i + 1][j] + cells[i][j - 1] + cells[i][j + 1] + cells[i + 1][j + 1] +
  2.                         cells[i + 1][j - 1] + cells[i - 1][j + 1] + cells[i - 1][j - 1];                        
复制代码


兄弟,虽然你这个代码我只能看懂一些,但是这里很明显,i,i+1,i-1或者j,j-1,j+1都是有联系的,直接给原数组赋值,岂不是破坏了逻辑结构,给另一个数组赋值,正好顺其形式~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-17 12:06:00 | 显示全部楼层
类似于辛普森的公式~额,可能你不了解辛普森,但是就是这样的吧,应该应该
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-14 16:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表