Skip to content

Instantly share code, notes, and snippets.

@alfanick
Created January 26, 2009 14:01
Show Gist options
  • Save alfanick/52821 to your computer and use it in GitHub Desktop.
Save alfanick/52821 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <windows.h>
#define W_SIZE 24
#define H_SIZE 79
#define TIME 300
using namespace std;
enum stan { ZYWY, MARTWY };
stan kolonia[W_SIZE][H_SIZE];
stan kolonia2[W_SIZE][H_SIZE];
void init()
{
srand(time(0));
for (int i = 0; i < W_SIZE; i++)
for (int j = 0; j < H_SIZE; j++)
kolonia[i][j] = rand()%2 == 1 ? ZYWY : MARTWY;
/* for (int i = 0; i < W_SIZE; i++)
for (int j = 0; j < H_SIZE; j++)
kolonia[i][j] = MARTWY;
kolonia[30][30] = kolonia[31][31] = kolonia[29][32] = kolonia[30][32] = kolonia[31][32] = ZYWY;
*/}
void show ()
{
system("cls");
for (int i =0; i<W_SIZE; i++)
{
for (int j = 0; j < H_SIZE; j++)
{
if (i == 0 or i == W_SIZE-1 or j == 0 or j == H_SIZE-1)
cout << char(219);
else
cout << (kolonia[i][j] == ZYWY ? char(2) : ' ');
}
cout << endl;
}
}
int neighbours(int x, int y)
{
int neighbour = 0;
if (x > 0 && x < W_SIZE &&
y > 0 && y < H_SIZE)
{
// Lewo
if (kolonia[x-1][y] == ZYWY)
neighbour++;
// Lewo gora
if (kolonia[x-1][y-1] == ZYWY)
neighbour++;
// Gora
if (kolonia[x][y-1] == ZYWY)
neighbour++;
// Prawo gora
if (kolonia[x+1][y-1] == ZYWY)
neighbour++;
// Prawo
if (kolonia[x+1][y] == ZYWY)
neighbour++;
// Prawo dol
if (kolonia[x+1][y+1] == ZYWY)
neighbour++;
// Dol
if (kolonia[x][y+1] == ZYWY)
neighbour++;
// Lewo dol
if (kolonia[x-1][y+1] == ZYWY)
neighbour++;
}
return neighbour;
}
int zywe = W_SIZE * H_SIZE;
void copy()
{
zywe = 0;
for(int i = 0; i < W_SIZE; i++)
{
for(int j = 0; j < H_SIZE; j++)
{
kolonia[i][j] = kolonia2[i][j];
if (kolonia[i][j] == ZYWY)
zywe++;
}
}
}
void make()
{
for(int x = 0; x < W_SIZE; x++)
{
for(int y = 0; y < H_SIZE; y++)
{
switch(neighbours(x, y))
{
case 2: kolonia2[x][y] = kolonia[x][y]; break;
case 3: kolonia2[x][y] = ZYWY; break;
default: kolonia2[x][y] = MARTWY;
}
}
}
copy();
}
int main()
{
init();
while (true)
{
if (zywe == 0)
break;
make();
Sleep(TIME);
show();
}
cout << "Wszyscy zmarli!!!" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment