Game of life problems
Dec 17, 2011 at 1:46am UTC
I can not seem to get the out put the way my teachers wants it to be.
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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int ROW = 20, COL = 20;
void print_array (bool [] [COL]);
void init_array (bool [] [COL]);
void input_array (bool [] [COL]);
void print_array2 (bool [] [COL]);
void calculate(bool [][COL], bool [][COL]);
void swap(bool [][COL], bool [][COL]);
int main()
{
bool current_gen[ROW][COL];
bool next_gen[ROW][COL];
init_array (current_gen);
input_array (current_gen);
print_array2 (current_gen);
init_array (next_gen);
calculate(current_gen, next_gen);
swap(current_gen,next_gen);
print_array2 (next_gen);
swap (current_gen,next_gen);
init_array (next_gen);
calculate(current_gen, next_gen);
swap(current_gen,next_gen);
print_array2 (next_gen);;
system("PAUSE" );
return 0;
}
void print_array2 (bool somearray [] [COL])
{
cout << " 01234567890123456789" << endl;
for (int i = 0; i < ROW; i++)
{
cout << setw(2) <<i;
for (int j = 0; j < COL; j++)
{
if (somearray[i][j])
cout << "*" ;
else
cout << " " ;
}
cout << endl;
}
}
void init_array (bool somearray [] [COL])
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
somearray[i][j] = false ;
}
}
}
void input_array (bool somearray[] [COL])
{
int row,col;
ifstream inData;
string fileName;
cout << "Please enter a filename" << endl;
cin >> fileName;
inData.open(fileName.c_str());
inData >> row >> col;
while (inData)
{
somearray[row][col] = true ;
inData >> row >> col;
}
}
void calculate(bool somearray[][COL], bool somearray1[][COL])
{
unsigned int neighbors;
for (int i = 0; i < ROW ; i++)
{
for (int j = 0; j < COL ; j++)
{
neighbors = 0;
if ( somearray[i-1][j-1] == 1) neighbors += 1;
if ( somearray[i-1][j] == 1) neighbors += 1;
if ( somearray[i-1][j+1] == 1) neighbors += 1;
if ( somearray[i][j-1] == 1) neighbors += 1;
if ( somearray[i][j+1] == 1) neighbors += 1;
if ( somearray[i+1][j-1] == 1) neighbors += 1;
if ( somearray[i+1][j] == 1) neighbors += 1;
if ( somearray[i+1][j+1] == 1) neighbors += 1;
if ( somearray[i][j] == 1 && neighbors < 2)
somearray1[i][j] = 0;
else if ( somearray[i][j] == 1 && neighbors > 3)
somearray1[i][j] = 0;
else if ( somearray[i][j] == 1 && (neighbors == 2 || neighbors == 3))
somearray1[i][j] = 1;
else if ( somearray[i][j] == 0 && neighbors == 3)
somearray1[i][j] = 1;
}
}
}
void swap(bool somearray[][COL], bool somearray1[][COL])
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
somearray[i][j] = somearray1[i][j];
}
}
I can get my first print up to look like what my teacher wants, but after that. The outputs are all off on the near the edges. Any help whould be awesome.
Thanks!!
Dec 17, 2011 at 2:18am UTC
I'm confused on what the program does.
Topic archived. No new replies allowed.