Skip to content

Commit

Permalink
Merge pull request #1 from elbachir-one/main
Browse files Browse the repository at this point in the history
Displaying Unicode characters.
  • Loading branch information
cdkw2 authored Aug 23, 2024
2 parents ce362de + ca9108d commit 1aec9af
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CC = gcc
CFLAGS = -Wall -Wextra -pedantic -std=c99
LDFLAGS = -lncurses -lm

SRC = conway.c
OBJ = $(SRC:.c=.o)
EXEC = conway

all: $(EXEC)

$(EXEC): $(OBJ)
$(CC) $(OBJ) -o $(EXEC) $(LDFLAGS)

%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

clean:
rm -f $(OBJ) $(EXEC)

.PHONY: all clean
31 changes: 19 additions & 12 deletions conway.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
#include <stdio.h>
#include <libgen.h>
#include <linux/limits.h>
#include <locale.h>
#include <wchar.h>

#define CONFIG_FILE "game_of_life.conf"
#define MAX_COLORS 8
#define CELL_CHAR_MAX 8

int WIDTH, HEIGHT;
time_t last_glider_time;
Expand All @@ -21,7 +24,7 @@ typedef struct {
typedef struct {
int infinite_mode;
int update_interval;
char cell_char[8];
wchar_t cell_char[CELL_CHAR_MAX];
int max_age;
int color_mode;
int glider_interval;
Expand Down Expand Up @@ -51,7 +54,7 @@ void load_config() {
if (file == NULL) {
config.infinite_mode = 0;
config.update_interval = 100000;
strcpy(config.cell_char, "&");
wcsncpy(config.cell_char, L"■", CELL_CHAR_MAX - 1);
config.max_age = 5;
config.color_mode = 1;
config.glider_interval = 3;
Expand All @@ -67,7 +70,10 @@ void load_config() {
if (key && value) {
if (strcmp(key, "infinite_mode") == 0) config.infinite_mode = atoi(value);
else if (strcmp(key, "update_interval") == 0) config.update_interval = atoi(value);
else if (strcmp(key, "cell_char") == 0) strncpy(config.cell_char, value, 7);
else if (strcmp(key, "cell_char") == 0) {
mbstowcs(config.cell_char, value, CELL_CHAR_MAX - 1);
config.cell_char[CELL_CHAR_MAX - 1] = L'\0';
}
else if (strcmp(key, "max_age") == 0) config.max_age = atoi(value);
else if (strcmp(key, "color_mode") == 0) config.color_mode = atoi(value);
else if (strcmp(key, "glider_interval") == 0) config.glider_interval = atoi(value);
Expand Down Expand Up @@ -95,10 +101,10 @@ void print_grid(Cell **grid) {
if (config.color_mode) {
int color = (grid[y][x].age % config.max_age) + 1;
attron(COLOR_PAIR(color));
mvaddstr(y, x, config.cell_char);
mvaddwstr(y, x, config.cell_char);
attroff(COLOR_PAIR(color));
} else {
mvaddstr(y, x, config.cell_char);
mvaddwstr(y, x, config.cell_char);
}
} else {
mvaddch(y, x, ' ');
Expand Down Expand Up @@ -132,7 +138,7 @@ void spawn_glider(Cell **grid, int x, int y) {
{0, 0, 1},
{1, 1, 1}
};

for (int dy = 0; dy < 3; dy++) {
for (int dx = 0; dx < 3; dx++) {
int nx = (x + dx) % WIDTH;
Expand All @@ -156,7 +162,7 @@ void update_grid(Cell **grid, Cell **new_grid) {
}
}
}

if (config.infinite_mode && difftime(time(NULL), last_glider_time) >= config.glider_interval) {
int rx = rand() % WIDTH;
int ry = rand() % HEIGHT;
Expand All @@ -166,8 +172,9 @@ void update_grid(Cell **grid, Cell **new_grid) {
}

int main() {
setlocale(LC_CTYPE, ""); // Set locale for wide character support
load_config();

srand(time(NULL));
initscr();
cbreak();
Expand All @@ -183,17 +190,17 @@ int main() {
}

getmaxyx(stdscr, HEIGHT, WIDTH);

Cell **grid = malloc(HEIGHT * sizeof(Cell *));
Cell **new_grid = malloc(HEIGHT * sizeof(Cell *));
for (int i = 0; i < HEIGHT; i++) {
grid[i] = malloc(WIDTH * sizeof(Cell));
new_grid[i] = malloc(WIDTH * sizeof(Cell));
}

init_grid(grid);
last_glider_time = time(NULL);

int generation = 0;
while (1) {
print_grid(grid);
Expand All @@ -216,7 +223,7 @@ int main() {
}
usleep(config.update_interval);
}

for (int i = 0; i < HEIGHT; i++) {
free(grid[i]);
free(new_grid[i]);
Expand Down

0 comments on commit 1aec9af

Please sign in to comment.