Guest User

Untitled

a guest
Oct 9th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #include <conio.h>      // specialized I/O functions
  6. #include <memory.h>
  7. #include <process.h>    // functions to deal with the operating system
  8. #include <time.h>       // contains functions related to the clock/time
  9.  
  10. void main ()
  11.     {
  12.     const   int NumCols (10);
  13.     const   int NumRows (10);
  14.     const   int MaxCols (NumCols + 2);  // add an outside band of cells that are always going to be unoccupied
  15.     const   int MaxRows (NumRows + 2);
  16.  
  17.     bool    Board   [MaxRows] [MaxCols];
  18.     bool    Board2  [MaxRows] [MaxCols];
  19.     time_t  EndTime;
  20.     int     Generation;
  21.     int     i;          //rows
  22.     int     j;          //columns
  23.     int livetest;       //variable to count up living neighbors if cell is alive
  24.     bool    TimedOut;
  25.     bool    KeyHit;
  26.     time_t  WaitTime (3);
  27.  
  28.     memset (Board, false, (MaxRows * MaxCols * sizeof (bool)));
  29.     memcpy (Board2, Board, (MaxRows * MaxCols * sizeof (bool)));    // copies info from Board to Board2
  30.     cout << "Enter the row (1 to " << NumRows << ") and col (1 to " << NumCols << ") position of each cell top be occupied" << endl;
  31.     cout << "Enter a negative number for each to stop" << endl;
  32.    
  33.     do  {
  34.         cout << "=> ";
  35.         cin >> i >> j;
  36.         if ((i  < 0) && (j < 0))
  37.                 break;
  38.             else
  39.                 if ((i <= 0) || (j <= 0) || (i > NumRows) || (j > NumCols))
  40.                         cout << "Invalid position, try again" << endl;
  41.                     else
  42.                             Board [i] [j] = true;
  43.         } while (true);
  44.  
  45.  
  46.     Generation = 0;
  47.     do  {
  48.         cout << "\tGeneration: " << Generation << endl;
  49.         for (i = 1; i <= NumRows; i++) // only display the inner set of cells, not the outside band
  50.             {
  51.             for (j = 1; j <= NumCols; j++)
  52.                 cout << (Board [i] [j] ? '*' : ' ');
  53.             cout << endl;
  54.             }
  55.        
  56.         /*  for each cell on the board
  57.         //      count how many neighbors are occupied
  58.         //      if current cell is occupied and num neighbors >= 4
  59.         //              it dies
  60.         //      if current cell is occupied and num neighbors <= 1
  61.         //              it dies
  62.         //      if current cell is not occupied and num neighbors == 3
  63.         //              it becomes alive
  64.                 if no match to above rules, cell remains unchanged
  65.         */
  66.  
  67.             for (i = 1; i <= NumRows; i++)
  68.             {
  69.                 for (j = 1, livetest = 0; j <= NumCols; j++)
  70.                 {
  71.                     if (Board [i] [j] == true)                  //Apply the rules if the cell is alive
  72.                     {
  73.                         if (Board [i-1] [j-1] == true)
  74.                             livetest ++;
  75.                         if (Board [i-1] [j] == true)
  76.                             livetest ++;
  77.                         if (Board [i-1] [j+1] == true)
  78.                             livetest ++;
  79.  
  80.                         if (Board [i] [j-1] == true)
  81.                             livetest ++;
  82.                         if (Board [i] [j+1] == true)
  83.                             livetest ++;
  84.  
  85.                         if (Board [i+1] [j-1] == true)
  86.                             livetest ++;
  87.                         if (Board [i+1] [j] == true)
  88.                             livetest ++;
  89.                         if (Board [i+1] [j+1] == true)
  90.                             livetest ++;
  91.  
  92.                         if ((livetest >= 4) || (livetest <= 1)) //If the current gen cell is alive and either has 4 or more neighbors or 1 or less neighbors, it dies and applies it to Board2.
  93.                             Board2 [i] [j] = false;
  94.                         else
  95.                             Board2 [i] [j] = true;              //If it doesn't fall under either, then it copies the alive status from the current generation to the next (Board2).
  96.                     }  
  97.                         else if (Board [i] [j] == false)        //Apply the rules of the game if the cell is dead.
  98.                         {
  99.                         if (Board [i-1] [j-1] == true)
  100.                             livetest ++;
  101.                         if (Board [i-1] [j] == true)
  102.                             livetest ++;
  103.                         if (Board [i-1] [j+1] == true)
  104.                             livetest ++;
  105.  
  106.                         if (Board [i] [j-1] == true)
  107.                             livetest ++;
  108.                         if (Board [i] [j+1] == true)
  109.                             livetest ++;
  110.  
  111.                         if (Board [i+1] [j-1] == true)
  112.                             livetest ++;
  113.                         if (Board [i+1] [j] == true)
  114.                             livetest ++;
  115.                         if (Board [i+1] [j+1] == true)
  116.                             livetest ++;
  117.  
  118.                         if (livetest == 3)                      //If the dead cell has 3 live neighbors, then it becomes a birth cell and the status is copied to Board2.
  119.                             Board2 [i] [j] = true;
  120.                         else                                    //If it doesn't, then it will remain dead for the next generation.
  121.                             Board2 [i] [j] = false;
  122.                         }
  123.                     }
  124.                 }
  125.  
  126.             memset (Board, false, (MaxRows * MaxCols * sizeof (bool)));     //sets all values of the current generation to 'false' or dead
  127.             memcpy (Board, Board2, (MaxRows * MaxCols * sizeof (bool)));    // copies info from Board2 (the next gen) to Board (current gen for the next iteration)
  128.             memset (Board2, false, (MaxRows * MaxCols * sizeof (bool)));    //clears the next generation's information as not to pollute the results for the loop
  129.  
  130.         EndTime = time (0) + WaitTime;  // time (0) returns the current time in seconds since Jan 1, 1970
  131.         do  {
  132.             TimedOut    = time (0) >= EndTime;
  133.             KeyHit      = _kbhit ();
  134.             } while (!TimedOut && !KeyHit);
  135.         if (KeyHit)
  136.                 _getch ();  // get the key off the keyboard
  137.             else;
  138.  
  139.         Generation++;
  140.         system ("cls");     // send a command to the operating system, cls clears the screen in Windows
  141.         } while (true);
  142.     }
Advertisement
Add Comment
Please, Sign In to add comment