2020-06-12 09:46:44 +08:00
|
|
|
#include <stdlib.h>
|
2020-06-15 08:12:14 +08:00
|
|
|
#include <time.h>
|
2020-06-12 09:46:44 +08:00
|
|
|
#include "grid.h"
|
|
|
|
|
2020-06-14 09:55:02 +08:00
|
|
|
void initGrid(Grid* grid, unsigned int width, unsigned int height)
|
|
|
|
{
|
|
|
|
grid->size = width*height;
|
|
|
|
grid->width = width;
|
2020-06-15 08:12:14 +08:00
|
|
|
grid->state = (bool*)malloc(sizeof(bool)*grid->size);
|
|
|
|
grid->next_state = (bool*)malloc(sizeof(bool)*grid->size);
|
2020-06-14 09:55:02 +08:00
|
|
|
}
|
|
|
|
|
2020-06-15 08:12:14 +08:00
|
|
|
void randomizeGrid(Grid* grid)
|
|
|
|
{
|
|
|
|
srand(time(NULL));
|
|
|
|
for (int i = 0; i < grid->size; i++) {
|
|
|
|
grid->state[i] = rand()%2;
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 09:46:44 +08:00
|
|
|
// maps x, y coordinate to array index of grid
|
|
|
|
unsigned int toIndex(Grid* grid, int x, int y)
|
|
|
|
{
|
|
|
|
return (grid->width*y + x)%grid->size;
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:12:14 +08:00
|
|
|
bool getPixel(Grid* grid, int x, int y)
|
|
|
|
{
|
|
|
|
return grid->state[toIndex(grid, x, y)];
|
|
|
|
}
|
|
|
|
|
2020-06-12 09:46:44 +08:00
|
|
|
void clearGrid(Grid* grid)
|
|
|
|
{
|
2020-06-15 08:12:14 +08:00
|
|
|
for (int i = 0; i < grid->size; i++) grid->state[i]=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if cell's next state is alive
|
|
|
|
static bool isAliveNext(Grid* grid, int x, int y)
|
|
|
|
{
|
|
|
|
bool nw = getPixel(grid, x-1, y-1);
|
|
|
|
bool n = getPixel(grid, x, y-1);
|
|
|
|
bool ne = getPixel(grid, x+1, y-1);
|
|
|
|
bool w = getPixel(grid, x-1, y);
|
|
|
|
bool e = getPixel(grid, x+1, y);
|
|
|
|
bool sw = getPixel(grid, x-1, y+1);
|
|
|
|
bool s = getPixel(grid, x, y+1);
|
|
|
|
bool se = getPixel(grid, x+1, y+1);
|
|
|
|
|
|
|
|
bool currentlyAlive = grid->state[toIndex(grid, x, y)];
|
|
|
|
|
|
|
|
const int no_neighbors = 8;
|
|
|
|
bool neighbors[] = {
|
|
|
|
nw, n, ne,
|
|
|
|
w, e,
|
|
|
|
sw, s, se
|
|
|
|
};
|
|
|
|
|
|
|
|
int alive_neighbors = 0;
|
|
|
|
for (int i = 0; i < no_neighbors; i++) {
|
|
|
|
alive_neighbors += neighbors[i];
|
|
|
|
}
|
|
|
|
// rules
|
|
|
|
if (currentlyAlive) {
|
|
|
|
if (alive_neighbors == 2 || alive_neighbors == 3) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (alive_neighbors == 3) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-06-12 09:46:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-14 09:55:02 +08:00
|
|
|
void updateGrid(Grid* grid)
|
2020-06-12 09:46:44 +08:00
|
|
|
{
|
2020-06-14 09:55:02 +08:00
|
|
|
unsigned int width = grid->width;
|
|
|
|
unsigned int height = grid->size/width;
|
|
|
|
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
for (int x = 0; x < width; x++) {
|
2020-06-15 08:12:14 +08:00
|
|
|
grid->next_state[toIndex(grid, x, y)] = isAliveNext(grid, x, y);
|
2020-06-14 09:55:02 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 08:12:14 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < grid->size; i++) {
|
|
|
|
grid->state[i] = grid->next_state[i];
|
|
|
|
}
|
2020-06-12 09:46:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void putPixel(Grid* grid, int x, int y)
|
|
|
|
{
|
2020-06-15 08:12:14 +08:00
|
|
|
grid->state[toIndex(grid, x, y)] = true;
|
2020-06-12 09:46:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawGrid(Grid* grid)
|
|
|
|
{
|
|
|
|
unsigned int width, height;
|
|
|
|
width = grid->width;
|
|
|
|
height = grid->size/width;
|
2020-06-19 05:26:14 +08:00
|
|
|
// Init color pair init_pair(index, fg, bg);
|
|
|
|
init_pair(1, COLOR_BLUE, COLOR_WHITE);
|
2020-06-12 09:46:44 +08:00
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
for (int x = 0; x < width; x++) {
|
2020-06-19 05:26:14 +08:00
|
|
|
if (grid->state[toIndex(grid, x, y)]){
|
|
|
|
attron(COLOR_PAIR(1));
|
|
|
|
mvaddch(y, x, ' ');
|
|
|
|
attroff(COLOR_PAIR(1));
|
|
|
|
}
|
|
|
|
else mvaddch(y, x, ' ');
|
2020-06-12 09:46:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|