¿Por qué mi juego de vida C ++ no funciona correctamente?
-
25-09-2019 - |
Pregunta
Esto se compila y funciona bien, pero los resultados son totalmente diferentes de lo que deberían ser.
He cortado el código de irrelaventes:
bool grid[1280][1024]; // hardcoded to my screen res for now
for (int x = 0; x<1280; x++) //init grid to false
{
for (int y = 0; y<1024; y++)
{
grid[x][y] = false;
}
}
grid[320][120] = true; // add a simple pattern
grid[320][121] = true;
grid[320][122] = true;
grid[320][123] = true;
grid[320][124] = true;
grid[320][125] = true;
grid[320][126] = true;
grid[320][127] = true;
grid[320][128] = true;
// [snipped]
for (int x = 1; x< (1280 - 1); x++)
{
for (int y = 1; y< (1024 - 1); y++)
{
int n = 0; // neighbours
if (grid[x-1][y-1]) n++; // top left
if (grid[x][y-1]) n++; // top middle
if (grid[x+1][y-1]) n++; // top right
if (grid[x-1][y]) n++; // left
if (grid[x+1][y]) n++; // right
if (grid[x-1][y+1]) n++; // bottom left
if (grid[x][y+1]) n++; // bottom middle
if (grid[x+1][y+1]) n++; // bottom right
if (grid[x][y]) // current cell is on
{
SetPixel(screen, x, y, on); // drawing function
if (n < 2) grid[x][y] = false; // die. :(
if (n > 3) grid[x][y] = false; // die. :(
// otherwise (2 or 3), survive. :)
}
else // current cell is off
{
SetPixel(screen, x, y, off); // drawing function
if (n == 3) grid[x][y] = true; // IT'S ALIVE!!!
}
}
}
Solución
En primer lugar, no funciona porque no ha separado los resultados de cada celda, es decir, si la cuadrícula [0] [0] muere, entonces esto se reflejará inmediatamente en la vida o la muerte de la red [1] [0], que No es como funciona el juego de la vida. En segundo lugar, no funciona porque no parece haber ejecutado el juego más de una iteración.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow