Advertisement
Lawnknome

gameolife

Apr 6th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. void arrayCopy(int arrayCopy[25][45], int arrayPaste [25][45]);
  7. void oscillation(int arrayOsc[25][45]);
  8. void lifeRules(int arrayRules[25][45]);
  9. void displayGrid(int outputGrid[25][45]);
  10. bool arrayCompare(int currentArray[25][45], int previousArray[25][45]);
  11.  
  12.  
  13. int main()
  14. {
  15.     int myarray [25][45];                   //array that will be displayed to user
  16.     int previous [25][45];                  //array to compare current configuration to previous
  17.     char repeat;                            //allows user to repeat simulation
  18.     int choice;                             //menu choice
  19.     int generation;                         //keeps track of how many generations have passed.
  20.     bool same;                              //value to stop simulation if pattern is stable
  21.  
  22.    
  23.      //Explains the rules of the game to the user.
  24.     std::cout
  25.          << std::endl << "This is a simulation of the \"Game of Life\"." << std::endl
  26.          << std::endl << "These are the rules of the \"Game of Life\":" << std::endl
  27.          << std::endl << "1. If an occupied cell has zero or one neighbor, it dies of loneliness."
  28.          << std::endl << "2. If an occupied cell has more than three neighbors, it dies of overcrowding."
  29.          << 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."
  30.          << std::endl << "4. Births and deaths are instantaneous and occur at the changes of generation."
  31.          << std::endl << std::endl;
  32.          
  33.     std::cout << "There are three choices to the starting pattern of your cells:" << std::endl;
  34.     std::cout << "1. Simple oscillation." << std::endl;
  35.     std::cout << "2. Glider pattern." << std::endl;
  36.     std::cout << "3. Gun pattern." << std::endl;
  37.     std::cout << "4. Quit program" << std::endl;
  38.     std::cout << "If the pattern becomes \"stable\" the program will end." << std::endl;
  39.  
  40.     //loop to allow the user to run a new simulation
  41.     do
  42.     {
  43.         //gets input choice from the user, if 4 is entered program terminates.
  44.         do
  45.         {
  46.  
  47.             std::cout << "Which pattern would you like to simulate?" << std::endl;
  48.             std::cout << "Or press 4 to quit." << std::endl;
  49.             std::cin >> choice;
  50.  
  51.             if(choice == 4)
  52.             {
  53.                 return 0;
  54.             }
  55.        
  56.         }while(choice < 1 || choice > 4);
  57.  
  58.        
  59.         //loop assigns all spaces a blank position to start the simulation
  60.         //the actual grid is larger than the displayed grid to account for "infinite grid"
  61.         for(int x=0; x<20; x++)
  62.             for(int y=0; y<40; y++)        
  63.             {
  64.                 myarray[x][y] = 0;          //assigns a blank spot to all spaces to start
  65.             }
  66.        
  67.  
  68.         if(choice == 1)
  69.         {
  70.             oscillation(myarray);
  71.         }
  72.  
  73.         do{
  74.            
  75.             arrayCopy(myarray, previous);
  76.             displayGrid(myarray);           //displays current grid
  77.             lifeRules(myarray);             //runes the cell life/death rules on current grid
  78.             system("sleep .2");             //allows for the system slow down the generation of new cells
  79.             generation++;
  80.  
  81.             if(generation > 100)
  82.             {
  83.                 break;
  84.             }
  85.  
  86.            
  87.             same = arrayCompare(myarray, previous);     //compares current grid to previous generation grid
  88.             if(same == true)                            //if they are the same, simulation ends
  89.                 std::cout << std::endl;
  90.             if(same == false)
  91.                 system("clear");
  92.            
  93.         }while(same == false); 
  94.        
  95.         std::cout << "Would you like to run another simulation?" << std::endl;
  96.         std::cin >> repeat;
  97.  
  98.     }while(repeat == 'y' || repeat == 'Y' && repeat != 'n');
  99.  
  100.     return 0;
  101. }
  102.  
  103.  
  104. //runs the oscillation pattern
  105. void oscillation(int arrayOsc[25][45])
  106. {
  107.     int coordX;
  108.     int coordY;
  109.     //int tempArray [25][45];
  110.     //arrayCopy(arrayOsc, tempArray);           //copies the passed array to a temp array to hold the values.
  111.  
  112.     std::cout << "This demonstration of a simple oscillation will be "
  113.               << "a 1x3 grouping of cells to start." << std::endl;
  114.     std::cout << "Please select the location you would like to center the cells within the 40x20 grid."
  115.               << std:: endl;
  116.     do
  117.     {
  118.         std::cout << "Please enter an X-axis coordinate between 2 and 39." << std::endl;
  119.         std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
  120.         std::cin >> coordX;
  121.     }while(coordX < 2 || coordX > 39);   
  122.          
  123.     do
  124.     {
  125.         std::cout << "Please enter an Y-axis coordinate between 2 and 19." << std::endl;
  126.         std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
  127.         std::cin >> coordY;
  128.     }while(coordY < 2 || coordY > 19); 
  129.  
  130.     arrayOsc[coordY][coordX] = 1;
  131.     arrayOsc[coordY][coordX-1] = 1;
  132.     arrayOsc[coordY][coordX+1] = 1;
  133. }
  134.  
  135.  
  136.  
  137. //copies the array to a temporary array for puposes of grid calculation (in real time)
  138. void arrayCopy(int arrayCopy[25][45], int arrayPaste [25][45])
  139. {
  140.     for(int j = 0; j < 25; j++)
  141.     {
  142.         for(int i = 0; i < 45; i++)            
  143.             arrayPaste[j][i] = arrayCopy[j][i];
  144.     }
  145. }
  146.  
  147.  
  148.  
  149.  
  150. void lifeRules(int arrayRules[25][45])
  151. {
  152.     int tempArray [25][45];
  153.     arrayCopy(arrayRules, tempArray);           //copies the passed array to a temp array to hold the values.
  154.  
  155.     for(int j = 0; j < 25; j++)
  156.     {
  157.         for(int i = 0; i < 45; i++)            
  158.         {  
  159.             //checks the surrounding cells for other alive or dead cells(1 alive, 0 dead)
  160.             int count = 0;
  161.             count = arrayRules[j-1][i] + arrayRules[j-1][i-1] + arrayRules[j][i-1] + arrayRules[j+1][i-1] +
  162.                     arrayRules[j+1][i] + arrayRules[j+1][i+1] + arrayRules[j][i+1] + arrayRules[j-1][i+1];
  163.                 //If the cell has exactly 2 neighbors, nothing happens to it.
  164.                  if(count == 2)
  165.                     tempArray[j][i] = arrayRules[j][i];
  166.                 //if the cell has 3 neighbors it is born, or if it is already alive it stays the same.
  167.                 if(count == 3)
  168.                     tempArray[j][i] = 1;
  169.                 //if the cell has 1 or less neighbors, or more than 3 neighbors he dies
  170.                 if(count < 2 || count > 3)
  171.                     tempArray[j][i] = 0;
  172.                    
  173.         }
  174.     }
  175.     //copies the temporary grid back to the display grid to show the user.
  176.     arrayCopy(arrayRules, tempArray);
  177. }
  178.  
  179. void displayGrid(int outputGrid[25][45])
  180. {
  181.     for(int x=0; x<20; x++)             //This loops on the rows.
  182.     {  
  183.         for(int y=0; y<40; y++)         //This loops on the columns
  184.         {
  185.             if(outputGrid[x][y] == 1)      
  186.                 std::cout << '*';
  187.             else
  188.                 std::cout << ' ';
  189.         }
  190.        
  191.         std::cout << std::endl;
  192.     }
  193. }
  194.  
  195. bool arrayCompare(int currentArray[25][45], int previousArray[25][45])
  196. {
  197.     int tally = 0;
  198.     for(int j = 0; j < 25; j++)
  199.     {
  200.         for(int i = 0; i < 45; i++)
  201.         {
  202.             if(currentArray[j][i] == previousArray[j][i])
  203.                 tally++;   
  204.         }
  205.     }
  206.  
  207.     //whenever the locations are matching, the tally goes up.  If tally equals the total number of possible
  208.     //cells, then the grids match perfectly and the pattern is stable.
  209.  
  210.     if(tally == 25*45)
  211.         return true;
  212.     else
  213.         return false;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement