Here are the directions
Write a program to implement the simulation of life as described in Scientific American by Martin Gardner. The program will be implemented on a two dimensional surface of size 60 by 60 visible elements. The rules of the simulation are as follows:
1) An initial set of cells are marked as “alive” by the user. This is generation 0. Your program will ask the user to input a set of row and column values to let the user determine which cells are “alive”. Display this generation.
2) Cells change for each succeeding generation by the following rules:
a. A living cell dies of overcrowding in the next generation if it currently has 4 or more living neighbors.
b. A living cell dies of loneliness in the next generation if it currently has only 0 or 1 living neighbors.
c. An empty cell becomes a “birth” cell (becomes alive) in the next generation if it has exactly 3 living neighbors.
d. All other cells remain unchanged.
3) The new generation becomes the current generation and is displayed.
4) After displaying each new generation, ask the user if they wish to continue to the next generation or stop at this point.
I'm not sure how to start with this program. My professor gave this to help us start, but I'm still kinda confused on this assignment. Tried looking for help with it, but I can't find anything. I even looked up how the code is supposed to work, but it is hard for me to understand it. Even when my professor demonstrated it to us, it didn't make too much sense. Do we have the user input where each living cell is and to make it loop until a certain result or will it be made for us?
First thing is first. What can I change and/or omit from this code to help me get a better start on it?
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
|
#include <iostream>
using namespace std;
#include <memory.h> // needed for functions that start with mem
#include <stdlib.h> // needed for system used in program below
#include <time.h> // needed for time/clock functions
void main ()
{
const long MaxCols (10);
const long MaxRows (10);
const time_t WaitTime (3);
bool Board [MaxRows] [MaxCols];
long CurrCol;
long CurrRow;
long Generation;
time_t StopTime; // time_t means unsigned 64 bit unsigned whole number
memset (Board, false, MaxRows * MaxCols * sizeof (bool)); // only guaranteed to work when setting to 0
// Allow user to initialize board as to which cells are alive or dead
for (CurrRow = 0; CurrRow < MaxRows; CurrRow++)
Board [CurrRow] [CurrRow] = true;
for (Generation = 0; ; Generation++)
{
// Show the board
system ("cls");
cout << "\tGeneration: " << Generation << endl;
for (CurrRow = 0; CurrRow < MaxRows; CurrRow++)
{
for (CurrCol = 0; CurrCol < MaxCols; CurrCol++)
cout << (Board [CurrRow] [CurrCol] ? '*' : ' ');
cout << endl;
}
// for each cell on the board
// count how many neighbors are alive
// apply rules to determine what happens to cell in next generation
// update the board
// ask if user wants to exit or not
StopTime = time (0) + WaitTime; // time (0) gets current time in seconds from Jan 1, 1970
do {
} while (StopTime > time (0)); // keep looking at clock until it is the stop time
}
}
|