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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#include <iostream>
using namespace std;
//For Testing purposes only
const bool DEBUG = false;
const int ROWS = 12;
const int COLUMNS = 12;
//void PrintGrid(int World[ROWS][COLUMNS], int Rows[], int Columns[]);
//1
void CreateWorld(int World[ROWS][COLUMNS]);
//2
void Print(int World[ROWS][COLUMNS], int Rows[], int Columns[]);
//3
void CountNeighbors(int World[ROWS][COLUMNS],int i, int j);
//4
void CopyWorld(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS]);
//5
void Step(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS]);
//NOTE TO SELF: r = rows, c = columns
int main()
{
int World[ROWS][COLUMNS];
int TempWorld[ROWS][COLUMNS];
int Rows[ROWS], Columns[COLUMNS];
//Begin world as dead
CreateWorld(World);
//Initialize TempWorld as a duplicate of World
CopyWorld(World,TempWorld);
//Print grid and world
Print(World, Rows, Columns);
//Update world (using TempWorld as scratch workspace)
Step(World, TempWorld);
//Print out updated world
Print(World, Rows, Columns);
return 0;
}
void CreateWorld(int World[ROWS][COLUMNS])
{
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLUMNS; j++)
{
World[i][j] = 0;
}
}
//For testing purposes only
World[6][4] = 1;
World[6][5] = 1;
World[6][6] = 1;
}
void Print(int World[ROWS][COLUMNS],int Rows[],int Columns[])
{
for(int i = 0; i < ROWS; i++)
{
Rows[i] = i % 10;
}
for(int j = 0; j < COLUMNS; j++)
{
Columns[j] = j % 10;
}
cout<<" ";
for(int j = 0; j < COLUMNS; j++)
{
cout<<Columns[j]<<" ";
}
cout<<endl<<endl;
for(int i = 0; i < ROWS ; i++)
{
cout<<Rows[i]<<" ";
for(int j = 0; j < COLUMNS; j++)
{
cout<<World[i][j]<<" ";
}
cout<<endl;
}
}
// Return number of CountNeighbors of cell World[i][j]
/********
Your main problem is that you are updating World as you go.
Don't do that.
Instead, update TempWorld as you examine World,
then copy TempWorld to World when you are done.
*********/
void CountNeighbors(int World[ROWS][COLUMNS], int i, int j, int TempWorld[ROWS][COLUMNS])
{
//r is MAX_ROWS and c is MAX_COLUMNS
int r, c;
int Neighbors = 0;
for (r = i - 1; r <= i + 1; r++)
{
for (c = j - 1; c <= j + 1; c++)
{
Neighbors += World[r][c];
}
}
if(Neighbors < 2)
TempWorld[r][c] = 0;
else if (Neighbors > 3)
TempWorld[r][c] = 0;
else if (Neighbors == 2 || Neighbors == 3)
TempWorld[r][c] = 1;
else if (Neighbors == 3)
TempWorld[r][c] = 1;
//add a (!) if you would like to test the count
if(!DEBUG) cout<<"Neighbor count is: "<<Neighbors<<endl;
}
void CopyWorld(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS])
{
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
{
TempWorld[i][j] = World[i][j];
}
}
//Update everything
void Step(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS])
{
//NO int TempWorld[ROWS][COLUMNS];
//NO CopyWorld(World, TempWorld);
for (int i = 0; i < ROWS - 1; i++)
for (int j = 0; j < COLUMNS - 1; j++)
{
CountNeighbors(World, i, j, TempWorld);
}
CopyWorld(World, TempWorld);
}
|