Skip to content

Instantly share code, notes, and snippets.

@fliiiix
Created October 17, 2014 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fliiiix/349cd5e83aa805a11bc5 to your computer and use it in GitHub Desktop.
Save fliiiix/349cd5e83aa805a11bc5 to your computer and use it in GitHub Desktop.
Do What The Fuck You Want To Public License
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <random>
#include <unistd.h>
#define BOARD_SIZE_X 150
#define BOARD_SIZE_Y 40
#define SEED_POPULATION 0.3 // represent a % value
#define ITERATIONS 1000
#define SLEEP_TIME 100 * 1000
struct LINE
{
int data[BOARD_SIZE_X] = {};
};
struct BOARD
{
LINE line[BOARD_SIZE_Y];
};
void PrintBoard(BOARD* board);
void PrintLine(LINE* line);
void Init(BOARD* board);
BOARD* Step(BOARD* board);
int GetRandom();
int CheckLine(LINE* line, int x, bool withSelf);
int CountNeighbours(BOARD* board, int x, int y);
int main()
{
BOARD* board= new BOARD();
Init(board);
for(int i = 0; i <= ITERATIONS; i++)
{
PrintBoard(board);
board = Step(board);
usleep(SLEEP_TIME);
system("clear");
}
std::cout << std::endl;
return 0;
}
void Init(BOARD* board)
{
srand(time(NULL));
for(int y = 0; y < BOARD_SIZE_Y; y++)
{
for(int x = 0; x < BOARD_SIZE_X; x++)
{
LINE* line = &board->line[y];
line->data[x] = GetRandom();
}
}
}
BOARD* Step(BOARD* board)
{
BOARD* new_board = new BOARD();
for(int y = 0; y < BOARD_SIZE_Y; y++)
{
for(int x = 0; x < BOARD_SIZE_X; x++)
{
LINE* line = &board->line[y];
int cell = line->data[x];
int new_cell = 0;
int neighbours = CountNeighbours(board, x, y);
if(cell == 1)
{
if(neighbours == 2 || neighbours == 3)
{
new_cell = 1;
}
}
else
{
if(neighbours == 3)
{
new_cell = 1;
}
}
LINE* new_line = &new_board->line[y];
new_line->data[x] = new_cell;
}
}
return new_board;
}
void PrintBoard(BOARD* board)
{
for(int i = 0; i < BOARD_SIZE_Y; i++)
{
PrintLine(&board->line[i]);
std::cout << std::endl;
}
std::cout << std::endl;
}
void PrintLine(LINE *line)
{
for(int i = 0; i < BOARD_SIZE_X; i++)
{
//printf("row %d", i);
std::cout << line->data[i];
}
}
int CheckLine(LINE* line, int x, bool withSelf = true)
{
int neighbours = 0;
if((x+1) < BOARD_SIZE_X)
{
if(line->data[x+1] == 1)
{
neighbours++;
}
}
if(withSelf)
{
if(line->data[x] == 1)
{
neighbours++;
}
}
if((x-1) >= 0)
{
if(line->data[x-1] == 1)
{
neighbours++;
}
}
return neighbours;
}
int CountNeighbours(BOARD* board, int x, int y)
{
int neighbours = 0;
LINE* line = &board->line[y];
neighbours += CheckLine(line, x, false);
if((y-1) >= 0)
{
LINE* line_top = &board->line[y-1];
neighbours += CheckLine(line_top, x);
}
if((y+1) <= BOARD_SIZE_Y)
{
LINE* line_bottom = &board->line[y+1];
neighbours += CheckLine(line_bottom, x);
}
return neighbours;
}
int GetRandom()
{
std::random_device randomdevice;
std::mt19937 generate(randomdevice());
std::bernoulli_distribution distibution(SEED_POPULATION);
if(distibution(generate))
{
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment