Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)

The Game Of Life: Windows API

By: GGMethos on Feb 6th, 2013  |  syntax: C++  |  size: 6.45 KB  |  hits: 101  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. //#include "life.h"
  5. #define HEIGHT 20
  6. #define WIDTH 60
  7. using namespace std;
  8.  
  9. //the shape that gliders, tenlines, and the actual board uses
  10. struct Shape {
  11. public:
  12.     char xCoord;
  13.     char yCoord;
  14.     char height;
  15.     char width;
  16.     char **figure;
  17. };
  18. struct Glider : public Shape {
  19.     static const char GLIDER_SIZE = 5;
  20.     Glider( char x , char y );
  21.     ~Glider();
  22. };
  23. struct tenline : public Shape {
  24.     static const char tenline_HEIGHT = 3;
  25.     static const char tenline_WIDTH = 1;
  26.     tenline( char x , char y );
  27.     ~tenline();
  28. };
  29. class GameOfLife {
  30. public:
  31.     GameOfLife( Shape sh );
  32.     void print();
  33.     void update();
  34.     char getState( char state , char xCoord , char yCoord , bool toggle);
  35.     void iterate(unsigned int iterations);
  36. private:
  37.     char world[HEIGHT][WIDTH];
  38.     char otherWorld[HEIGHT][WIDTH];
  39.     bool toggle;
  40.     Shape shape;
  41. };
  42. GameOfLife::GameOfLife( Shape sh ) :
  43.     shape(sh) ,
  44.     toggle(true)
  45. {
  46.     for ( char i = 0; i < HEIGHT; i++ ) {
  47.         for ( char j = 0; j < WIDTH; j++ ) {
  48.             world[i][j] = '.';
  49.         }
  50.     }
  51.     for ( char i = shape.yCoord; i - shape.yCoord < shape.height; i++ ) {
  52.         for ( char j = shape.xCoord; j - shape.xCoord < shape.width; j++ ) {
  53.             if ( i < HEIGHT && j < WIDTH ) {
  54.                 world[i][j] =
  55.                     shape.figure[ i - shape.yCoord ][j - shape.xCoord ];
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61.         //precondition: toggle must be true
  62.         //post-condition: status of coordinate on grid is printed to the screen
  63. void GameOfLife::print() {
  64.     if ( toggle ) {
  65.         for ( char i = 0; i < HEIGHT; i++ ) {
  66.             for ( char j = 0; j < WIDTH; j++ ) {
  67.                 std::cout << world[i][j];
  68.             }
  69.             std::cout << std::endl;
  70.         }
  71.     } else {
  72.         for ( char i = 0; i < HEIGHT; i++ ) {
  73.             for ( char j = 0; j < WIDTH; j++ ) {
  74.                 std::cout << otherWorld[i][j];
  75.             }
  76.             std::cout << std::endl;
  77.         }
  78.     }
  79.     for ( char i = 0; i < WIDTH; i++ ) {
  80.         std::cout << '=';
  81.     }
  82.     std::cout << std::endl;
  83. }
  84.  
  85.  
  86. //precondition: toggle must be true
  87. //post-condition: toggle is set to false
  88. void GameOfLife::update() {
  89.     if (toggle) {
  90.         for ( char i = 0; i < HEIGHT; i++ ) {
  91.             for ( char j = 0; j < WIDTH; j++ ) {
  92.                 otherWorld[i][j] =
  93.                     GameOfLife::getState(world[i][j] , i , j , toggle);
  94.             }
  95.         }
  96.         toggle = !toggle;
  97.     } else {
  98.         for ( char i = 0; i < HEIGHT; i++ ) {
  99.             for ( char j = 0; j < WIDTH; j++ ) {
  100.                 world[i][j] =
  101.                     GameOfLife::getState(otherWorld[i][j] , i , j , toggle);
  102.             }
  103.         }
  104.         toggle = !toggle;
  105.     }
  106. }
  107.  
  108.  
  109. //precondition: gameoflife board must be created
  110. //post condition: status of game board is read
  111. char GameOfLife::getState( char state, char yCoord, char xCoord, bool toggle ) {
  112.     char neighbors = 0;
  113.     if ( toggle ) {
  114.         for ( char i = yCoord - 1; i <= yCoord + 1; i++ ) {
  115.             for ( char j = xCoord - 1; j <= xCoord + 1; j++ ) {
  116.                 if ( i == yCoord && j == xCoord ) {
  117.                     continue;
  118.                 }
  119.                 if ( i > -1 && i < HEIGHT && j > -1 && j < WIDTH ) {
  120.                     if ( world[i][j] == 'X' ) {
  121.                         neighbors++;
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.     } else {
  127.         for ( char i = yCoord - 1; i <= yCoord + 1; i++ ) {
  128.             for ( char j = xCoord - 1; j <= xCoord + 1; j++ ) {
  129.                 if ( i == yCoord && j == xCoord ) {
  130.                     continue;
  131.                 }
  132.                 if ( i > -1 && i < HEIGHT && j > -1 && j < WIDTH ) {
  133.                     if ( otherWorld[i][j] == 'X' ) {
  134.                         neighbors++;
  135.                     }
  136.                 }
  137.             }
  138.         }
  139.     }
  140.     if (state == 'X') {
  141.         return ( neighbors > 1 && neighbors < 4 ) ? 'X' : '.';
  142.     }
  143.     else {
  144.         return ( neighbors == 3 ) ? 'X' : '.';
  145.     }
  146. }
  147.  
  148. void GameOfLife::iterate( unsigned int iterations ) {
  149.     for ( int i = 0; i < iterations; i++ ) {
  150.         print();
  151.         update();
  152.     }
  153. }
  154.  
  155. //precondition: none
  156. //post-condition: glider is created
  157. Glider::Glider( char x , char y ) {
  158.     xCoord = x;
  159.     yCoord = y;
  160.     height = GLIDER_SIZE;
  161.     width = GLIDER_SIZE;
  162.     figure = new char*[GLIDER_SIZE];
  163.     for ( char i = 0; i < GLIDER_SIZE; i++ ) {
  164.         figure[i] = new char[GLIDER_SIZE];
  165.     }
  166.     for ( char i = 0; i < GLIDER_SIZE; i++ ) {
  167.         for ( char j = 0; j < GLIDER_SIZE; j++ ) {
  168.             figure[i][j] = '.';
  169.         }
  170.     }
  171.     figure[0][1] = 'X';
  172.     figure[1][2] = 'X';
  173.     figure[2][0] = 'X';
  174.     figure[2][1] = 'X';
  175.     figure[2][2] = 'X';
  176. }
  177.  
  178.  
  179. //precondition: glider must exist
  180. //post-condition: glider is cleared
  181. Glider::~Glider() {
  182.     for ( char i = 0; i < GLIDER_SIZE; i++ ) {
  183.         delete[] figure[i];
  184.     }
  185.     delete[] figure;
  186. }
  187.  
  188. //precondition: none
  189. //post-condition: tenline created
  190. tenline::tenline( char x , char y ) {
  191.     xCoord = x;
  192.     yCoord = y;
  193.     height = tenline_HEIGHT;
  194.     width = tenline_WIDTH;
  195.     figure = new char*[tenline_HEIGHT];
  196.     for ( char i = 0; i < tenline_HEIGHT; i++ ) {
  197.         figure[i] = new char[tenline_WIDTH];
  198.     }
  199.     for ( char i = 0; i < tenline_HEIGHT; i++ ) {
  200.         for ( char j = 0; j < tenline_WIDTH; j++ ) {
  201.             figure[i][j] = 'X';
  202.         }
  203.     }
  204. }
  205.  
  206. //precondition: tenline must exist
  207. //post-condition: tenline is cleared
  208. tenline::~tenline() {
  209.     for ( char i = 0; i < tenline_HEIGHT; i++ ) {
  210.         delete[] figure[i];
  211.     }
  212.     delete[] figure;
  213. }
  214.  
  215.  
  216.  
  217. int main() {
  218.  
  219.         int iteratenum; //number of iterations
  220.         int gliderXusernum; //X coordinate of glider shape
  221.         int gliderYusernum; //Y coordinate of glider shape
  222.         cout << "With what position on the board shall we begin?" << endl;
  223.         cout << "Enter the X coordinate:" << endl;
  224.         cin >> gliderXusernum;
  225.         cout << "Enter the Y coordinate:" << endl;
  226.         cin >> gliderYusernum;
  227.         cout << "How many times would you like to run Conway's Game of Life?" << endl;
  228.         cin >> iteratenum;
  229.         cout << "Game will play " << iteratenum << " times... " << endl;
  230.     Glider glider(gliderXusernum,gliderYusernum);
  231.     GameOfLife gol(glider);
  232.     gol.iterate(iteratenum);
  233.     tenline tenline(2,4);
  234.         system("PAUSE");
  235.         return 1;
  236. }