Hello everyone!
I have here my last homework assignment for an introductory C++ programming class. My skill level in this class ended about 4 weeks ago, so I'm have a terrible time with this last assignment.
I've searched the board and found a few threads related to this exact same program, but due to my limited knowledge in this stuff, I couldn't apply any of it to my code to find a fix.
It's another "Game of Life" type program I'm sure many of you are familiar with and probably consider pretty simple...I wish I did!
Anyway, here is the assignment instructions and my code to follow:
And the code:Write a program to simulate life.
The world is a rectangular grid of cells, each of which may contain an
organism.
Each cell has eight neighbors, like so:
1 2 3
4 * 5 (the numbers are for illustration purposes
only)
6 7 8
The world is initially created by the user with some organisms at
various
cells. The user specifies the cells that are initially "alive".
Then successive generations are obtained by two rules:
1) an organism in a cell survives to the next generation if exactly
2 or 3 of its neighbors are living, otherwise it dies
2) an organism is born into an unoccupied cell if exactly 3 of its
neighbors are occupied.
Display the generations under user command until the user grows weary
of this world.
Method:
The world will be represented by a 2-dimensional array (which may not
be a global variable). 20 by 20 is a good size.
Each component of the array will represent a cell that has a living
organism in it or is dead.
A new generation is generated by examining each cell and creating its
next state (living or dead) simultaneously with all the other cells,
thus the next generation must be created into a copy of the world.
To simplify the code, the "perimeter" of the world can always be dead
cells.
And here's my code. Only problem is, I can't figure out the algorithm
for
Option 2:
2) an organism is born into an unoccupied cell if exactly 3 of its
neighbors are occupied.
I hope it's not a total disaster. As is, this doesn't output anything and I don't know why. I found an example online that I followed which did work, somewhat, but for some reason mine doesn't at all.Code:#include <iostream> #include <iomanip> #include <cstdlib> #include <cctype> const int MAX_SIZE = 20; void printGen(int[][MAX_SIZE]); void createGen(int[][MAX_SIZE]); using namespace std; int main() { int lifeGen[MAX_SIZE][MAX_SIZE]; int row, col; int x = 0; int y = 0; char cont; //Initialize array by setting all blocks to 0 for(row = 0; row < MAX_SIZE; row++) for(col = 0; col < MAX_SIZE; col++) lifeGen[row][col] = 0; //Initial instructions for inputting coordinates. cout << "Enter the coordinates to plot cell locations. Input " << endl; cout << "results in the format of:" << endl << endl; cout << "10 15" << endl << endl; cout << "When finished, enter coordinates with at least one being negative." << endl; //Loop for entering coordinates until user enters in a negative one. do { cout << "Enter coordinates: "; cin >> x; cin >> y; if(x > MAX_SIZE || y > MAX_SIZE) { cout << "Coordinates must be from 0 - 20! Reenter coordinates: "; cin >> x >> y; } else lifeGen[x - 1][y - 1] = 1; } while(x > 0 && y > 0); printGen(lifeGen); //User-controlled loop for outputting new generations. do { cout << endl << endl; cout << "Press any key and 'Enter' to continue with next generation, " << endl; cout << "or press 'Q' and 'Enter' to quit: "; cin >> cont; if(toupper(cont) == 'q') { cout << "Thanks for trying my life simulator! Bye!" << endl; return 0; system("PAUSE"); } else { createGen(lifeGen); printGen(lifeGen); } } while(cont); return EXIT_SUCCESS; } //Function to output life generation on screen. void printGen(int lifeGen[][MAX_SIZE]) { int rows; int cols; do { for(rows = 0; rows < MAX_SIZE; rows++) for(cols = 0; cols < MAX_SIZE; cols++) { if(cols % 2 == 0) cout << endl; if(lifeGen[rows][cols] == 1) cout << "*"; else if(lifeGen[rows][cols] != 1) cout << " "; } } while(cols < MAX_SIZE); } //Function to create new generations of cells. void createGen(int lifeGen[][MAX_SIZE]) { int r; int c; int totalrc = 0; for(r = 0; r < MAX_SIZE; r++) for(c = 0; c < MAX_SIZE; c++) { if(lifeGen[r][c] == 1) totalrc = lifeGen[r - 1][c - 1] + lifeGen[r - 1][c] + lifeGen[r - 1][c + 1] + lifeGen[r][c - 1] + lifeGen[r][c + 1] + lifeGen[r + 1][c - 1] + lifeGen[r][c - 1] + lifeGen[r + 1][c] + lifeGen[r + 1][c + 1]; if(totalrc <= 1 || totalrc >= 4) lifeGen[r][c] = 0; else if(totalrc == 3) lifeGen[r][c] = 1; } }
Any help would be greatly appreciated!