我正试图为康威的生命游戏编写一个计数邻居方法。如果一个死细胞与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;
}发布于 2015-09-17 11:33:08
正如@AlexD所指出的,i<row+1应该是i<=row+1,这将解释您的答案。
https://stackoverflow.com/questions/32618909
复制相似问题