"The Game Of Life: Windows API" By GGMethos (https://pastebin.com/u/GGMethos) URL: https://pastebin.com/6JhH4zXR Created on: Wednesday 6th of February 2013 12:37:25 PM CDT Retrieved on: Monday 26 of October 2020 05:56:21 AM UTC #include #include #include //#include "life.h" #define HEIGHT 20 #define WIDTH 60 using namespace std; //the shape that gliders, tenlines, and the actual board uses struct Shape { public: char xCoord; char yCoord; char height; char width; char **figure; }; struct Glider : public Shape { static const char GLIDER_SIZE = 5; Glider( char x , char y ); ~Glider(); }; struct tenline : public Shape { static const char tenline_HEIGHT = 3; static const char tenline_WIDTH = 1; tenline( char x , char y ); ~tenline(); }; class GameOfLife { public: GameOfLife( Shape sh ); void print(); void update(); char getState( char state , char xCoord , char yCoord , bool toggle); void iterate(unsigned int iterations); private: char world[HEIGHT][WIDTH]; char otherWorld[HEIGHT][WIDTH]; bool toggle; Shape shape; }; GameOfLife::GameOfLife( Shape sh ) : shape(sh) , toggle(true) { for ( char i = 0; i < HEIGHT; i++ ) { for ( char j = 0; j < WIDTH; j++ ) { world[i][j] = '.'; } } for ( char i = shape.yCoord; i - shape.yCoord < shape.height; i++ ) { for ( char j = shape.xCoord; j - shape.xCoord < shape.width; j++ ) { if ( i < HEIGHT && j < WIDTH ) { world[i][j] = shape.figure[ i - shape.yCoord ][j - shape.xCoord ]; } } } } //precondition: toggle must be true //post-condition: status of coordinate on grid is printed to the screen void GameOfLife::print() { if ( toggle ) { for ( char i = 0; i < HEIGHT; i++ ) { for ( char j = 0; j < WIDTH; j++ ) { std::cout << world[i][j]; } std::cout << std::endl; } } else { for ( char i = 0; i < HEIGHT; i++ ) { for ( char j = 0; j < WIDTH; j++ ) { std::cout << otherWorld[i][j]; } std::cout << std::endl; } } for ( char i = 0; i < WIDTH; i++ ) { std::cout << '='; } std::cout << std::endl; } //precondition: toggle must be true //post-condition: toggle is set to false void GameOfLife::update() { if (toggle) { for ( char i = 0; i < HEIGHT; i++ ) { for ( char j = 0; j < WIDTH; j++ ) { otherWorld[i][j] = GameOfLife::getState(world[i][j] , i , j , toggle); } } toggle = !toggle; } else { for ( char i = 0; i < HEIGHT; i++ ) { for ( char j = 0; j < WIDTH; j++ ) { world[i][j] = GameOfLife::getState(otherWorld[i][j] , i , j , toggle); } } toggle = !toggle; } } //precondition: gameoflife board must be created //post condition: status of game board is read char GameOfLife::getState( char state, char yCoord, char xCoord, bool toggle ) { char neighbors = 0; if ( toggle ) { for ( char i = yCoord - 1; i <= yCoord + 1; i++ ) { for ( char j = xCoord - 1; j <= xCoord + 1; j++ ) { if ( i == yCoord && j == xCoord ) { continue; } if ( i > -1 && i < HEIGHT && j > -1 && j < WIDTH ) { if ( world[i][j] == 'X' ) { neighbors++; } } } } } else { for ( char i = yCoord - 1; i <= yCoord + 1; i++ ) { for ( char j = xCoord - 1; j <= xCoord + 1; j++ ) { if ( i == yCoord && j == xCoord ) { continue; } if ( i > -1 && i < HEIGHT && j > -1 && j < WIDTH ) { if ( otherWorld[i][j] == 'X' ) { neighbors++; } } } } } if (state == 'X') { return ( neighbors > 1 && neighbors < 4 ) ? 'X' : '.'; } else { return ( neighbors == 3 ) ? 'X' : '.'; } } void GameOfLife::iterate( unsigned int iterations ) { for ( int i = 0; i < iterations; i++ ) { print(); update(); } } //precondition: none //post-condition: glider is created Glider::Glider( char x , char y ) { xCoord = x; yCoord = y; height = GLIDER_SIZE; width = GLIDER_SIZE; figure = new char*[GLIDER_SIZE]; for ( char i = 0; i < GLIDER_SIZE; i++ ) { figure[i] = new char[GLIDER_SIZE]; } for ( char i = 0; i < GLIDER_SIZE; i++ ) { for ( char j = 0; j < GLIDER_SIZE; j++ ) { figure[i][j] = '.'; } } figure[0][1] = 'X'; figure[1][2] = 'X'; figure[2][0] = 'X'; figure[2][1] = 'X'; figure[2][2] = 'X'; } //precondition: glider must exist //post-condition: glider is cleared Glider::~Glider() { for ( char i = 0; i < GLIDER_SIZE; i++ ) { delete[] figure[i]; } delete[] figure; } //precondition: none //post-condition: tenline created tenline::tenline( char x , char y ) { xCoord = x; yCoord = y; height = tenline_HEIGHT; width = tenline_WIDTH; figure = new char*[tenline_HEIGHT]; for ( char i = 0; i < tenline_HEIGHT; i++ ) { figure[i] = new char[tenline_WIDTH]; } for ( char i = 0; i < tenline_HEIGHT; i++ ) { for ( char j = 0; j < tenline_WIDTH; j++ ) { figure[i][j] = 'X'; } } } //precondition: tenline must exist //post-condition: tenline is cleared tenline::~tenline() { for ( char i = 0; i < tenline_HEIGHT; i++ ) { delete[] figure[i]; } delete[] figure; } int main() { int iteratenum; //number of iterations int gliderXusernum; //X coordinate of glider shape int gliderYusernum; //Y coordinate of glider shape cout << "With what position on the board shall we begin?" << endl; cout << "Enter the X coordinate:" << endl; cin >> gliderXusernum; cout << "Enter the Y coordinate:" << endl; cin >> gliderYusernum; cout << "How many times would you like to run Conway's Game of Life?" << endl; cin >> iteratenum; cout << "Game will play " << iteratenum << " times... " << endl; Glider glider(gliderXusernum,gliderYusernum); GameOfLife gol(glider); gol.iterate(iteratenum); tenline tenline(2,4); system("PAUSE"); return 1; }