Game of Life help
Hey guys, been trying to write this Game of life program. I can't get the next generation to display anything, it just turns blank.
Anyone want to tell me what i'm doing wrong here?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include <iostream>
using namespace std;
#include <conio.h>
#include <memory.h>
#include <stdlib.h>
void main ()
{
const int maxc (60);
const int maxr (60);
bool bContinue;
bool Board [maxr + 2] [maxc + 2];
bool NextBoard [maxr + 2] [maxc + 2];
int Col;
int Row;
int Generation;
memset (Board, false, (maxr + 2) * (maxc + 2) * sizeof (bool));
memset (NextBoard, false, (maxr + 2) * (maxc + 2) * sizeof (bool));
cout << "Row numbers must go from 1 to " << maxr << ", Column numbers from 1 to " << maxc << endl;
cout << "Enter a zero for both values to stop entering cells and go to generations" << endl;
do {
bContinue = true;
cout << "Enter a row and col pair: ";
cin >> Row >> Col;
if ((Row == 0) && (Col == 0))
bContinue = false;
else
Board [Row] [Col] = true;
} while (bContinue);
for (Generation = 0; ; Generation++)
{
cout << "Generation " << Generation << endl;
for (Row = 1; Row <= maxr; Row++)
{
for (Col = 1; Col <= maxc; Col++)
cout << (Board [Row] [Col] ? '*' : ' ');
cout << endl;
}
{
int neigh;
int numneighbor;
numneighbor = 0;
if (Board [Row] [Col-1])
numneighbor++;
if (Board [Row] [Col+1])
numneighbor++;
if (Board [Row-1] [Col])
numneighbor++;
if (Board [Row+1] [Col])
numneighbor++;
if (Board [Row+1] [Col+1])
numneighbor++;
if (Board [Row-1] [Col-1])
numneighbor++;
if (Board [Row-1] [Col+1])
numneighbor++;
if (Board [Row+1] [Col-1])
numneighbor++;
else;
if (numneighbor >= 4)
NextBoard [Row] [Col] = false;
else;
if (numneighbor <= 1)
NextBoard [Row] [Col] = false;
else;
if (numneighbor == 3)
NextBoard [Row] [Col] = true;
}
memcpy (Board, NextBoard, (maxr + 2) * (maxc + 2) * sizeof (bool));
while (!_kbhit ());
_getch ();
}
}
|
(1)
It was difficult to notice because you are not consistent with your indentation.
Fix that. You seem like an
Allman Style man to me.
http://en.wikipedia.org/wiki/Indent_style#Allman_style
(2)
Your problem is that you are only looking at
one cell each iteration, instead of the entire board.
It is because you forgot the following between lines 46 and 47:
46 47 48 49 50
|
}
for (Row = 1; Row <= maxr; Row++)
for (Col = 1; Col <= maxc; Col++)
{
|
(3)
Your code to update the cells (lines 78 through 87) needs some work. It doesn't match the
rules.
http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#Rules
(4)
Anything that says
else;
is useless. Get rid of it. Line 48 has an unused variable on it.
Also, your game currently doesn't provide any way to escape without a Control-C or a vulcan nerve pinch.
(5)
Otherwise, looks pretty good so far. Kudos!
Hope this helps.
Topic archived. No new replies allowed.