Advertisement
Lawnknome

GOL

Apr 6th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6.  
  7. int main()
  8. {
  9.     int myarray [25][45];
  10.     int arrayCopy [25][45];
  11.     char repeat;
  12.     int choice;
  13.  
  14.    
  15.      //Explains the rules of the game to the user.
  16.     std::cout
  17.          << std::endl << "This is a simulation of the \"Game of Life\"." << std::endl
  18.          << std::endl << "These are the rules of the \"Game of Life\":" << std::endl
  19.          << std::endl << "1. If an occupied cell has zero or one neighbor, it dies of loneliness."
  20.          << std::endl << "2. If an occupied cell has more than three neighbors, it dies of overcrowding."
  21.          << std::endl << "3. If an empty cell has exactly three occupied neighbor cells, there is a birth of a new cell to replace the empty cell."
  22.          << std::endl << "4. Births and deaths are instantaneous and occur at the changes of generation."
  23.          << std::endl << std::endl;
  24.          
  25.     std::cout << "There are three choices to the starting pattern of your cells:" << std::endl;
  26.     std::cout << "1. Simple oscillation." << std::endl;
  27.     std::cout << "2. Glider pattern." << std::endl;
  28.     std::cout << "3. Gun pattern." << std::endl;
  29.     std::cout << "4. Quit program" << std::endl;
  30.     std::cout << "If the pattern becomes \"stable\" the program will end." << std::endl;
  31.  
  32.     //loop to allow the user to run a new simulation
  33.     do
  34.     {
  35.         //gets input choice from the user, if 4 is entered program terminates.
  36.         do
  37.         {
  38.  
  39.             std::cout << "Which pattern would you like to simulate?" << std::endl;
  40.             std::cout << "Or press 4 to quit." << std::endl;
  41.             std::cin >> choice;
  42.  
  43.             if(choice == 4)
  44.             {
  45.                 return 0;
  46.             }
  47.        
  48.         }while(choice < 1 || choice > 4);
  49.  
  50.        
  51.         //loop assigns all spaces a blank position to start the simulation
  52.         //the actual grid is larger than the displayed grid to account for "infinite grid"
  53.         for(int x=0; x<20; x++)
  54.             for(int y=0; y<40; y++)        
  55.             {
  56.                 myarray[x][y] = 0;          //assigns a blank spot to all spaces to start
  57.             }
  58.        
  59.         for(int x=0; x<20; x++)             //This loops on the rows.
  60.         {  
  61.             for(int y=0; y<40; y++)         //This loops on the columns
  62.             {
  63.                 if(myarray[x][y] == 1)     
  64.                     std::cout << '*';
  65.                 else
  66.                     std::cout << ' ';
  67.             }
  68.             std::cout << std::endl;
  69.         }
  70.  
  71.         std::cout << "Would you like to run another simulation?" << std::endl;
  72.         std::cin >> repeat;
  73.  
  74.     }while(repeat == 'y' || repeat == 'Y' && repeat != 'n');
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement