Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #include <conio.h> // specialized I/O functions
- #include <memory.h>
- #include <process.h> // functions to deal with the operating system
- #include <time.h> // contains functions related to the clock/time
- void main ()
- {
- const int NumCols (10);
- const int NumRows (10);
- const int MaxCols (NumCols + 2); // add an outside band of cells that are always going to be unoccupied
- const int MaxRows (NumRows + 2);
- bool Board [MaxRows] [MaxCols];
- bool Board2 [MaxRows] [MaxCols];
- time_t EndTime;
- int Generation;
- int i; //rows
- int j; //columns
- int livetest; //variable to count up living neighbors if cell is alive
- bool TimedOut;
- bool KeyHit;
- time_t WaitTime (3);
- memset (Board, false, (MaxRows * MaxCols * sizeof (bool)));
- memcpy (Board2, Board, (MaxRows * MaxCols * sizeof (bool))); // copies info from Board to Board2
- cout << "Enter the row (1 to " << NumRows << ") and col (1 to " << NumCols << ") position of each cell top be occupied" << endl;
- cout << "Enter a negative number for each to stop" << endl;
- do {
- cout << "=> ";
- cin >> i >> j;
- if ((i < 0) && (j < 0))
- break;
- else
- if ((i <= 0) || (j <= 0) || (i > NumRows) || (j > NumCols))
- cout << "Invalid position, try again" << endl;
- else
- Board [i] [j] = true;
- } while (true);
- Generation = 0;
- do {
- cout << "\tGeneration: " << Generation << endl;
- for (i = 1; i <= NumRows; i++) // only display the inner set of cells, not the outside band
- {
- for (j = 1; j <= NumCols; j++)
- cout << (Board [i] [j] ? '*' : ' ');
- cout << endl;
- }
- /* for each cell on the board
- // count how many neighbors are occupied
- // if current cell is occupied and num neighbors >= 4
- // it dies
- // if current cell is occupied and num neighbors <= 1
- // it dies
- // if current cell is not occupied and num neighbors == 3
- // it becomes alive
- if no match to above rules, cell remains unchanged
- */
- for (i = 1; i <= NumRows; i++)
- {
- for (j = 1, livetest = 0; j <= NumCols; j++)
- {
- if (Board [i] [j] == true) //Apply the rules if the cell is alive
- {
- if (Board [i-1] [j-1] == true)
- livetest ++;
- if (Board [i-1] [j] == true)
- livetest ++;
- if (Board [i-1] [j+1] == true)
- livetest ++;
- if (Board [i] [j-1] == true)
- livetest ++;
- if (Board [i] [j+1] == true)
- livetest ++;
- if (Board [i+1] [j-1] == true)
- livetest ++;
- if (Board [i+1] [j] == true)
- livetest ++;
- if (Board [i+1] [j+1] == true)
- livetest ++;
- if ((livetest >= 4) || (livetest <= 1)) //If the current gen cell is alive and either has 4 or more neighbors or 1 or less neighbors, it dies and applies it to Board2.
- Board2 [i] [j] = false;
- else
- Board2 [i] [j] = true; //If it doesn't fall under either, then it copies the alive status from the current generation to the next (Board2).
- }
- else if (Board [i] [j] == false) //Apply the rules of the game if the cell is dead.
- {
- if (Board [i-1] [j-1] == true)
- livetest ++;
- if (Board [i-1] [j] == true)
- livetest ++;
- if (Board [i-1] [j+1] == true)
- livetest ++;
- if (Board [i] [j-1] == true)
- livetest ++;
- if (Board [i] [j+1] == true)
- livetest ++;
- if (Board [i+1] [j-1] == true)
- livetest ++;
- if (Board [i+1] [j] == true)
- livetest ++;
- if (Board [i+1] [j+1] == true)
- livetest ++;
- if (livetest == 3) //If the dead cell has 3 live neighbors, then it becomes a birth cell and the status is copied to Board2.
- Board2 [i] [j] = true;
- else //If it doesn't, then it will remain dead for the next generation.
- Board2 [i] [j] = false;
- }
- }
- }
- memset (Board, false, (MaxRows * MaxCols * sizeof (bool))); //sets all values of the current generation to 'false' or dead
- memcpy (Board, Board2, (MaxRows * MaxCols * sizeof (bool))); // copies info from Board2 (the next gen) to Board (current gen for the next iteration)
- memset (Board2, false, (MaxRows * MaxCols * sizeof (bool))); //clears the next generation's information as not to pollute the results for the loop
- EndTime = time (0) + WaitTime; // time (0) returns the current time in seconds since Jan 1, 1970
- do {
- TimedOut = time (0) >= EndTime;
- KeyHit = _kbhit ();
- } while (!TimedOut && !KeyHit);
- if (KeyHit)
- _getch (); // get the key off the keyboard
- else;
- Generation++;
- system ("cls"); // send a command to the operating system, cls clears the screen in Windows
- } while (true);
- }
Advertisement
Add Comment
Please, Sign In to add comment