Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Fix branch #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .replit
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
language = "bash"
run = "make && ./2048"
run = "make && ./2048 -c"
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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
#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
$(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)
Expand Down
4 changes: 4 additions & 0 deletions src/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions src/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#define INPUT_RIGHT 3

#include "engine.h"
#include <ncurses.h>
#include <termios.h>

struct gfx_state;

Expand All @@ -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
16 changes: 9 additions & 7 deletions src/gfx_curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
#include <unistd.h>
#include "gfx.h"
#include "merge.h"
#include <gettext.h>
#include <libintl.h>
#include <locale.h>

#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();
Expand Down Expand Up @@ -71,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, '-'));
Expand Down
10 changes: 6 additions & 4 deletions src/gfx_terminal.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include "merge.h"
#include "gfx.h"
Expand All @@ -13,9 +12,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)
{
Expand Down
16 changes: 9 additions & 7 deletions src/highscore.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@ 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 */
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;
Expand Down
7 changes: 0 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@
#include <locale.h>
#include <string.h>

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;
Expand Down
5 changes: 2 additions & 3 deletions src/merge_fib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand All @@ -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)
Expand Down