From 8c9dbb2ec9d7e900efdc900b52b71458c6bdf680 Mon Sep 17 00:00:00 2001 From: BROsander <53278658+BROsandr@users.noreply.github.com> Date: Sat, 4 Sep 2021 16:57:50 +0000 Subject: [PATCH 1/6] Fixed the high score file. The file is supposed to be saved in PWD. --- src/highscore.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/highscore.c b/src/highscore.c index fbf51e3..945ee24 100644 --- a/src/highscore.c +++ b/src/highscore.c @@ -24,7 +24,8 @@ static const char* highscore_retrieve_file(void) getenv("HOME"), hs_dir_name, hs_file_name); } else { - return hs_file_name; + snprintf(buffer, sizeof(buffer), "%s/%s", getenv("PWD"), hs_file_name); + return buffer; } /* Create file only if it doesn't exist */ From 44f1223ea11238cee200e1b7ab6be1084544fb8c Mon Sep 17 00:00:00 2001 From: BROsander <53278658+BROsandr@users.noreply.github.com> Date: Sat, 4 Sep 2021 17:17:04 +0000 Subject: [PATCH 2/6] The default compilation was switched to curses. --- .replit | 2 +- Makefile | 3 ++- src/gfx_curses.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.replit b/.replit index 560dc58..7401e0c 100644 --- a/.replit +++ b/.replit @@ -1,2 +1,2 @@ language = "bash" -run = "make && ./2048" +run = "make && ./2048 -c" \ No newline at end of file diff --git a/Makefile b/Makefile index 0003ea0..3d3bc6c 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,8 @@ C_FILES := $(wildcard src/*.c) MERGE_FILE := src/merge_std.c FILTERED_C_FILES := $(filter-out src/gfx%.c src/merge%.c, $(C_FILES)) -all: terminal +#all: terminal +all: curses curses: $(FILTERED_C_FILES) src/gfx_curses.c $(CC) $(CFLAGS) $(FILTERED_C_FILES) $(MERGE_FILE) src/gfx_curses.c -o $(PROGRAM) $(LDFLAGS) -lcurses diff --git a/src/gfx_curses.c b/src/gfx_curses.c index 0a2682f..f7791de 100644 --- a/src/gfx_curses.c +++ b/src/gfx_curses.c @@ -3,7 +3,7 @@ #include #include "gfx.h" #include "merge.h" -#include +#include #include #define NUMBER_OF_COLORS 7 From deb5e777c7f14a9dfae48c22b34d3345f71c5579 Mon Sep 17 00:00:00 2001 From: BROsander <53278658+BROsandr@users.noreply.github.com> Date: Sat, 4 Sep 2021 17:31:03 +0000 Subject: [PATCH 3/6] Minor file relocations. --- Makefile | 2 +- src/gfx.h | 15 +++++++++++++++ src/gfx_curses.c | 12 +++++++----- src/gfx_terminal.c | 9 ++++++--- src/main.c | 7 ------- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 3d3bc6c..a466124 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ FILTERED_C_FILES := $(filter-out src/gfx%.c src/merge%.c, $(C_FILES)) all: curses curses: $(FILTERED_C_FILES) src/gfx_curses.c - $(CC) $(CFLAGS) $(FILTERED_C_FILES) $(MERGE_FILE) src/gfx_curses.c -o $(PROGRAM) $(LDFLAGS) -lcurses + $(CC) $(CFLAGS) -DNCURSES $(FILTERED_C_FILES) $(MERGE_FILE) src/gfx_curses.c -o $(PROGRAM) $(LFLAGS) -lcurses terminal: $(FILTERED_C_FILES) src/gfx_terminal.c $(CC) $(CFLAGS) $(FILTERED_C_FILES) $(MERGE_FILE) src/gfx_terminal.c -o $(PROGRAM) $(LDFLAGS) diff --git a/src/gfx.h b/src/gfx.h index 3974763..8fcd16b 100644 --- a/src/gfx.h +++ b/src/gfx.h @@ -7,6 +7,8 @@ #define INPUT_RIGHT 3 #include "engine.h" +#include +#include struct gfx_state; @@ -25,4 +27,17 @@ void gfx_destroy(struct gfx_state *); /* Sleep for a specifed millisecond period */ void gfx_sleep(int ms); +void draw_then_sleep(struct gfx_state *, struct gamestate *); + +#ifdef NCURSES +struct gfx_state { + WINDOW *window; + size_t window_height, window_width; +}; +#else +struct gfx_state { + struct termios oldt, newt; +}; +#endif + #endif diff --git a/src/gfx_curses.c b/src/gfx_curses.c index f7791de..461338b 100644 --- a/src/gfx_curses.c +++ b/src/gfx_curses.c @@ -8,17 +8,19 @@ #define NUMBER_OF_COLORS 7 +void draw_then_sleep(struct gfx_state *s, struct gamestate *g) +{ + gfx_draw(s, g); + /* Have a fixed time for each turn to animate (160 default) */ + gfx_sleep(160 / g->opts->grid_width); +} + #define iterate(n, expression)\ do {\ int i;\ for (i = 0; i < n; ++i) { expression; }\ } while (0) -struct gfx_state { - WINDOW *window; - size_t window_height, window_width; -}; - struct gfx_state* gfx_init(struct gamestate *g) { initscr(); diff --git a/src/gfx_terminal.c b/src/gfx_terminal.c index 93d0520..e874636 100644 --- a/src/gfx_terminal.c +++ b/src/gfx_terminal.c @@ -13,9 +13,12 @@ for (i = 0; i < n; ++i) { expression; }\ } while (0) -struct gfx_state { - struct termios oldt, newt; -}; +void draw_then_sleep(struct gfx_state *s, struct gamestate *g) +{ + gfx_draw(s, g); + /* Have a fixed time for each turn to animate (160 default) */ + gfx_sleep(160 / g->opts->grid_width); +} struct gfx_state* gfx_init(struct gamestate *g) { diff --git a/src/main.c b/src/main.c index 794f1cf..6d92c11 100644 --- a/src/main.c +++ b/src/main.c @@ -7,13 +7,6 @@ #include #include -void draw_then_sleep(struct gfx_state *s, struct gamestate *g) -{ - gfx_draw(s, g); - /* Have a fixed time for each turn to animate (160 default) */ - gfx_sleep(160 / g->opts->grid_width); -} - char *targetDir(char *env, char *path) { char *dir; From 3bd95d4e79209bb5ee20747bd415f01a51e434fe Mon Sep 17 00:00:00 2001 From: BROsander <53278658+BROsandr@users.noreply.github.com> Date: Sat, 4 Sep 2021 17:34:04 +0000 Subject: [PATCH 4/6] typo --- src/gfx_curses.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gfx_curses.c b/src/gfx_curses.c index 461338b..220dd45 100644 --- a/src/gfx_curses.c +++ b/src/gfx_curses.c @@ -73,7 +73,7 @@ void gfx_draw(struct gfx_state *s, struct gamestate *g) if (g->score >= g->score_high) g->score_high = g->score; - mvwprintw(s->window, 1, 0, gettext(" Hil: %d\n"), g->score_high); + mvwprintw(s->window, 1, 0, gettext(" Hi: %d\n"), g->score_high); wattron(s->window, A_DIM); iterate(g->opts->grid_width * (g->print_width + 2) + 1, waddch(s->window, '-')); From 211a74d8051f2ad305851d08897c7a2e4d9401b3 Mon Sep 17 00:00:00 2001 From: BROsander <53278658+BROsandr@users.noreply.github.com> Date: Sat, 4 Sep 2021 17:37:17 +0000 Subject: [PATCH 5/6] Fixed mkdir. --- src/gfx_terminal.c | 1 - src/highscore.c | 13 +++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/gfx_terminal.c b/src/gfx_terminal.c index e874636..2d59ad5 100644 --- a/src/gfx_terminal.c +++ b/src/gfx_terminal.c @@ -1,6 +1,5 @@ #include #include -#include #include #include "merge.h" #include "gfx.h" diff --git a/src/highscore.c b/src/highscore.c index 945ee24..120a30a 100644 --- a/src/highscore.c +++ b/src/highscore.c @@ -32,14 +32,15 @@ static const char* highscore_retrieve_file(void) if (access(buffer, F_OK) != -1) return buffer; - char *sep = strrchr(buffer, '/'); + char *sep = strchr(buffer + 1, '/'); + char *view = sep + 1; + while (sep != NULL) { *sep = '\0'; - if (strlen(buffer) != 0) - mkdir(buffer, 0777); - char *tmpsep = sep; - sep = strrchr(buffer, '/'); - *tmpsep = '/'; + mkdir(buffer, 0777); + *sep = '/'; + sep = strchr(view, '/'); + view = sep + 1; } return buffer; From 999f5e090af9bff1648d2d8cea8ad71fd7ccc04f Mon Sep 17 00:00:00 2001 From: BROsander <53278658+BROsandr@users.noreply.github.com> Date: Sat, 4 Sep 2021 17:58:24 +0000 Subject: [PATCH 6/6] Fixed fib --- Makefile | 3 ++- src/engine.c | 4 ++++ src/merge_fib.c | 5 ++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index a466124..67dc847 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,12 @@ CC ?= clang -CFLAGS += -Wno-visibility -Wno-incompatible-pointer-types -Wall -Wextra +CFLAGS += -Wall -Wextra CFLAGS += -DINVERT_COLORS -DVT100 -O2 LFLAGS += PROGRAM := 2048 C_FILES := $(wildcard src/*.c) MERGE_FILE := src/merge_std.c +#MERGE_FILE := src/merge_fib.c FILTERED_C_FILES := $(filter-out src/gfx%.c src/merge%.c, $(C_FILES)) #all: terminal diff --git a/src/engine.c b/src/engine.c index 0afc777..1616f02 100644 --- a/src/engine.c +++ b/src/engine.c @@ -186,7 +186,11 @@ void gamestate_new_block(struct gamestate *g) for (x = 0; x < g->opts->grid_width; ++x) { if (!g->grid[x][y]) { if (p == block_number) { +#ifdef NCURSES + g->grid[x][y] = 1; +#else g->grid[x][y] = rand() & 3 ? 1 : 2; +#endif g->blocks_in_play += 1; return; } diff --git a/src/merge_fib.c b/src/merge_fib.c index f1b8975..0bb9b1c 100644 --- a/src/merge_fib.c +++ b/src/merge_fib.c @@ -3,7 +3,7 @@ #define MERGE_GOAL (int)((sizeof(merge_values)/sizeof(merge_values[0]))-1) const long merge_values[] = { - 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, + 0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597 }; @@ -19,8 +19,7 @@ inline long merge_goal(void) inline int merge_possible(const int v1, const int v2) { - return v1 == v2 - 1 || v2 == v1 - 1 || - ((v1 == 1 || v1 == 2) && (v2 == 1 || v2 == 2)); + return ( ( (v1 == (v2 - 1) ) || ( v2 == (v1 - 1) ) || ( v2 == 1 && v1 == 1 ) )&& (v1 != 0 && v2 != 0) ); } inline int merge_result(const int v1, const int v2)