gol

Implementation of Conway's Game of Life writen in C
git clone git://git.dimitrijedobrota.com/gol.git
Log | Files | Refs | README

commit d46d4807e8318027f45798e84b770d627db7aa88
parent 53d6b402cb20183b6506084fcc0feacf27ce9518
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sun, 12 Jun 2022 15:06:21 +0200

General improvement and bug fixing

- Makefile supports docs target to make documentation using Doxygen
- Fix escape key exiting from everything with one press
- Fix coordinates for pattern while loading in a wrapping game
- Fix bug in evolvePredator() and evolveVirus()

Diffstat:
MMakefile | 8+++++++-
Minclude/utils.h | 6++++--
Msrc/display.c | 6+++++-
Msrc/file.c | 5++++-
Msrc/game.c | 6+++---
Msrc/logic.c | 6++++--
6 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile @@ -10,6 +10,7 @@ CFLAGS = -I include SRC = src OBJ = obj BINDIR = bin +LATEX = docs/latex BIN = bin/$(NAME) SRCS=$(wildcard $(SRC)/*.c) @@ -45,6 +46,10 @@ $(OBJ)/%.o: $(SRC)/%.c clean: -$(RM) $(DEL_CLEAN) +docs: + doxygen + make -C $(LATEX) + help: @echo "Game of Life Simulation" @echo @@ -54,10 +59,11 @@ help: @echo " all - Compiles binary file [Default]" @echo " clean - Clean the project by removing binaries" @echo " help - Prints a help message with target rules" + @echo " docs - Compile html and pdf documentation using doxygen and pdflatex" @echo @echo "Optional parameters:" @echo " DEBUG - Compile binary file with debug flags enabled" @echo " NO_UNICODE - Compile binary file that does not use Unicode characters" @echo -.PHONY: all clean help +.PHONY: all clean help docs diff --git a/include/utils.h b/include/utils.h @@ -2,12 +2,14 @@ #define UTILS_H #include <curses.h> + #include "display.h" #define MAX(a, b) ((a > b) ? a : b) #define MIN(a, b) ((a < b) ? a : b) #define CLAMP(a, x, y) ((a) = (MAX(x, MIN(a, y)))) #define ACLAMP(a, x, y) (MAX(x, MIN(a, y))) +#define WCLAMP(a, x) ((a + x) % x) #ifdef _WIN32 #define is_term_resized(a, b) is_termresized() @@ -34,11 +36,11 @@ #define MEM_CHECK(x) \ if ((x) == NULL) { \ display_stop(); \ - printf("MEM ERROR"); \ + printf("MEM ERROR"); \ abort(); \ } -#define FILE_CHECK(x) \ +#define FILE_CHECK(x) \ if ((x) == NULL) { \ display_stop(); \ printf("FILE ERROR"); \ diff --git a/src/display.c b/src/display.c @@ -52,6 +52,7 @@ int input(WINDOW *win, char *buffer, int size, input_f crit) { while ((ch = getch()) != '\n') { switch (ch) { case 27: + flushinp(); buffer[read] = '\0'; return 100; case KEY_BACKSPACE: @@ -211,8 +212,10 @@ redraw:; wattrset(win, COLOR_PAIR(0)); items[current].callback(items[current].name, current); return; - } else if (c == 27) + } else if (c == 27) { + flushinp(); return; + } if (is_term_resized(CLINES, CCOLS)) { HANDLE_RESIZE; goto redraw; @@ -366,6 +369,7 @@ redraw:; case 'q': case 'Q': case '\n': + flushinp(); goto end; } if (is_term_resized(CLINES, CCOLS)) { diff --git a/src/file.c b/src/file.c @@ -185,7 +185,10 @@ void file_load_pattern(char *name, int index) { rewind(f); while (fscanf(f, "%d %d %d", &row, &col, &val) != EOF) - setAt(pos_y + row, pos_x + col, val); + if (height != 0 && width != 0) + setAt(WCLAMP(pos_y + row, height), WCLAMP(pos_x + col, width), val); + else + setAt(pos_y + row, pos_x + col, val); } void file_save_pattern(char *name, int index) { diff --git a/src/game.c b/src/game.c @@ -203,6 +203,7 @@ int display_select(window_T wind) { case 27: case 'q': case 'Q': + flushinp(); goto end; } flushinp(); @@ -317,9 +318,8 @@ redraw:; time_const, cord(y_at(cursor_offset_y)), cord(x_at(cursor_offset_x))); - if (play || screen_change) { + if (screen_change) { display_game(game_w); - wrefresh(game_W); screen_change = 0; cursor_change = 1; } @@ -331,7 +331,6 @@ redraw:; } while ((total_t = (long int)(end_t - start_t)) < time_const * TIME_MOD) { - refresh(); int c = getch(); switch (c) { @@ -345,6 +344,7 @@ redraw:; case 27: case 'q': case 'Q': + flushinp(); goto end; // change num of evolutions before display diff --git a/src/logic.c b/src/logic.c @@ -86,8 +86,8 @@ void addToCellsWrap(int i, int j, int value) { for (int k = i - 1; k <= i + 1; k++) for (int l = j - 1; l <= j + 1; l++) { - int a = (k + height) % height; - int b = (l + width) % width; + int a = WCLAMP(k, height); + int b = WCLAMP(l, width); if (a != i || b != j) insert(a, b, 0, mod); } @@ -160,6 +160,7 @@ void evolvePredator(void) { s1 = (c->val & 31) >> 2; mod = c->val & 3; if ((s1 + s2) < 2 || (s1 + s2) > 3) { + deleter(c); continue; } switch (mod) { @@ -195,6 +196,7 @@ void evolveVirus(void) { mod = c->val & 3; if ((s1 + s2) < 2 || (s1 + s2) > 3) { deleter(c); + continue; } switch (mod) { case 0: