Created
January 26, 2009 13:43
-
-
Save alfanick/52816 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <windows.h> | |
#define W_SIZE 50 | |
#define H_SIZE 40 | |
#define TIME 10 | |
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; | |
} | |
void show () | |
{ | |
system("cls"); | |
for (int i =0; i<W_SIZE; i++) | |
{ | |
for (int j = 0; j < H_SIZE; j++) | |
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 and zywe > 0) | |
{ | |
make(); | |
Sleep(TIME); | |
show(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment