monoteen

Conway

Nov 4th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4.  
  5. const int gridsize = 75; //Making this a global constant to avoid array issues.
  6.  
  7. void Display(bool grid[gridsize+1][gridsize+1]){
  8.     for(int a = 1; a < gridsize; a++){
  9.         for(int b = 1; b < gridsize; b++){
  10.             if(grid[a][b] == true){
  11.                 std::cout << " *";
  12.             }
  13.             else{
  14.                 std::cout << "  ";
  15.             }
  16.             if(b == gridsize-1){
  17.                 std::cout << std::endl;
  18.             }
  19.         }
  20.     }
  21. }
  22. //This copy's the grid for comparision purposes.
  23. void CopyGrid (bool grid[gridsize+1][gridsize+1],bool grid2[gridsize+1][gridsize+1]){
  24.     for(int a =0; a < gridsize; a++){
  25.         for(int b = 0; b < gridsize; b++){grid2[a][b] = grid[a][b];}
  26.     }
  27. }
  28. //Calculates Life or Death
  29. void liveOrDie(bool grid[gridsize+1][gridsize+1]){
  30.     bool grid2[gridsize+1][gridsize+1] = {};
  31.     CopyGrid(grid, grid2);
  32.     for(int a = 1; a < gridsize; a++){
  33.         for(int b = 1; b < gridsize; b++){
  34.             int life = 0;
  35.         for(int c = -1; c < 2; c++){
  36.             for(int d = -1; d < 2; d++){
  37.                 if(!(c == 0 && d == 0)){
  38.                     if(grid2[a+c][b+d]) {++life;}
  39.                 }
  40.             }
  41.         }
  42.             if(life < 2) {grid[a][b] = false;}
  43.             else if(life == 3){grid[a][b] = true;}
  44.             else if(life > 3){grid[a][b] = false;}
  45.         }
  46.     }
  47. }
  48.  
  49. int main(void)
  50. {
  51.  
  52.     //const int gridsize = 50;
  53.     bool grid[gridsize+1][gridsize+1] = {};
  54.  
  55.     //Still have to manually enter the starting cells.
  56.     grid[gridsize/2][gridsize/2] = true;
  57.     grid[gridsize/2-1][gridsize/2] = true;
  58.     grid[gridsize/2][gridsize/2+1] = true;
  59.     grid[gridsize/2][gridsize/2-1] = true;
  60.     grid[gridsize/2+1][gridsize/2+1] = true;
  61.  
  62.     while (true){
  63.         //The following copies our grid.
  64.  
  65.         Display(grid);     //This is our display.
  66.         liveOrDie(grid); //calculate if it lives or dies.
  67.         system("CLS");
  68.     }
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment