C++年康威生命游戏的相邻细胞计数
Counting neighboring cells for Conway's Game of Life in C++
我正在尝试为康威的生活游戏写一个计数邻居方法。如果一个死细胞周围有2到3个活细胞,它就会活过来。然而,我的代码没有正确地计算所有的邻居。如果我给出输入坐标(10,10)(10,11)(10,12)这将产生
***
程序将把下一代打印为
*
*
,坐标为(10,11)和(11,11)。然而,也应该有一个点在(9,11)。我知道问题出现在这个函数中对于点(9,11)这个函数没有计算3个邻居。
int Life::neighbor_count (int row, int col)
{
int i, j;
int count=0;
for(i=row-1; i<row+1; i++){
for (j=col-1; j<=col+1; j++){
count +=grid[i][j];//increase the count is neighbor is alive
}
}
count -=grid [row][col];//reduce count, since cell is not its own neighbor
return count;
}
正如@AlexD指出的,i<row+1
应该是i<=row+1
,这就解释了你的答案
相关文章:
- 大量序列中核苷酸类型的快速计数
- 我的字符计数代码计算错误.为什么
- 递归计数给定目录的文件和所有目录
- 计算排序向量的向量中唯一值的计数
- c++中O(n^(1/3))中一个数的除数的有效计数
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Qt SQLite没有查询或参数计数不匹配
- Craps游戏问题,忽略if语句
- 地图计数确实很重要,或者只是检查是否存在
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 字符串操作 - 字符计数
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 抛硬币计数游戏C++数据结构!.
- 活着的邻居细胞未正确计数
- Qt——计数有色细胞
- 在网格细胞游戏中查找子弹轨迹上的所有细胞
- C++年康威生命游戏的相邻细胞计数