#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cctype>
#include <cstring>
#include <initializer_list>
const std::size_t MAX_ROW = 30 ;
const std::size_t MAX_COL = 60 ;
char pq;
using currentArray = char[MAX_ROW][MAX_COL] ;
int ng;
constexpr int NROWS = 30 ;
constexpr int NCOLS = 60 ;
using array_type = char[NROWS][NCOLS] ;
enum liveness : char { DEAD = '0', ALIVE = '1', BAD_VALUE = 'B' };
void copyArray( const currentArray& array ) // array passed by reference
{
for( const auto& row : array ) // for each row in the array
{
for( char c : row ) std::cout << c ; // print each character i the row
std::cout << '\n' ;
}
std::cout << '\n' ;
}
void setNextGenArray ()
{
}
void setZeroArray( currentArray& array, char fill_char )
{
for( auto& row : array ) // each row in array
for( char& c : row ) // each character in row
c = fill_char ;
}
// set U at random location
void setInitialPatternArray( currentArray& array, char pattern_char )
{
const std::size_t MIN_SZ = 5 ;
// width and height of the 'U'
const std::size_t height = 5 + MIN_SZ ;
const std::size_t width = 7 + MIN_SZ ;
// random location for top left corner of U
std::size_t x = std::rand() % (MAX_ROW/2-MIN_SZ) ;
std::size_t y = std::rand() % (MAX_COL/2-MIN_SZ) ;
// place two vertical lines
for( std::size_t i = 0 ; i < (height-1) ; ++i )
array[x+i][y] = array[x+i][y+width] = pattern_char ;
// bottom line of U
for( std::size_t j = 0 ; j <= width ; ++j )
array[x+height-1][y+j] = pattern_char ;
}
void init( currentArray& array )
{
setZeroArray( array, '0' ) ; // fill with '0'
setInitialPatternArray( array, '1' ) ; // set the pattern using '1'
}
char displayMenu()
{
std::cout << "[P]lay - Press 'P' to play.\n"
<< "[Q]uit - Press 'Q' to exit.\n";
char pq ;
std::cin >> pq;
if( pq == 'p' || pq == 'q' ) return pq ;
std::cout << "invalid input. try again\n" ;
return displayMenu() ;
}
int main()
{
while (displayMenu() == 'p')
{
if (ng == 3) ng = 0;
while ( ng<3 )
{
std::srand( std::time(nullptr) ) ;
currentArray array ;
init(array) ;
copyArray(array) ;
void copy( const array_type& srce, array_type& dest ) // copy array_type from srce to dest
{
if( srce != dest ) std::memcpy( dest, srce, sizeof(array_type) ) ;
}
bool equal( const array_type& a, const array_type& b ) // true if contents of a and b are the same
{
return std::memcmp( a, b, sizeof(array_type) ) == 0 ;
}
// return the value of the character at array[row][col]
// return BAD_VALUE if either row or col is out of range
char get_value( const array_type& array, int row, int col )
{
if( row < 0 || row >= NROWS || col < 0 || col >= NCOLS ) return BAD_VALUE ;
else return array[row][col] ;
}
// return true if the cell array[row][col] is alive
bool is_alive( const array_type& array, int row, int col )
{ return get_value( array, row, col ) == ALIVE ; }
// return the count of live neighbours of array[row,col]
int count_live_neighbours( const array_type& array, int row, int col )
{
int count = 0 ;
// get the count for all live cells in the 3x3 segment with array[row][col] at the centre
//
http://en.cppreference.com/w/cpp/utility/initializer_list
for( int delta_row : { -1, 0, +1 } ) for( int delta_col : { -1, 0, +1 } )
if( is_alive( array, row+delta_row, col+delta_col ) ) ++count ;
// the count now includes the liveness of array[row][col]; so adjust it
if( is_alive( array, row, col ) ) --count ;
return count ;
}
// move array to the next generation
// return true if steady state has not been reached, false otherwise
bool generate( array_type& array )
{
array_type next_generation ; // note that there is no need to make this a global variable
// for each cell in the array
for( int row = 0 ; row < NROWS ; ++row ) for( int col = 0 ; col < NCOLS ; ++col )
{
const auto n = count_live_neighbours( array, row, col ) ;
// Rules for "1" (living cells)
// if (2-3 live "1" neighbours = lives to next generation (1 stays 1))
// else (death (1 become 0))
if( is_alive( array, row, col ) )
next_generation[row][col] = ( n==2 || n==3 ) ? ALIVE : DEAD ;
// Rules for "0" (dead cells)
// if (3 live "1" neighbours = cell is born (0 becomes 1))
// else (no life (0 stays 0))
else next_generation[row][col] = n == 3 ? ALIVE : DEAD ;
}
if( equal( array, next_generation ) ) return false ; // reached steady state (no change)
copy( next_generation, array ) ; // update to the next generation
return true ;
}
ng++;
}
}
I tried integrating it into the mian function and incuded the int variables and added the libraries up top, I pretty much put it right in the main function since I wasn't sure how the new arrays would interact with the old ones but I'm getting an erro code on code:blocks on the copyArray function, It didnt give me that problem before I added the new code, is there some kind of interference with the functions that I'm not seeing?