1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
const char LIFE = 'L';
const char BLANK = '.';
const int N = 20;
const int M = 20;
const int nLife = M>N ? M/2 : N/2;
char world[N*M]; // will simulate 2-dimenisonal array.
// OUR FUNCTIONS (ie, TOOLS) that work on our DATA
void fillWorldwith(char w[], const int N, const int M, const char stuff){
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
w[i*M+j*sizeof(char)]=stuff;
}
void fillWorldwithLife(char w[], const int N, const int M, const int Life){
for(int k=0; k<Life; k++)
{
int i = rand()%N;
int j = rand()%M;
// cout << "i="<< i<< ", j="<< j ;
// cout << " loc= "<<i+j*sizeof(char) << endl;
w[i*M+j*sizeof(char)]=LIFE;
}
}
void displayWorld(char *w, const int N, const int M){
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
cout << setw(2) << w[i*M+j*sizeof(char)];
cout << endl;
}
}
// Used in applyRules as a buffer to update world.
void copyWorld(char dest[], char src[], const int N, const int M){
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
dest[i*M+j*sizeof(char)]= src[i*M+j*sizeof(char)];
}
// Rules
void applyRules(char w[], const int N, const int M)
{
//char *t = (char *) malloc( sizeof(char)*N*M);
char *t = new char[N*M]; // create a temporary world
int connect;
copyWorld(t, w, N, M); // copy world w to temporary world t
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
{
connect = 0;
if (w[(i-1)*M+j*sizeof(char)] == LIFE)
connect +=1; // rules look at w, to change t
if (w[(i-1)*M+(j-1)*sizeof(char)] == LIFE)
connect +=1;
if (w[(i-1)*M+(j+1)*sizeof(char)] == LIFE)
connect +=1;
if (w[(i+1)*M+(j-1)*sizeof(char)] == LIFE)
connect +=1;
if (w[(i+1)*M+(j+1)*sizeof(char)] == LIFE)
connect +=1;
if (w[(i+1)*M+j*sizeof(char)] == LIFE)
connect +=1;
if (w[i*M+(j-1)*sizeof(char)] == LIFE)
connect +=1;
if (w[i*M+(j+1)*sizeof(char)] == LIFE)
connect +=1;
//cout << connect;
if (w[i*M+j*sizeof(char)] == LIFE && connect > 3)
t[i*M+j*sizeof(char)] = BLANK;
else if (w[i*M+j*sizeof(char)] == LIFE && connect < 2)
t[i*M+j*sizeof(char)] = BLANK;
else if (w[i*M+j*sizeof(char)] == LIFE && (connect == 2 || connect == 3))
t[i*M+j*sizeof(char)] = LIFE;
else if (w[i*M+j*sizeof(char)] == BLANK && connect == 3)
t[i*M+j*sizeof(char)] = LIFE;
}
copyWorld(w, t, N, M); // copy temp world to w world: w <- temp
//system("pause");
}
int main()
{
SMALL_RECT windowSize= {0, 0, 200, 200}; // windows
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // windows
// windowSize ;
SetConsoleWindowInfo(hConsole, TRUE, &windowSize); // windows
SetConsoleTitle(TEXT("Game of Life")); // windows
system("cls"); // clear the console
stringstream ss; // trick to combine string and numbers
ss << "color " << Black << Yellow;
// objects are fun to use.
system( ss.str().c_str() ); // change console colour
fillWorldwith(world, N, M, BLANK); // fill whole world with BLANKS
displayWorld(world, N, M);
// cout << endl << "now add stuff" << endl;
for(int time=0; time<100; time++){ // each loop, considered time.
system("cls");
fillWorldwithLife(world, N, M, nLife); // new random life
applyRules(world,N,M);
displayWorld(world, N, M); // display world
Sleep(500); // delay so we can see world
fillWorldwith(world, N, M, BLANK); // fill whole world with BLANKS
}
return 0;
}
|