USING CODE BLOCKS C++ ONLY: Please write the code below: The game of Life, inven
ID: 3689937 • Letter: U
Question
USING CODE BLOCKS C++ ONLY:
Please write the code below:
The game of Life, invented by the mathematician John H. Conway, is intended to model life in a society of organisms. Consider a rectangular array of cells, each of which may contain an organism. If the array is assumed to extend indefinitely in both directions, each cell will have eight neighbors, the eight cells surrounding it.
Birth and deaths occur according to the following rules.
An organism is born in an empty cell that has exactly three neighbors.
An organism dies from isolation if it has fewer than two neighbors.
An organism dies from overcrowding if it has more than three neighbors.
The following display shows the first five generations of a particular configuration of organisms
O
O OOO O OOO
OOO OOO O O O O
O OOO O O O OOO
O O .
Write a program that will start with the first generation above. Your program must generate the next four generations. This can be done with two functions.
Submit your source code in Blackboard.
Read the first generation from a file named LIFE.txt.
Your source code must be well documented.
Explanation / Answer
#include #include #include #include typedef unsigned char byte; class world { public: world( int x, int y ) : _wid( x ), _hei( y ) { int s = _wid * _hei * sizeof( byte ); _cells = new byte[s]; memset( _cells, 0, s ); } ~world() { delete [] _cells; } int wid() const { return _wid; } int hei() const { return _hei; } byte at( int x, int y ) const { return _cells[x + y * _wid]; } void set( int x, int y, byte c ) { _cells[x + y * _wid] = c; } void swap( world* w ) { memcpy( _cells, w->_cells, _wid * _hei * sizeof( byte ) ); } private: int _wid, _hei; byte* _cells; }; class rule { public: rule( world* w ) : wrd( w ) { wid = wrd->wid(); hei = wrd->hei(); wrdT = new world( wid, hei ); } ~rule() { if( wrdT ) delete wrdT; } bool hasLivingCells() { for( int y = 0; y set( x, y, inBirth( n ) ? 1 : 0 ); } } } } private: int neighbours( int xx, int yy ) { int n = 0, nx, ny; for( int y = -1; y < 2; y++ ) { for( int x = -1; x < 2; x++ ) { if( !x && !y ) continue; nx = ( wid + xx + x ) % wid; ny = ( hei + yy + y ) % hei; n += wrd->at( nx, ny ) > 0 ? 1 : 0; } } return n; } bool inStay( int n ) { return( _stay.end() != find( _stay.begin(), _stay.end(), n ) ); } bool inBirth( int n ) { return( _birth.end() != find( _birth.begin(), _birth.end(), n ) ); } int wid, hei; world *wrd, *wrdT; std::vector _stay, _birth; }; class cellular { public: cellular( int w, int h ) : rl( 0 ) { wrd = new world( w, h ); } ~cellular() { if( rl ) delete rl; delete wrd; } void start( int r ) { rl = new rule( wrd ); gen = 1; std::vector t; switch( r ) { case 1: // conway t.push_back( 2 ); t.push_back( 3 ); rl->setRuleS( t ); t.clear(); t.push_back( 3 ); rl->setRuleB( t ); break; case 2: // amoeba t.push_back( 1 ); t.push_back( 3 ); t.push_back( 5 ); t.push_back( 8 ); rl->setRuleS( t ); t.clear(); t.push_back( 3 ); t.push_back( 5 ); t.push_back( 7 ); rl->setRuleB( t ); break; case 3: // life34 t.push_back( 3 ); t.push_back( 4 ); rl->setRuleS( t ); rl->setRuleB( t ); break; case 4: // maze t.push_back( 1 ); t.push_back( 2 ); t.push_back( 3 ); t.push_back( 4 ); t.push_back( 5 ); rl->setRuleS( t ); t.clear(); t.push_back( 3 ); rl->setRuleB( t ); break; } /* just for test - shoud read from a file */ /* GLIDER */ wrd->set( 6, 1, 1 ); wrd->set( 7, 2, 1 ); wrd->set( 5, 3, 1 ); wrd->set( 6, 3, 1 ); wrd->set( 7, 3, 1 ); /* BLINKER */ wrd->set( 1, 3, 1 ); wrd->set( 2, 3, 1 ); wrd->set( 3, 3, 1 ); /******************************************/ generation(); } private: void display() { system( "cls" ); int wid = wrd->wid(), hei = wrd->hei(); std::coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.