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
|
#include <iostream>
#include <fstream>
#include <cstdlib>
/*I made the array 2 bigger than it should be, and just
ignored the outside edge to make it easier to find neighbors
*/
#define MaxRows 24
#define MaxColumns 82
using namespace std;
void neighborCheck(char lifeArray[MaxRows][MaxColumns], int neighborArray[MaxRows][MaxColumns]);
void nextGenerations(char lifeArray[MaxRows][MaxColumns], int neighborArray[MaxRows][MaxColumns]);
void game(char lifeArray[MaxRows][MaxColumns], int neighborArray[MaxRows][MaxColumns]);
int main(){
int neighborArray[MaxRows][MaxColumns];
char lifeArray[MaxRows][MaxColumns];
//initializes all positions in the array to spaces
for(int x = 0; x < MaxRows; x++)
for(int y = 0; y < MaxColumns; y++)
lifeArray[x][y] = ' ';
//Trys to get the file name from the user
ifstream input;
char fileName[40];
cout << "What is the name of the input file?" << endl;
cin >> fileName;
input.open(fileName);
//If you can't read the file, quit the program
if(input.fail())
{
cout << "Could not read the file\n";
return 1;
}
//Else put the asterisks into the array
else{
int x = 1, y = 1;
while(!input.eof() && y <= MaxRows-1){
char tempChar;
input.get(tempChar);
if(tempChar !='\n'){
while(!input.eof() && tempChar != '/n' && x <= MaxColumns-1){
lifeArray[x][y] = tempChar;
input.get(tempChar);
y++;
}
y = 1;
x++;
}
else if(tempChar == '\n'){
y = 1;
x++;
}
}
}
input.close();
game(lifeArray, neighborArray);
return 0;
}
void neighborCheck(char lifeArray[MaxRows][MaxColumns], int neighborArray[MaxRows][MaxColumns]){
//Initialize neighbor counts to zero
for(int x = 0; x < MaxRows; x++){
for(int y = 0; y < MaxColumns; y++){
neighborArray[x][y] = 0;
}
}
//check each element in the array for neighbors
for(int x = 1; x < MaxRows -1; x++){
for(int y = 1; y < MaxColumns -1; y++){
if(lifeArray[x-1][y-1] == '*'){
neighborArray[x][y] += 1;
}
if(lifeArray[x-1][y] == '*'){
neighborArray[x][y] += 1;
}
if(lifeArray[x-1][y+1] == '*'){
neighborArray[x][y] += 1;
}
if(lifeArray[x][y-1] == '*'){
neighborArray[x][y] += 1;
}
if(lifeArray[x][y+1] == '*'){
neighborArray[x][y] += 1;
}
if(lifeArray[x+1][y-1] == '*'){
neighborArray[x][y]+= 1;
}
if(lifeArray[x+1][y] == '*'){
neighborArray[x][y] += 1;
}
if(lifeArray[x+1][y+1] == '*'){
neighborArray[x][y] += 1;
}
}
}
}
void nextGenerations(char lifeArray[MaxRows][MaxColumns], int neighborArray[MaxRows][MaxColumns]){
for(int x = 1; x < MaxRows-1; x++)
for(int y = 1; y < MaxColumns-1; y++){
if(lifeArray[x][y] == '*' && neighborArray[x][y] < 2){
lifeArray[x][y] = ' ';
}
else if(lifeArray[x][y] == '*' && neighborArray[x][y] > 3){
lifeArray[x][y] = ' ';
}
else if(lifeArray[x][y] == ' ' && neighborArray[x][y] == 3){
lifeArray[x][y] = '*';
}
}
}
void game(char lifeArray[MaxRows][MaxColumns], int neighborArray[MaxRows][MaxColumns]){
int generationCount = 1;
system("CLS");
while(true){
for(int x = 1; x < MaxRows-1; x++){
for(int y = 1; y < MaxColumns-1; y++){
cout << lifeArray[x][y];
}
}
cout << endl << "Generation: " << generationCount << " ";
system("pause");
neighborCheck(lifeArray, neighborArray);
nextGenerations(lifeArray, neighborArray);
generationCount++;
system("CLS");
}
}
|