Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #include<ctime>
- using namespace std;
- #define ROWS 12 // Definted amount of rows
- #define COLS 12 // Defined amount of columns
- int generation(int world[ROWS][COLS], int& rows, int& cols, int& n_count);
- void display(int world[ROWS][COLS], int world_2[ROWS][COLS], int& rows, int& cols);
- void scanArray(int world[ROWS][COLS], int& rows, int& cols, int& n_count);
- void generateArray(int world[ROWS][COLS], int& rows, int& cols);
- int main()
- {
- int world[ROWS][COLS]={0}; // Original World/ Also helps print border of zeroes
- int world_2[ROWS][COLS]={0}; //New world generated
- int rows = 12;
- int cols = 12;
- int n_count = 0;
- cout << "Welcome to The Game of Life!" << endl;
- generateArray(world, rows, cols);
- display(world, world_2, rows, cols);
- cout << "This is the original world generated with 0s as no life and 1s as a single life";
- scanArray(world, rows, cols, n_count);
- world_2[ROWS][COLS] = world[ROWS][COLS];
- display(world, world_2, rows, cols);
- system("pause");
- return 0;
- }
- void generateArray(int world[ROWS][COLS], int& rows, int& cols)
- {
- for(int row = 1; row < rows-1; row++) //Minus one and set equal to one so that broder is not included.
- {
- for(int col = 1; col < cols-1; col++)
- {
- world[row][col] = rand()%2;
- }
- }
- }
- void display(int world[ROWS][COLS], int world_2[ROWS][COLS], int& rows, int& cols)
- {
- for(int row = 0; row < rows; row++)
- {
- for(int col = 0; col < cols; col++)
- {
- cout << setw(5) << world[row][col];
- }
- cout << endl;
- }
- cout << endl;
- }
- void scanArray(int world[ROWS][COLS], int& rows, int& cols, int& n_count)
- {
- int neighbor_count = 0;
- for(int row = 1; row < rows-1; row++)
- {
- for(int col = 1; col < cols-1; col++)
- {
- neighbor_count = generation(world, rows, cols, n_count);
- if(world[row][col] == 1){
- if(neighbor_count == 1 || neighbor_count == 0)
- world[col][row] = 0;
- if(neighbor_count >= 4)
- world[col][row] = 0;
- if(neighbor_count == 2 || neighbor_count == 3)
- world[col][row] = 1;
- }
- if(world[row][col] == 0){
- if(neighbor_count == 3)
- world[row][col] = 1;
- }
- }
- }
- }
- int generation(int world[ROWS][COLS], int& rows, int& cols, int& n_count)
- {
- if(world[cols][rows-1] == 1) // The cell above
- n_count++;
- if(world[cols-1][rows] == 1) // The cell to the left
- n_count++;
- if(world[cols+1][rows] == 1) // The cell to the right
- n_count++;
- if(world[cols][rows+1] == 1) // The cell below
- n_count++;
- if(world[cols-1][rows-1] == 1) // The cell diagonally upper left
- n_count++;
- if(world[cols-1][rows+1] == 1) // The cell diagonally lower left
- n_count++;
- if(world[cols+1][rows-1] == 1) // The cell diagonally upper right
- n_count++;
- if(world[cols+1][rows+1] == 1) // The cell diagonally lower right
- n_count++;
- return n_count++;
- }
Add Comment
Please, Sign In to add comment