参与康威的生命游戏,我觉得我非常接近完成它,但我的逻辑有一些缺陷。我已经倾倒了几个小时了,我只觉得第二双眼睛能够比我更快地发现我的错误。我的程序编译得很好,但输出不符合特定位置的游戏指南(第0行,第1行在第二代时有生命,但第10行似乎没有按预期工作)。对于任何不熟悉的人: http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#Rules
我几乎可以肯定逻辑错误发生在函数advGen中。以下链接包含函数readGrid使用的bacteria.txt的内容:http://pastebin.com/6vurFRSB
感谢您花时间阅读并帮助您选择这样做。
//sample setup to start the game of life - startlife.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int SIZE = 20;
//initial function prototypes
void initGrid(bool life[][SIZE], bool nextGen[][SIZE]);
void readGrid(bool life[][SIZE], bool nextGen[][SIZE]);
void printGrid(bool life[][SIZE], bool nextGen[][SIZE]);
void advGen(bool life[][SIZE], bool nextGen[][SIZE]);
int main()
{
bool life[SIZE][SIZE];
bool nextGen[SIZE][SIZE];
readGrid(life, nextGen);
/* for (int count = 0; count < 5; count++){
determineNextGen(life);
}*/
printGrid(life, nextGen);
advGen(life, nextGen);
printGrid(life, nextGen);
return 0;
}
/*-------------------------------------------------------
readGrid (and related functions)
---------------------------------------------------------*/
void readGrid(bool life[][SIZE], bool nextGen[][SIZE])
{
ifstream infile("bacteria.txt"); //see class site project#5 for data file
int numBacteria, row, col;
initGrid(life, nextGen);
infile >> row >> col;
while (infile){
life[row][col] = true;
infile >> row >> col;
}
infile.close();
}
void initGrid(bool life[][SIZE], bool nextGen[][SIZE])
{
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++){
life[row][col] = false;
}
}
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++){
nextGen[row][col] = false;
}
}
}
void printGrid(bool life[][SIZE], bool nextGen[][SIZE])
{
cout << " 01234567890123456789" << endl;
for (int row = 0; row < SIZE; row++)
{
cout << setw(2) << row;
for (int col = 0; col < SIZE; col++)
{
if (life[row][col])
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
}
void advGen(bool life[][SIZE], bool nextGen[][SIZE])
{
int neighbor = 0;
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
if (row == 0)
{
if (col == 0)
{
if (life[row + 1][col] == true)
++neighbor;
if (life[row][col + 1] == true)
++neighbor;
if (life[row + 1][col + 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
nextGen[row][col] = false;
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
nextGen[row][col] = true;
}
}
else if (col == 19)
{
if (life[row + 1][col] == true)
++neighbor;
if (life[row][col - 1] == true)
++neighbor;
if (life[row + 1][col - 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
nextGen[row][col] = false;
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
nextGen[row][col] = true;
}
}
else
{
if (life[row + 1][col] == true)
++neighbor;
if (life[row][col + 1] == true)
++neighbor;
if (life[row][col - 1] == true)
++neighbor;
if (life[row + 1][col + 1] == true)
++neighbor;
if (life[row + 1][col - 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
nextGen[row][col] = false;
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
nextGen[row][col] = true;
}
}
}
if (row == 19)
{
if (col == 0)
{
if (life[row - 1][col] == true)
++neighbor;
if (life[row][col + 1] == true)
++neighbor;
if (life[row - 1][col + 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
nextGen[row][col] = false;
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
nextGen[row][col] = true;
}
}
else if (col == 19)
{
if (life[row - 1][col] == true)
++neighbor;
if (life[row][col - 1] == true)
++neighbor;
if (life[row - 1][col - 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
nextGen[row][col] = false;
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
nextGen[row][col] = true;
}
}
else
{
if (life[row - 1][col] == true)
++neighbor;
if (life[row][col - 1] == true)
++neighbor;
if (life[row][col + 1] == true)
++neighbor;
if (life[row - 1][col - 1] == true)
++neighbor;
if (life[row - 1][col + 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
nextGen[row][col] = false;
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
nextGen[row][col] = true;
}
}
}
if (col == 0)
{
if (row == 0)
{
if (life[row + 1][col] == true)
++neighbor;
if (life[row][col + 1] == true)
++neighbor;
if (life[row + 1][col + 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
//nothing
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
//nothing
}
}
else if (row == 19)
{
if (life[row - 1][col] == true)
++neighbor;
if (life[row][col + 1] == true)
++neighbor;
if (life[row - 1][col + 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
//nothing
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
//nothing
}
}
else
{
if (life[row - 1][col] == true)
++neighbor;
if (life[row + 1][col] == true)
++neighbor;
if (life[row][col + 1] == true)
++neighbor;
if (life[row - 1][col + 1] == true)
++neighbor;
if (life[row + 1][col + 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
nextGen[row][col] = false;
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
nextGen[row][col] = true;
}
}
}
if (col == 19)
{
if (row == 0)
{
if (life[row - 1][col] == true)
++neighbor;
if (life[row][col - 1] == true)
++neighbor;
if (life[row + 1][col - 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
//nothing
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
//nothing
}
}
else if (row == 19)
{
if (life[row - 1][col] == true)
++neighbor;
if (life[row][col - 1] == true)
++neighbor;
if (life[row - 1][col - 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
//nothing
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
//nothing
}
}
else
{
if (life[row][col - 1] == true)
++neighbor;
if (life[row - 1][col] == true)
++neighbor;
if (life[row + 1][col] == true)
++neighbor;
if (life[row + 1][col - 1] == true)
++neighbor;
if (life[row - 1][col - 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
nextGen[row][col] = false;
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
nextGen[row][col] = true;
}
}
}
else
{
if (life[row - 1][col] == true)
++neighbor;
if (life[row + 1][col] == true)
++neighbor;
if (life[row][col - 1] == true)
++neighbor;
if (life[row][col + 1] == true)
++neighbor;
if (life[row - 1][col - 1] == true)
++neighbor;
if (life[row + 1][col + 1] == true)
++neighbor;
if (life[row - 1][col + 1] == true)
++neighbor;
if (life[row + 1][col - 1] == true)
++neighbor;
if ( (life[row][col] == true) && (neighbor < 2 || neighbor > 3))
{
nextGen[row][col] = false;
}
else if ( (life[row][col] == false) && (neighbor == 3) )
{
nextGen[row][col] = true;
}
}neighbor = 0;
}
}
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
life[row][col] = nextGen[row][col];
}
}
}
答案 0 :(得分:1)
您的代码重复计算邻居的数量。这是因为,例如,如果row
为0且col
为0,则会在row == 0
情况下执行row == 0
情况并在{{1}内执行col == 0
情况if语句大约100行。
解决此问题的一种方法是使用正确的else
子句。即做一些事情:
if (row == 0)
{
...
}
else if (row == 19)
{
...
}
else
{
...
}
您也需要为列案例执行此操作。
更好的解决方案是查看所有这些特殊情况,并尝试将它们合理化为重复代码较少的东西。
您要实现的目标是防止在数组边界之外进行读取。
一种简单的方法是使用函数来获取row, col
处的数组值,该值检查row
和col
并返回false
if {{ 1}}或row
超出范围。然后,您可以为每个位置的8个周围块中的每个块调用该函数,而不会对第0行和第19行以及第0行和第10行有特殊情况。
我建议尝试编写一个类似上面的函数,而不是仅仅为你粘贴它。
编辑:另请注意 - 您并不总是将值设置为col
,因此您要杀死有2或3个邻居的单元格,您可能需要在分配时重新考虑if / else语句进入nextGen