Guest User

Untitled

a guest
Jun 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<ctime>
  4. using namespace std;
  5.  
  6. #define ROWS 12 // Definted amount of rows
  7. #define COLS 12 // Defined amount of columns
  8.  
  9. int generation(int world[ROWS][COLS], int& rows, int& cols, int& n_count);
  10. void display(int world[ROWS][COLS], int world_2[ROWS][COLS], int& rows, int& cols);
  11. void scanArray(int world[ROWS][COLS], int& rows, int& cols, int& n_count);
  12. void generateArray(int world[ROWS][COLS], int& rows, int& cols);
  13.  
  14. int main()
  15. {
  16.     int world[ROWS][COLS]={0}; // Original World/ Also helps print border of zeroes
  17.     int world_2[ROWS][COLS]={0}; //New world generated
  18.     int rows = 12;
  19.     int cols = 12;
  20.     int n_count = 0;
  21.     cout << "Welcome to The Game of Life!" << endl;
  22.     generateArray(world, rows, cols);
  23.     display(world, world_2, rows, cols);
  24.     cout << "This is the original world generated with 0s as no life and 1s as a single life";
  25.     scanArray(world, rows, cols, n_count);
  26.     world_2[ROWS][COLS] = world[ROWS][COLS];
  27.     display(world, world_2, rows, cols);
  28.     system("pause");
  29.  
  30.     return 0;
  31. }
  32.  
  33. void generateArray(int world[ROWS][COLS], int& rows, int& cols)
  34. {
  35.     for(int row = 1; row < rows-1; row++) //Minus one and set equal to one so that broder is not included.
  36.     {
  37.         for(int col = 1; col < cols-1; col++)
  38.         {
  39.             world[row][col] = rand()%2;
  40.            
  41.         }
  42.     }
  43. }
  44.  
  45. void display(int world[ROWS][COLS], int world_2[ROWS][COLS], int& rows, int& cols)
  46. {
  47.     for(int row = 0; row < rows; row++)
  48.     {
  49.         for(int col = 0; col < cols; col++)
  50.         {
  51.             cout << setw(5) << world[row][col];
  52.         }
  53.         cout << endl;
  54.     }
  55.     cout << endl;
  56. }
  57.  
  58. void scanArray(int world[ROWS][COLS], int& rows, int& cols, int& n_count)
  59. {
  60.     int neighbor_count = 0;
  61.     for(int row = 1; row < rows-1; row++)
  62.     {
  63.         for(int col = 1; col < cols-1; col++)
  64.         {
  65.             neighbor_count = generation(world, rows, cols, n_count);
  66.             if(world[row][col] == 1){
  67.                 if(neighbor_count == 1 || neighbor_count == 0)
  68.                     world[col][row] = 0;
  69.                 if(neighbor_count >= 4)
  70.                     world[col][row] = 0;
  71.                 if(neighbor_count == 2 || neighbor_count == 3)
  72.                     world[col][row] = 1;
  73.                 }
  74.             if(world[row][col] == 0){
  75.                 if(neighbor_count == 3)
  76.                     world[row][col] = 1;
  77.             }
  78.  
  79.         }
  80.     }
  81. }
  82.  
  83. int generation(int world[ROWS][COLS], int& rows, int& cols, int& n_count)
  84. {
  85.    
  86.     if(world[cols][rows-1] == 1) // The cell above
  87.     n_count++;
  88.     if(world[cols-1][rows] == 1) // The cell to the left
  89.         n_count++;
  90.     if(world[cols+1][rows] == 1) // The cell to the right
  91.         n_count++;
  92.     if(world[cols][rows+1] == 1) // The cell below
  93.         n_count++;
  94.     if(world[cols-1][rows-1] == 1) // The cell diagonally upper left
  95.         n_count++;
  96.     if(world[cols-1][rows+1] == 1) // The cell diagonally lower left
  97.         n_count++;
  98.     if(world[cols+1][rows-1] == 1) // The cell diagonally upper right
  99.         n_count++;
  100.     if(world[cols+1][rows+1] == 1) // The cell diagonally lower right
  101.         n_count++;
  102.  
  103.     return n_count++;
  104.  
  105. }
Add Comment
Please, Sign In to add comment