The game of life, the classic problem and unbeliverable.
What is the game of life ????
The game of life ( or simply life ) is not a game in convenional sense. There are no players , and no winning or losing. One the “piece” are placed in the starting position, the rules determine everything that happens later. Nevertheless , life is full of surprise! In most cases, it is impossible to look at starting position ( or pattern ) and see what will happen in the future. The only way to find out is to follow the rules of the game…..
There is a good reference site for the rule of this game go to this link : http://www.math.com/students/wonders/life/life.html or if you want to see some online example go : http://www.bitstorm.org/gameoflife/ and this is my code for the game of life in C++ version ( and very simple one )
#include
using namespace std;
int main(int argc, char *argv[])
{
const int row = 34 ;
const int col = 34 ;
// this table for displaying the result
int myTable[row+1][col+1] = {{0}};
// this table for caculating …
int cTable[row+1][col+1]=
{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,1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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} } ;
int count = 1; ;
int Pcount ;
while(count < 1000)
{//----------------------Start of while -----------------------------------
cout << "Round number : " << count << endl ;
for ( int i = 0; i < row ; i++)
{
for (int j = 0; j < col; j++)
{
Pcount = 0;
//------------------------------------------------------------------
// Determine amount of neihbore cell....
// -----------------------------------------------------------------
if (( i == 0 )&& ( j == 0 )) // Upper left corner
{
if (cTable[i+1][j] == 1)
Pcount++;
if (cTable[i+1][j+1] == 1)
Pcount++;
if (cTable[i][j+1] == 1)
Pcount++;
}
else if (( i == 0 ) && ( j > 0 )) // Upper border …..
{
if (cTable[i][j-1] == 1 )
Pcount++;
if (cTable[i+1][j-1] == 1)
Pcount++;
if (cTable[i+1][j] == 1)
Pcount++;
if (cTable[i+1][j+1] == 1)
Pcount++ ;
if (cTable[i][j+1] == 1)
Pcount++;
}
else if ( ( i == 0 ) && ( j == col )) // Upper right cornet
{
if (cTable[i+1][j] == 1)
Pcount++;
if (cTable[i+1][j-1] == 1)
Pcount++;
if (cTable[i][j-1] == 1)
Pcount++;
}
else if ( ( j == 0 ) && ( i > 0 ) ) // left border …………
{
if (cTable[i-1][j] == 1)
Pcount++;
if (cTable[i-1][j+1] == 1)
Pcount++;
if (cTable[i][j+1] == 1)
Pcount++;
if (cTable[i+1][j+1] ==1)
Pcount++;
if (cTable[i+1][j] == 1)
Pcount++;
}
else if ( ( j == 0 ) && ( i == row )) // lower left
{
if(cTable[i-1][j] == 1 )
Pcount++;
if(cTable[i-1][j+1] == 1)
Pcount++;
if(cTable[i][j+1] == 1)
Pcount++;
}
else if ( ( i == row ) && ( j > 0 ) ) // lower border …….
{
if(cTable[i][j-1] == 1)
Pcount++;
if(cTable[i-1][j-1] == 1)
Pcount++;
if(cTable[i-1][j] == 1)
Pcount++;
if(cTable[i-1][j+1] == 1)
Pcount++;
if(cTable[i][j+1] == 1)
Pcount++;
}
else if ( ( i == row ) && ( j == col )) // lower right
{
if(cTable[i][j-1] == 1 )
Pcount++;
if(cTable[i-1][j-1] == 1)
Pcount++;
if(cTable[i-1][j] == 1)
Pcount++;
}
else if ( ( i > 0 ) && ( j == col )) // right border …..
{
if(cTable[i+1][j] == 1)
Pcount++;
if(cTable[i+1][j-1] == 1)
Pcount++;
if(cTable[i][j-1] == 1)
Pcount++;
if(cTable[i-1][j-1] == 1)
Pcount++;
if(cTable[i-1][j] ==1)
Pcount++;
}
else if ( (( i > 0 ) && ( i < row )) && ( ( j > 0 )&&( j < col )) ) // inside the box ......
{
if(cTable[i-1][j-1]==1)
Pcount++;
if(cTable[i-1][j] == 1)
Pcount++;
if(cTable[i-1][j+1] == 1)
Pcount++;
if(cTable[i][j-1] == 1)
Pcount++;
if(cTable[i][j+1] == 1)
Pcount++;
if(cTable[i+1][j-1] == 1)
Pcount++;
if(cTable[i+1][j] == 1)
Pcount++;
if(cTable[i+1][j+1] == 1)
Pcount++;
}
else
{
Pcount = 0;
}
// -----------------------------------------------------------------
// Determine and plot to myTable...
// This part apply to all i,j
// -----------------------------------------------------------------
if ( cTable[i][j] == 0 ) // dead cell
{
if ( Pcount == 3 )
{
myTable[i][j] = 1;
}
}
else // life cell
{
if ( ( Pcount == 2 ) || ( Pcount == 3) )
{
myTable[i][j] = 1;
}
else
{
myTable[i][j] = 0 ;
}
}
}
}
// -------------------------------------------------------------------------
// -------- Copy all from myTable to cTable , for the next calculation -----
// -------------------------------------------------------------------------
for (int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
cTable[i][j]= myTable[i][j];
}
}
// --------------------------------------------------------------------------
// ------- Display the result -----------------------------------------------
// --------------------------------------------------------------------------
for ( int i = 0; i < row; i++ )
{
for(int j= 0; j < col; j++)
{
//cout << myTable[i][j] << " " ;
if (myTable[i][j] == 1 )
{
cout << "*" << "" ;
}
else
{
cout << "-" << "";
}
}
//cout << endl;
cout << endl;
}
//--------------------------------------------------------------------------
// Clear The display Table....
for ( int i = 0; i < row; i++ )
{
for(int j= 0; j < col; j++)
{
myTable[i][j] = 0;
}
}
// delay form some amount of time..
Time_Delay();
// clear the console...
system("cls");
// Add one to count ...
count++;
}// end of while
system("PAUSE");
return EXIT_SUCCESS;
}
//----------------------------------------------------------------------------------------------
// ---- Function Library -----------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
void Time_Delay(void)
{
//-----------------------------------------------------------------------------------------
// delay for some amount of times... don't know how many , but just some amount of time..
//-----------------------------------------------------------------------------------------
for ( int i = 0; i < 10000 ; i++)
{
for ( int j = 0; j < 20000 ; j++)
{
;
}
}
}
[/sourcecode]
Good !