我认为每个人都听说过这款游戏,但是如果没有听说过,请访问以下链接: https://bitstorm.org/gameoflife/
我正在尝试在C ++中实现它而不使用结构,类等。
到目前为止,我已经做到了:
#define N 10
#define M 10
#include <iostream>
bool** createGrid(int n, int m)
{
bool** grid = new bool*[n];
for (int i = 0; i < n; i++)
grid[i] = new bool[m];
return grid;
}
void displayGrid(bool** grid, int n, int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 1)
std::cout << 'X';
else
std::cout << '.';
}
std::cout << std::endl;
}
std::cout << "##########\n";
}
void releaseGrid(bool** grid, int n)
{
for (int i = 0; i < n; i++)
delete[] grid[i];
delete[] grid;
}
int countAliveNeighbours(bool** grid, int k, int l)
{
int aliveNeighbours = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
aliveNeighbours += grid[k + i][l + j];
// The cell needs to be subtracted from it's neighbours as it was counted before
aliveNeighbours -= grid[k][l];
return aliveNeighbours;
}
bool** nextGeneration(bool** grid, int n, int m)
{
bool** next = createGrid(n, m);
// Loop through every cell
for (int k = 1; k < n - 1; k++) {
for (int l = 1; l < m - 1; l++) {
// finding count of neighbours that are alive
int aliveNeighbours = countAliveNeighbours(grid, k, l);
// Implementing the Rules of Life
// Cell is lonely and dies
if ((grid[k][l] == 1) && (aliveNeighbours < 2))
next[k][l] = 0;
// Cell dies due to over population
else if ((grid[k][l] == 1) && (aliveNeighbours > 3))
next[k][l] = 0;
// A new cell is born
else if ((grid[k][l] == 0) && (aliveNeighbours == 3))
next[k][l] = 1;
// Remains the same
else
next[k][l] = grid[k][l];
}
}
return next;
}
bool checksTwoGridsForDifferences(bool** prevGrid, bool** nextGrid, int n, int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (prevGrid[i][j] != nextGrid[i][j]) {
return true;
}
}
}
return false;
}
void fillGrid(bool** grid, int n, int m)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
grid[i][j] = 0;
}
}
grid[1][3] = 1;
grid[1][4] = 1;
grid[2][4] = 1;
grid[5][3] = 1;
grid[5][4] = 1;
grid[6][2] = 1;
grid[6][3] = 1;
grid[7][5] = 1;
grid[8][4] = 1;
}
int main()
{
bool** prevGrid = createGrid(N, M); // create starting grid
fillGrid(prevGrid, N, M); // fill starting grid
std::cout << "Starting grid:\n";
displayGrid(prevGrid, N, M); // display starting grid
bool** nextGrid = nextGeneration(prevGrid, N, M); //generate next grid
while (checksTwoGridsForDifferences(prevGrid, nextGrid, N, M)) {
displayGrid(nextGrid, N, M);
releaseGrid(prevGrid, N);
prevGrid = nextGrid;
nextGrid = nextGeneration(prevGrid, N, M);
}
releaseGrid(nextGrid, N);
releaseGrid(prevGrid, N);
return 0;
}
但是我被卡住了,因为第三个网格不正确(错误地擦除了两个X),我想知道为什么吗?谁能告诉我这个错误?
它显示:
..........
..........
...XX.....
..........
...X......
..X.X.....
..........
..XXX.....
..........
..........
代替:
..........
...XX.....
...XX.....
..........
...X......
..X.X.....
..........
..XXX.....
..........
..........
答案 0 :(得分:1)
问题是您没有填充下一代网格的边缘。 // Loop through every cell
中的注释nextGeneration
不正确,因为您跳过了网格的边缘。
您需要在countAliveNeighbours
函数中加倍努力,以免跨过网格边缘,然后更改nextGeneration
函数,以便真正遍历每个单元格。或者,您可以永久关闭网格的边缘。
我使用调试器在大约两分钟内发现了此问题。通过查看代码我看不到它。您确实应该自学如何使用调试器。作为程序员,这是您有史以来最大的进步。
请问问题BTW,足够的信息可以轻松解决问题。