Advertisement
Lawnknome

GoL

Apr 8th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.99 KB | None | 0 0
  1. /*********************************************************************
  2. ** Program Filename: Assignment1
  3. ** Author: Jared Hughes
  4. ** Date: 4/7/2015
  5. ** Description: Program demostrates oscillation, glider, and gun patterns
  6. **              in Conway's Game of Life.
  7. ** Input: User imputs type of pattern desired and beginning location
  8. **        on the grid.  They also input repeat commands.
  9. ** Output: Display outputs three different patterns on the screen.
  10. **         until 100 generations have passed or user quits.
  11. *********************************************************************/
  12. #include <iostream>
  13. #include <string>
  14. #include <cstdlib>
  15. #include <ctime>
  16. #include <stdlib.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19.  
  20. void array_copy(int array_copy[30][50], int array_paste [30][50]);
  21. void oscillation(int array_osc[30][50]);
  22. void glider(int array_glider[30][50]);
  23. void gun(int array_gun[30][50]);
  24. void life_rules(int array_rules[30][50]);
  25. void display_grid(int output_grid[30][50]);
  26. bool array_compare(int current_array[30][50], int previous_array[30][50]);
  27. void array_wipe(int array_blank[30][50]);
  28.  
  29.  
  30. int main()
  31. {
  32.     int my_array [30][50];                  //array that will be displayed to user
  33.     int previous [30][50];                  //array to compare current configuration to previous
  34.     char repeat;                            //allows user to repeat simulation
  35.     int choice;                             //menu choice
  36.     int generation;                         //keeps track of how many generations have passed.
  37.     bool same;                              //value to stop simulation if pattern is stable
  38.  
  39.    
  40.      //Explains the rules of the game to the user.
  41.     std::cout
  42.          << std::endl << "This is a simulation of the \"Game of Life\"." << std::endl
  43.          << std::endl << "These are the rules of the \"Game of Life\":" << std::endl
  44.          << std::endl << "1. If an occupied cell has zero or one neighbor, it dies of loneliness."
  45.          << std::endl << "2. If an occupied cell has more than three neighbors, it dies of overcrowding."
  46.          << 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."
  47.          << std::endl << "4. Births and deaths are instantaneous and occur at the changes of generation."
  48.          << std::endl << std::endl;
  49.          
  50.     std::cout << "There are three choices to the starting pattern of your cells:" << std::endl;
  51.     std::cout << "1. Simple oscillation." << std::endl;
  52.     std::cout << "2. Glider pattern." << std::endl;
  53.     std::cout << "3. Gun pattern." << std::endl;
  54.     std::cout << "4. Quit program" << std::endl;
  55.     std::cout << "If the pattern becomes \"stable\" the program will end." << std::endl;
  56.  
  57.     //loop to allow the user to run a new simulation
  58.     do
  59.     {
  60.         //loop assigns all spaces a blank position to start the simulation
  61.         //the actual grid is larger than the displayed grid to account for "infinite grid"
  62.         array_wipe(my_array);
  63.         array_wipe(previous);
  64.         generation = 0;
  65.  
  66.         //gets input choice from the user, if 4 is entered program terminates.
  67.         do
  68.         {
  69.  
  70.             std::cout << "Which pattern would you like to simulate?" << std::endl;
  71.             std::cout << "Or press 4 to quit." << std::endl;
  72.             std::cin >> choice;
  73.  
  74.             if(choice == 1)
  75.             {
  76.                 oscillation(my_array);
  77.             }
  78.            
  79.             if(choice == 2)
  80.             {
  81.                 glider(my_array);
  82.             }
  83.            
  84.             if(choice == 3)
  85.             {
  86.                 gun(my_array);
  87.             }
  88.            
  89.             if(choice == 4)
  90.             {
  91.                 return 0;
  92.             }
  93.        
  94.         }while(choice < 1 || choice > 4);
  95.  
  96.         do{
  97.            
  98.             array_copy(my_array, previous);
  99.             display_grid(my_array);         //displays current grid
  100.             life_rules(my_array);           //runs the cell life/death rules on current grid
  101.             usleep(200000);             //allows for the system slow down the generation of new cells
  102.             generation++;                   //increments the generation for tracking purposes.
  103.  
  104.             if(generation > 100)
  105.             {
  106.                 break;
  107.             }
  108.  
  109.            
  110.             same = array_compare(my_array, previous);       //compares current grid to previous generation grid
  111.             if(same == true)                            //if they are the same, simulation ends
  112.                 std::cout << std::endl;
  113.             if(same == false)
  114.                 std::cout << std::endl;
  115.            
  116.         }while(same == false); 
  117.        
  118.         std::cout << "Would you like to run another simulation?" << std::endl;
  119.         std::cin >> repeat;
  120.  
  121.     }while(repeat == 'y' || repeat == 'Y' && repeat != 'n');
  122.  
  123.     return 0;
  124. }
  125.  
  126.  
  127. /*******************************************************************************
  128. ** Function: void oscillation
  129. ** Description: inputs the simple oscillation pattern onto the grid
  130. ** Parameters: the first array is needed to run the function
  131. ** Pre-Conditions: User selects oscillation from the menu
  132. ** Post-Conditions: Grid is updated with location of 1x3 occpied cells on grid
  133. ********************************************************************************/
  134. void oscillation(int array_osc[30][50])
  135. {
  136.     int coord_x;
  137.     int coord_y;
  138.    
  139.     std::cout << "This demonstration of a simple oscillation will be "
  140.               << "a 1x3 grouping of cells to start." << std::endl;
  141.     std::cout << "Please select the location you would like to center the cells within the 40x20 grid."
  142.               << std:: endl;
  143.     do
  144.     {
  145.         std::cout << "Please enter an X-axis coordinate between 2 and 39." << std::endl;
  146.         std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
  147.         std::cin >> coord_x;
  148.     }while(coord_x < 2 || coord_x > 39);     
  149.          
  150.     do
  151.     {
  152.         std::cout << "Please enter an Y-axis coordinate between 2 and 19." << std::endl;
  153.         std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
  154.         std::cin >> coord_y;
  155.     }while(coord_y < 2 || coord_y > 19);   
  156.  
  157.     array_osc[coord_y+4][coord_x+4] = 1;
  158.     array_osc[coord_y+4][coord_x+3] = 1;
  159.     array_osc[coord_y+4][coord_x+5] = 1;
  160. }
  161.  
  162. /*******************************************************************************
  163. ** Function: void glider
  164. ** Description: inputs the glider pattern onto the active grid
  165. ** Parameters: the first array is needed to run the function
  166. ** Pre-Conditions: User selects glider from the menu
  167. ** Post-Conditions: Grid is updated with location of a glider that will move
  168. **                  after each cycle of generations until outside the active grid.
  169. ********************************************************************************/
  170. void glider(int array_glider[30][50])
  171. {
  172.     int coord_x;
  173.     int coord_y;
  174.    
  175.     std::cout << "Please select the location you would like to center the cells within the 40x20 grid."
  176.               << std:: endl;
  177.     do
  178.     {
  179.         std::cout << "Please enter an X-axis coordinate between 2 and 39." << std::endl;
  180.         std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
  181.         std::cin >> coord_x;
  182.     }while(coord_x < 2 || coord_x > 39);     
  183.          
  184.     do
  185.     {
  186.         std::cout << "Please enter an Y-axis coordinate between 2 and 19." << std::endl;
  187.         std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
  188.         std::cin >> coord_y;
  189.     }while(coord_y < 2 || coord_y > 19);   
  190.  
  191.     array_glider[coord_y+4][coord_x+4] = 1;
  192.     array_glider[coord_y+4][coord_x+5] = 1;
  193.     array_glider[coord_y+3][coord_x+3] = 1;
  194.     array_glider[coord_y+5][coord_x+4] = 1;
  195.     array_glider[coord_y+5][coord_x+3] = 1;
  196. }
  197.  
  198. void gun(int array_gun[30][50])
  199. {
  200.     int coord_x;
  201.     int coord_y;
  202.    
  203.     std::cout << "Please select the location you would like to put the left square of the gun pattern within the 40x20 grid."
  204.               << std:: endl;
  205.     do
  206.     {
  207.         std::cout << "Please enter an X-axis coordinate between 1 and 5." << std::endl;
  208.         std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
  209.         std::cin >> coord_x;
  210.     }while(coord_x < 1 || coord_x > 5);  
  211.          
  212.     do
  213.     {
  214.         std::cout << "Please enter an Y-axis coordinate between 5 and 16." << std::endl;
  215.         std::cout << "This will make sure the pattern will start on the displayed grid." << std::endl;
  216.         std::cin >> coord_y;
  217.     }while(coord_y < 5 || coord_y > 16);   
  218.  
  219.     //hard codes location of left square
  220.     array_gun[coord_y+4][coord_x+4] = 1;
  221.     array_gun[coord_y+4][coord_x+5] = 1;
  222.     array_gun[coord_y+5][coord_x+4] = 1;
  223.     array_gun[coord_y+5][coord_x+5] = 1;
  224.    
  225.     /*********************************
  226.     *   Hard codes shape
  227.     *       ##
  228.     *     #    
  229.     *   #
  230.     *   #       #
  231.     *   #  
  232.     *     #
  233.     *       ##
  234.     **********************************/
  235.     array_gun[coord_y+4][coord_x+14] = 1;
  236.     array_gun[coord_y+5][coord_x+14] = 1;
  237.     array_gun[coord_y+6][coord_x+14] = 1;
  238.     array_gun[coord_y+3][coord_x+15] = 1;
  239.     array_gun[coord_y+7][coord_x+15] = 1;
  240.     array_gun[coord_y+2][coord_x+16] = 1;
  241.     array_gun[coord_y+8][coord_x+16] = 1;
  242.     array_gun[coord_y+2][coord_x+17] = 1;
  243.     array_gun[coord_y+8][coord_x+17] = 1;
  244.     array_gun[coord_y+5][coord_x+18] = 1;
  245.    
  246.     /***********************************
  247.     *   #
  248.     *     #
  249.     *     ##
  250.     *     #
  251.     *   #  
  252.     ***********************************/
  253.     array_gun[coord_y+3][coord_x+19] = 1;
  254.     array_gun[coord_y+7][coord_x+19] = 1;
  255.     array_gun[coord_y+4][coord_x+20] = 1;
  256.     array_gun[coord_y+5][coord_x+20] = 1;
  257.     array_gun[coord_y+6][coord_x+20] = 1;
  258.     array_gun[coord_y+5][coord_x+21] = 1;
  259.    
  260.     /***********************************
  261.     *   Hard codes shape
  262.     *               #
  263.     *               #
  264.     *           #  
  265.     *       # #  
  266.     *       # #
  267.     *       # #
  268.     *           #
  269.     *               #
  270.     *               #    
  271.     ***********************************/
  272.     array_gun[coord_y+4][coord_x+24] = 1;
  273.     array_gun[coord_y+3][coord_x+24] = 1;
  274.     array_gun[coord_y+2][coord_x+24] = 1;
  275.     array_gun[coord_y+4][coord_x+25] = 1;
  276.     array_gun[coord_y+3][coord_x+25] = 1;
  277.     array_gun[coord_y+2][coord_x+25] = 1;
  278.     array_gun[coord_y+1][coord_x+26] = 1;
  279.     array_gun[coord_y+5][coord_x+26] = 1;
  280.     array_gun[coord_y+5][coord_x+28] = 1;
  281.     array_gun[coord_y+1][coord_x+28] = 1;
  282.     array_gun[coord_y][coord_x+28] = 1;
  283.     array_gun[coord_y+6][coord_x+28] = 1;
  284.    
  285.     //hard codes location of right square
  286.     array_gun[coord_y+2][coord_x+38] = 1;
  287.     array_gun[coord_y+3][coord_x+38] = 1;
  288.     array_gun[coord_y+2][coord_x+39] = 1;
  289.     array_gun[coord_y+3][coord_x+39] = 1;
  290.  
  291. }
  292.  
  293.  
  294. /*********************************************************************
  295. ** Function: array_copy
  296. ** Description: copys the active array to a temporary one not seen by
  297. **              the user.
  298. ** Parameters: requires 2 arrays to be passed, one to copy one to paste
  299. ** Pre-Conditions: 2 arrays.
  300. ** Post-Conditions: First array is copied to second array.
  301. *********************************************************************/
  302. void array_copy(int array_copy[30][50], int array_paste [30][50])
  303. {
  304.     for(int j = 0; j < 30; j++)
  305.     {
  306.         for(int i = 0; i < 50; i++)            
  307.             array_paste[j][i] = array_copy[j][i];
  308.     }
  309. }
  310.  
  311.  
  312.  
  313. /*********************************************************************
  314. ** Function: life_rules
  315. ** Description: Runs the game of life rules.  Each cell has each of 8
  316. **              adjacent cells checked for occupancy.
  317. ** Parameters: active array (grid) with pattern input
  318. ** Pre-Conditions: Grid needs to have something on it to act on
  319. ** Post-Conditions: Grid may or may not change depending on rules
  320. *********************************************************************/
  321. void life_rules(int array_rules[30][50])
  322. {
  323.     int temp_array [30][50];
  324.     array_copy(array_rules, temp_array);            //copies the passed array to a temp array to hold the values.
  325.  
  326.     for(int j = 1; j < 29; j++)
  327.     {
  328.         for(int i = 1; i < 49; i++)            
  329.         {  
  330.             //checks the surrounding cells for other alive or dead cells(1 alive, 0 dead)
  331.             int count = 0;
  332.             count = array_rules[j-1][i] + array_rules[j-1][i-1] + array_rules[j][i-1] + array_rules[j+1][i-1] +
  333.                     array_rules[j+1][i] + array_rules[j+1][i+1] + array_rules[j][i+1] + array_rules[j-1][i+1];
  334.                 //If the cell has exactly 2 neighbors, nothing happens to it.
  335.                  if(count == 2)
  336.                     temp_array[j][i] = array_rules[j][i];
  337.                 //if the cell has 3 neighbors it is born, or if it is already alive it stays the same.
  338.                 if(count == 3)
  339.                     temp_array[j][i] = 1;
  340.                 //if the cell has 1 or less neighbors, or more than 3 neighbors he dies
  341.                 if(count < 2 || count > 3)
  342.                     temp_array[j][i] = 0;          
  343.         }
  344.     }
  345.  
  346.     //copies the temporary grid back to the display grid to show the user.
  347.     array_copy(temp_array, array_rules);
  348. }
  349.  
  350. /*********************************************************************
  351. ** Function: display_grid
  352. ** Description: Displays the grid to the user.
  353. ** Parameters: active array with pattern
  354. ** Pre-Conditions: None
  355. ** Post-Conditions: Pattern is displayed on screen
  356. *********************************************************************/
  357. void display_grid(int output_grid[30][50])
  358. {
  359.     for(int x=5; x<24; x++)             //This loops on the rows.
  360.     {  
  361.         for(int y=5; y<44; y++)         //This loops on the columns
  362.         {
  363.             if(output_grid[x][y] == 1)     
  364.                 std::cout << '*';
  365.             else
  366.                 std::cout << ' ';
  367.         }
  368.        
  369.         std::cout << std::endl;
  370.     }
  371. }
  372.  
  373. /*****************************************************************************
  374. ** Function: array_compare
  375. ** Description: checks to see if the array has a stable pattern.
  376. **              stable pattern means no change through a generation to
  377. **              the next.
  378. ** Parameters: active array with pattern and previous generations grid
  379. ** Pre-Conditions: None
  380. ** Post-Conditions: If the same returns a true value which will stop program.
  381. *****************************************************************************/
  382. bool array_compare(int current_array[30][50], int previous_array[30][50])
  383. {
  384.     int tally = 0;
  385.     for(int j = 0; j < 30; j++)
  386.     {
  387.         for(int i = 0; i < 50; i++)
  388.         {
  389.             if(current_array[j][i] == previous_array[j][i])
  390.                 tally++;   
  391.         }
  392.     }
  393.  
  394.     //whenever the locations are matching, the tally goes up.  If tally equals the total number of possible
  395.     //cells, then the grids match perfectly and the pattern is stable.
  396.  
  397.     if(tally == 30*50)
  398.         return true;
  399.     else
  400.         return false;
  401. }
  402.  
  403. /*****************************************************************************
  404. ** Function: array_wipe
  405. ** Description: Wipes the passed array to a blank slate of only 0's
  406. ** Parameters: desired array to erase
  407. ** Pre-Conditions: None
  408. ** Post-Conditions: Array will have 0 for each element
  409. *****************************************************************************/
  410. void array_wipe(int array_blank[30][50])
  411. {
  412.     for(int x=0; x<30; x++)
  413.         for(int y=0; y<50; y++)        
  414.         {
  415.             array_blank[x][y] = 0;          //assigns a blank spot to all spaces to start
  416.         }
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement