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
|
#include <iostream>
#include <iomanip>
#include <cassert>
#include <fstream>
#include "boolmatrix.h"
using namespace std;
void readGrid(/*out*/ boolMatrix& life);
void getGenerations(/*out*/ int& numGenerations);
void determineNextGeneration(/*inout*/ boolMatrix& life);
void determineFateOfSingleCell(/*in*/ const boolMatrix& life,/*out*/ boolMatrix& life2,/*in*/int row,/*in*/ int col);
void printResults(/*in*/ const boolMatrix& life);
int main()
{
boolMatrix life; //Used for file reading
int numGenerations;
/* life.print(); //Normal ADT Grid Display */
readGrid(life); //ADT / File Read Display
getGenerations(numGenerations);
for (int count = 0; count < numGenerations; count++)
{
determineNextGeneration(life);
printResults(life);
}
//*NOTE: MULTI LINE COMMENTS ARE CLASS TESTING FUNCTIONS*
/* //populate a bool array using rand values for cell coordinates or positions
for (int i = 0; i < 50; i++) {
life.set(rand() % 20, rand() % 20, true);
} */
// cout << life.get(0,0) << endl;
/* for (int row = 0; row < boolMatrix::NUM_ROWS; row++) {
for (int col = 0; col < boolMatrix::NUM_COLS; col++) {
cout << life.neighborCount(row, col); //calling an important function to look around a cell
}
cout << endl;
} */
}
void readGrid(boolMatrix& life)
{
int row, col;
//readGrid - HINT source code below should be a read or get_input function
ifstream infile("lifedata.txt");
infile >> row >> col;
while (infile)
{
//matrix[row][col] = true;
life.set(row,col,true); //a setter class member function is needed here
infile >> row >> col;
}
infile.close();
}
void getGenerations(/*out*/ int& numGenerations)
{
cout << "Enter number of generations: ";
cin >> numGenerations;
cout << endl;
}
void printResults(/*in*/ const boolMatrix& life)
{
life.print(); //calling the class print member function
cout << endl;
//calling three more class member functions
cout<<"Total alive in matrix: " << life.totalCount()<< endl; //Determines total alive
cout<<"Total alive in row 10: " << life.rowCount(10) << endl; //Determines total alive in specific row
cout<<"Total alive in column 10: " << life.colCount(10) << endl; //Determines total alive in specific column
cout << endl;
}
void determineNextGeneration(/*inout*/ boolMatrix& life)
{
boolMatrix b;
for(int row=0;row<boolMatrix::NUM_ROWS;row++)
{
for (int col=0;col<boolMatrix::NUM_COLS;col++)
{
int count = b.neighborCount(row,col);
if(b.get(row,col))
{
if(count > 1 && count < 4)
b.set(row,col,true);
}
else if(b.get(row,col))
{
if(count == 3)
b.set(row,col,true);
}
else if(b.get(row,col))
{
if(count == 1 || count == 0)
b.set(row,col,false);
}
else if(b.get(row,col))
{
if(count == 4 || count > 4)
b.set(row,col,false);
}
else if(b.get(row,col))
{
if(count == 2 || count == 4)
b.set(row,col,true);
}
}
}
life = b;
}
|