Stuck on my "The Game of Life" assignment
Jul 8, 2011 at 3:47am UTC
I've searched a bit and found plenty of posts about this, I just can't figure out what mine's doing. If anyone's willing to take a look at my code and notices anything blatantly not working, that would be really helpful. :)
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 99 100 101 102 103 104 105 106 107 108 109 110 111
#include <iostream>
#define Row 62
#define Col 62
using namespace std;
int i;
int j;
void Cycle (bool BoardA [] [Col], bool BoardB [] [Col]) // Performs the rule checks required for the assignment:
{ // Dies if less than two neighbors
unsigned int Neighbors; // Dies if four or more neighbors
for (i = 0; i < Row; i++) // Born/survives if exactly three neighbors
{
for (j = 0; j < Col; j++)
{
Neighbors = 0;
if (BoardA [i - 1] [j - 1] = true ) Neighbors += 1; // Determining number of neighbors
if (BoardA [i - 1] [j] = true ) Neighbors += 1;
if (BoardA [i - 1] [j + 1] = true ) Neighbors += 1;
if (BoardA [i] [j - 1] = true ) Neighbors += 1;
if (BoardA [i] [j + 1] = true ) Neighbors += 1;
if (BoardA [i + 1] [j - 1] = true ) Neighbors += 1;
if (BoardA [i + 1] [j] = true ) Neighbors += 1;
if (BoardA [i + 1] [j + 1] = true ) Neighbors += 1;
if (BoardA [i] [j] = true && Neighbors < 2) // applying rules
BoardB [i] [j] = false ;
else if (BoardA [i] [j] = true && Neighbors >= 4)
BoardB [i] [j] = false ;
else if (BoardA [i] [j] = true && Neighbors == 3)
BoardB [i] [j] = true ;
else if (BoardA [i] [j] = false && Neighbors == 3)
BoardB [i] [j] = true ;
}
}
}
void Clear (bool Board [] [Col]) // clears my game board
{
for (i = 0; i <= Row; i++)
{
for (j = 0; j <= Col; j++)
Board [i] [j] = false ;
}
}
void Update (bool BoardA [] [Col], bool BoardB [] [Col])// Assigns the new board to the original board
{ // I think this is where part of my problem arises
for (i = 0; i < Row; i++)
{
for (j = 0; j < Col; j++)
BoardA [i] [j] = BoardB [i] [j];
}
}
void Print (bool Board [] [Col]) // Prints board
{
for (int i = 0; i < Row; i++)
{
for (int j = 0; j < Col; j++)
{
if (Board [i][j]) cout << "*" ;
else cout << "_" ;
}
cout << endl;
}
}
void main()
{
bool BoardA [Row] [Col];
bool BoardB [Row] [Col];
int i;
int j;
int Input;
for (i = 0; i <= 62; i++) // Clearing BoardA. Didn't use my function, was messing around
{
for (j = 0; j <= 62; j++)
BoardA [i] [j] = false ;
}
cout << "Please enter column and row coordinates for living cells, then -2 to populate: " ;
do { // Taking input from user to apply to board
cin >> j;
if (j == -2)
break ;
cin >> i;
BoardA [i] [j] = true ;
Print (BoardA);
}while (j != -2);
do {
Life: // Application of all my functions. Stops working here
Clear (BoardB);
Cycle (BoardA, BoardB);
Update (BoardA, BoardB);
Print (BoardA);
cout << "Enter -2 to continue or -1 to exit: " ;
cin >> Input;
if (Input == -2)
goto Life;
if (Input == -1)
exit (0);
}while (Input != -1);
}
Jul 8, 2011 at 5:38am UTC
You got some problems with the positions in arrays like:
1 2 3 4 5 6 7
{
for (i = 0; i <= Row; i++)
{
for (j = 0; j <= Col; j++)
Board [i] [j] = false ;
}
}
this part of your code will look from Board[5][62] for example which doesnt exist. Just change the <= operator to <.
1 2 3 4 5 6 7 8
if (BoardA [i - 1] [j - 1] = true ) Neighbors += 1; // Determining number of neighbors
if (BoardA [i - 1] [j] = true ) Neighbors += 1;
if (BoardA [i - 1] [j + 1] = true ) Neighbors += 1;
if (BoardA [i] [j - 1] = true ) Neighbors += 1;
if (BoardA [i] [j + 1] = true ) Neighbors += 1;
if (BoardA [i + 1] [j - 1] = true ) Neighbors += 1;
if (BoardA [i + 1] [j] = true ) Neighbors += 1;
if (BoardA [i + 1] [j + 1] = true ) Neighbors += 1;
also same here. you are checking if BoardA[-1][0] is true which doesnt exist. Also do you want the == comparison operator in the if statements?
Last edited on Jul 8, 2011 at 5:39am UTC
Topic archived. No new replies allowed.