fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4.  
  5. using namespace std;
  6.  
  7. struct params_t{
  8. int world_size; // size of the world (side of the square world)
  9. double initial_proportion; // initial proportion of living cells
  10. int max_t; // maximum time for simulation
  11. int delay; // period in terms of the number of time steps before
  12. // each display of the world (to make more efficient
  13. // outputs)
  14. };
  15.  
  16. void initialize_world(vector< vector<int> > &world, double p)
  17. {
  18. int i, j, n;
  19.  
  20. n = world.size();
  21.  
  22. // generate each cell to be alive with probability p
  23.  
  24. for (i=0; i<n; i++)
  25. for (j=0; j<n; j++)
  26. {
  27. if (drand48()<p)
  28. world[i][j] = 1;
  29. else
  30. world[i][j] = 0;
  31. }
  32. }
  33.  
  34. int count_neighbors(vector< vector<int> > &world,
  35. int i,
  36. int j,
  37. int n)
  38. {
  39. int di, dj;
  40. int num_neighbors=0;
  41.  
  42. // note that this assumes wrap-around world, in which
  43. // the last cell in any row is a neighbor of the very
  44. // first cell in the row; similarly for the columns
  45.  
  46. for (di = -1; di <= 1; di++)
  47. for (dj = -1; dj <= 1; dj++)
  48. if ((di!=0)||(dj!=0))
  49. num_neighbors += world[(i+di+n)%n][(j+dj+n)%n];
  50.  
  51. // return the number of neighbors
  52.  
  53. return num_neighbors;
  54. }
  55.  
  56. void process_world(vector< vector<int> > &new_world,
  57. vector< vector<int> > &world)
  58. {
  59. int i, j;
  60. int n = world.size();
  61.  
  62. // count neighbors of every cell, and generate the new
  63. // world using the basic rules of the game of life
  64. // - living cell dies if less than 2 living neighbors
  65. // - living cell dies if more than 3 living neighbors
  66. // - dead cell changes to living if exactly 3 neighbors
  67. // - otherwise, no change
  68.  
  69. for (i=0; i<n; i++)
  70. for (j=0; j<n; j++)
  71. {
  72. int num_neighbors = count_neighbors(world, i, j, n);
  73.  
  74. // these conditions implement the rules
  75.  
  76. if (world[i][j]==1) // living cell
  77. {
  78. if (num_neighbors<2)
  79. new_world[i][j] = 0; // died because of underpopulation
  80. else
  81. if (num_neighbors>3)
  82. new_world[i][j] = 0; // died because of overcrowding
  83. else
  84. new_world[i][j] = 1; // cell goes on living
  85. }
  86. else
  87. if (num_neighbors==3) // dead cell
  88. new_world[i][j] = 1; // reproduction (new cell)
  89. else
  90. new_world[i][j] = 0; // remains empty cell
  91. }
  92. }
  93.  
  94. void display_world(vector< vector<int> > &world)
  95. {
  96. int i, j;
  97. int n = world.size();
  98. int num_living = 0;
  99. int num_dead = 0;
  100.  
  101. // show the content of each cell in a reasonably readable format
  102. // (as a matrix with * denoting a living cell)
  103.  
  104. for (i=0; i<n; i++)
  105. {
  106. for (j=0; j<n; j++)
  107. if (world[i][j]==1)
  108. {
  109. cout << "* ";
  110. num_living++;
  111. }
  112. else
  113. {
  114. cout << " ";
  115. num_dead++;
  116. }
  117. cout << endl;
  118. }
  119.  
  120. cout << endl;
  121. cout << "num_living = " << num_living << endl;
  122. cout << "num_dead = " << num_dead << endl;
  123. cout << endl;
  124. }
  125.  
  126. void run_simulation(params_t &params)
  127. {
  128. int i, t;
  129. vector< vector<int> > world(params.world_size);
  130. vector< vector<int> > new_world(params.world_size);
  131.  
  132. // runs the simulation
  133.  
  134. for (i=0; i<params.world_size; i++)
  135. {
  136. world[i].resize(params.world_size);
  137. new_world[i].resize(params.world_size);
  138. }
  139.  
  140. // initialize the world
  141.  
  142. initialize_world(world, params.initial_proportion);
  143.  
  144. // print out the initial world
  145.  
  146. cout << "----------------------------------------------------------" << endl;
  147. cout << "INITIAL WORLD" << endl << endl;
  148. display_world(world);
  149.  
  150. for (t=1; t<=params.max_t; t++)
  151. {
  152. // create new world using the old world and the rules
  153.  
  154. process_world(new_world, world);
  155.  
  156. // new world becomes current world
  157.  
  158. swap(world, new_world);
  159.  
  160. // print out the world (once in each params.delay steps)
  161.  
  162. if ((t-1)%params.delay==0)
  163. {
  164. cout << "----------------------------------------------------------" << endl;
  165. cout << "WORLD AT TIME " << t << endl << endl;
  166. display_world(world);
  167. }
  168. }
  169. }
  170.  
  171.  
  172. int main()
  173. {
  174. params_t params;
  175.  
  176. // initialize random number generator
  177.  
  178. srand48(123);
  179.  
  180. // read user parameters
  181.  
  182. cin >> params.world_size;
  183. cin >> params.initial_proportion;
  184. cin >> params.max_t;
  185. cin >> params.delay;
  186.  
  187. // adjust parameters if necessary
  188.  
  189. if (params.world_size<1)
  190. params.world_size=1;
  191. if (params.initial_proportion<0)
  192. params.initial_proportion=0;
  193. if (params.initial_proportion>1)
  194. params.initial_proportion=1;
  195. if (params.max_t<1)
  196. params.max_t=1;
  197. if (params.delay<1)
  198. params.delay=1;
  199.  
  200. // print out parameters
  201.  
  202. cout << "World size: " << params.world_size << endl;
  203. cout << "Initial proportion: " << params.initial_proportion << endl;
  204. cout << "Maximum time steps: " << params.max_t << endl;
  205. cout << "Reporting period: " << params.delay << endl;
  206. cout << endl;
  207.  
  208. // run the game of life simulation
  209.  
  210. run_simulation(params);
  211.  
  212. // return 0 from main
  213.  
  214. return 0;
  215. }
  216.  
Success #stdin #stdout 0.02s 2868KB
stdin
20
0.2
10
1
stdout
World size:         20
Initial proportion: 0.2
Maximum time steps: 10
Reporting period:   1

----------------------------------------------------------
INITIAL WORLD

      * *         *       *   *       * 
              *         *               
    *       *     *         *       *   
                              * *       
*       *               *               
  *       *   * *                     * 
          *           *   *           * 
*   *           *                   * * 
                                      * 
  *               * *     *       *     
                      *           *   * 
      * * *                             
                  *             *   *   
        *                   *     *   * 
            * *       *       *         
  *                   *     *           
      * *                               
        *       *           *   *       
        *                               
    *     *   *   * *           *       

num_living = 71
num_dead =   329

----------------------------------------------------------
WORLD AT TIME 1

      * *   *     * *                   
      *         *         * *           
                              *         
                              *         
*                                       
        * * *           *             * 
  *         * * *                       
*                                   *   
  *               *                   * 
*                   *                   
        *           *               *   
        *                         * *   
      *   *                       * *   
                              * * * *   
                            * *         
                                        
      * *                     *         
        * *                             
      * * *     * *           *         
          *     * * *                   

num_living = 59
num_dead =   341

----------------------------------------------------------
WORLD AT TIME 2

      * * *   *     *                   
      * *         *         *           
                              *         
                                        
          *                             
*         * *                           
*           * *                       * 
* *           * *                     * 
  *                                   * 
*                 * *                 * 
                                  * * * 
      * * *                           * 
        *                             * 
                            * *     *   
                            * *   *     
                            * *         
      * * *                             
                                        
      *     *   *   *                   
            * *                         

num_living = 55
num_dead =   345

----------------------------------------------------------
WORLD AT TIME 3

      *   *   * *                       
      *   *                             
                                        
                                        
          * *                           
*         *   *                       * 
          *     *                       
  *         * * *                   *   
  *             * *                 *   
                                        
        *                               
*     * * *                           * 
      * * *                         * * 
                            * * *   *   
                          *             
        *                   * * *       
        *                               
      *   *                             
            *                           
      *         * *                     

num_living = 50
num_dead =   350

----------------------------------------------------------
WORLD AT TIME 4

    * *     * * * *                     
            *                           
                                        
                                        
          * *                           
        * *   *                         
*         *     *                     * 
            *                           
                * *                     
                                        
      * * *                             
*                                   * * 
*     *   *                   *   * *   
        *                   * *   * * * 
                          *       *     
                            * *         
      * * *                   *         
        * *                             
        *                               
        *   *   * *                     

num_living = 52
num_dead =   348

----------------------------------------------------------
WORLD AT TIME 5

      *     *     *                     
            *   *                       
                                        
                                        
        * * *                           
        *     *                         
        * *   *                         
              * * *                     
                                        
        *                               
        *                             * 
*     *   *                       * *   
*       *                   * *         
        *                   * *       * 
                          *       *     
        *                   * * *       
      *   *                 * *         
                                        
      * *                               
        *   *     *                     

num_living = 47
num_dead =   353

----------------------------------------------------------
WORLD AT TIME 6

            *   * *                     
              *                         
                                        
          *                             
        * * *                           
      *       *                         
        * *   *                         
            * * *                       
                *                       
                                        
      * * *                         * * 
*     *   *                         *   
*     * * *                 * * *   *   
                          *   * *       
                          *             
        *                 *     *       
        *                   *   *       
      *                                 
      * * *                             
        *                               

num_living = 49
num_dead =   351

----------------------------------------------------------
WORLD AT TIME 7

              * *                       
              * *                       
                                        
        * * *                           
        * * *                           
      *       *                         
        * *                             
          * *   *                       
                *                       
        *                               
      *   *                         * * 
*   *       *                 *     *   
      *   *                 *   *       
        *                 *     * *     
                        * *   * *       
                          * * *         
      * *                     *         
      *   *                             
      *   *                             
      * *                               

num_living = 52
num_dead =   348

----------------------------------------------------------
WORLD AT TIME 8

              * *                       
              * *                       
          * * *                         
        *   *                           
      *       *                         
      *                                 
        * *   *                         
        * * * *                         
          *   *                         
        *                               
      * * *                         * * 
    * *   * *                 *   * *   
      * * *                 *   *       
        *               * *       *     
                        *         *     
                        * *             
      * *                     *         
    * *   *                             
    * *   *                             
      * *                               

num_living = 58
num_dead =   342

----------------------------------------------------------
WORLD AT TIME 9

              * *                       
                                        
          *     *                       
        *                               
      * *                               
      *     *                           
      *       *                         
              * *                       
              *                         
      *     *                           
    *       *                     * * * 
    *       *                 * * * * * 
    *       *             * * * *   *   
      * * *             * *     * *     
                      *                 
                        * *             
    * * *                               
          *                             
          *                             
    * * *                               

num_living = 53
num_dead =   347

----------------------------------------------------------
WORLD AT TIME 10

      *                                 
              * *                       
                                        
      * * *                             
      * * *                             
    * *                                 
            * * *                       
            * * *                       
            * * *                       
            * *                     *   
    * *   * * *                       * 
  * * *   * * *                         
    *   *   *           * *           * 
      * * *             * *     * *     
        *             *                 
      *                 *               
      * *                               
      *   *                             
      *   *                             
      * *                               

num_living = 60
num_dead =   340