diff --git a/Makefile b/Makefile index 39222a3..164cbef 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CC ?= clang +CC := clang CFLAGS += -g -Wall -Wextra LFLAGS += DEFINES := -DVT100 $(shell pkg-config --cflags sdl2) diff --git a/src/engine.c b/src/engine.c index 42d35ac..f95b1c2 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,5 +1,6 @@ #include #include +#include "merge.h" #include "engine.h" #include "highscore.h" @@ -95,12 +96,12 @@ static void merge(struct gfx_state *s, struct gamestate *g, int d, void (*callba #define merge_if_equal(xoff, yoff)\ do {\ - if (g->grid[x][y] && (g->grid[x][y] == g->grid[x+xoff][y+yoff])) {\ - g->grid[x][y] += g->grid[x+xoff][y+yoff];\ + if (g->grid[x][y] && (merge_possible(g->grid[x][y], g->grid[x+xoff][y+yoff]))) {\ + g->grid[x][y] = merge_result(g->grid[x][y], g->grid[x+xoff][y+yoff]);\ g->grid[x+xoff][y+yoff] = 0;\ g->blocks_in_play -= 1;\ - g->score_last += g->grid[x][y];\ - g->score += g->grid[x][y];\ + g->score_last += merge_value(g->grid[x][y]);\ + g->score += merge_value(g->grid[x][y]);\ g->moved = 1;\ }\ } while (0) @@ -158,12 +159,12 @@ int gamestate_end_condition(struct gamestate *g) for (x = 0; x < g->opts->grid_width; ++x) { for (y = 0; y < g->opts->grid_height; ++y) { - if (g->grid[x][y] >= g->opts->goal) + if (g->grid[x][y] == merge_goal()) return 1; if (!g->grid[x][y] || ((x + 1 < g->opts->grid_width) && - (g->grid[x][y] == g->grid[x+1][y])) + merge_possible(g->grid[x][y], g->grid[x+1][y])) || ((y + 1 < g->opts->grid_height) && - (g->grid[x][y] == g->grid[x][y+1]))) + merge_possible(g->grid[x][y], g->grid[x][y+1]))) ret = 0; } } @@ -174,43 +175,30 @@ int gamestate_end_condition(struct gamestate *g) /* Place a random block into the current grid */ void gamestate_new_block(struct gamestate *g) { - /* pick random square, if it is full, then move forward until we find - * an empty square. This is biased */ - - static int seeded = 0; - if (!seeded) { - seeded = 1; - srand(time(NULL)); - } - /* Exit early if there are no spaces to place a block */ if (g->blocks_in_play == g->gridsize) return; - /* Fix up this random number generator */ - /* Method: - * - Find a non-biased index between 0 and blocks_play, n - * - Find the nth 0 element in the array - * - insert a random value there - */ +#ifdef FIX + int block_number = rand() % (g->gridsize - g->blocks_in_play); - /* Error here */ -#ifdef SKIP - size_t block_position = (size_t)rand() % ( - g->opts->grid_width * g->opts->grid_height - g->blocks_in_play); + int x, y, p = 0; + for (y = 0; y < g->opts->grid_height; ++y) { + for (x = 0; x < g->opts->grid_width; ++x) { + if (p == block_number) { + g->grid[x][y] = rand() & 1 ? 1 : 2; + g->blocks_in_play += 1; + return; + } - size_t i, ps; - for (i = 0, ps = 0; ps < block_position; ++i) { - if (!g->grid[i / g->opts->grid_width][i % g->opts->grid_height]) ps++; + if (!g->grid[x][y]) p++; + } } - - g->grid[i / g->opts->grid_width][i % g->opts->grid_height] - = (rand() & 3) ? g->opts->spawn_value : g->opts->spawn_value * 2; #endif /* Use rudimentary for now */ int x, y; while (g->grid[x = rand() % g->opts->grid_width][y = rand() % g->opts->grid_height]); - g->grid[x][y] = (rand() & 3) ? g->opts->spawn_value : g->opts->spawn_value * 2; + g->grid[x][y] = rand() & 1 ? 1 : 2; g->blocks_in_play += 1; } @@ -231,19 +219,21 @@ struct gamestate* gamestate_init(struct gameoptions *opt) { if (!opt) return NULL; + srand(time(NULL)); + struct gamestate *g = malloc(sizeof(struct gamestate)); if (!g) goto gamestate_alloc_fail; g->gridsize = opt->grid_width * opt->grid_height; - g->grid_data_ptr = calloc(g->gridsize, sizeof(long)); + g->grid_data_ptr = calloc(g->gridsize, sizeof(int)); if (!g->grid_data_ptr) goto grid_data_alloc_fail; - g->grid = malloc(opt->grid_height * sizeof(long*)); + g->grid = malloc(opt->grid_height * sizeof(int*)); if (!g->grid) goto grid_alloc_fail; /* Switch to two allocation version */ int i; - long **iterator = g->grid; + int **iterator = g->grid; for (i = 0; i < g->gridsize; i += opt->grid_width) *iterator++ = &g->grid_data_ptr[i]; @@ -251,7 +241,7 @@ struct gamestate* gamestate_init(struct gameoptions *opt) g->score = 0; g->score_high = 0; g->score_last = 0; - g->print_width = digits_ceiling(opt->goal); + g->print_width = digits_ceiling(merge_value(merge_goal())); g->blocks_in_play = 0; g->opts = opt; diff --git a/src/engine.h b/src/engine.h index 0e90f07..3aff024 100644 --- a/src/engine.h +++ b/src/engine.h @@ -13,8 +13,8 @@ struct gamestate { /* Game state */ - long *grid_data_ptr; - long **grid; + int *grid_data_ptr; + int **grid; int gridsize; int moved; long score; diff --git a/src/gfx.h b/src/gfx.h index 06f88fa..c88e3c4 100644 --- a/src/gfx.h +++ b/src/gfx.h @@ -3,13 +3,6 @@ #include "engine.h" -/* These can be defined by an implemenation if special codes are used - * to represent some keys. e.g. KEY_LEFT in ncurses */ -#define GFX_RIGHT_EXTRA -#define GFX_LEFT_EXTRA -#define GFX_DOWN_EXTRA -#define GFX_UP_EXTRA - struct gfx_state; /* Initialization of a graphics context */ diff --git a/src/gfx_curses.c b/src/gfx_curses.c index d9869ef..d9e45a7 100644 --- a/src/gfx_curses.c +++ b/src/gfx_curses.c @@ -3,11 +3,6 @@ #include #include "gfx.h" -#define GFX_EXTRA_UP case KEY_UP: -#define GFX_EXTRA_DOWN case KEY_DOWN: -#define GFX_EXTRA_RIGHT case KEY_RIGHT: -#define GFX_EXTRA_LEFT case KEY_LEFT: - #define NUMBER_OF_COLORS 7 #define iterate(n, expression)\ @@ -38,28 +33,18 @@ struct gfx_state* gfx_init(struct gamestate *g) if (g->opts->enable_color && has_colors()) { start_color(); - int x = 0; - init_pair(x++, 1, 0); - init_pair(x++, 2, 0); - init_pair(x++, 3, 0); - init_pair(x++, 4, 0); - init_pair(x++, 5, 0); - init_pair(x++, 6, 0); - init_pair(x++, 7, 0); - char dummy[x == NUMBER_OF_COLORS ? 1 : -1]; + init_pair(0, 1, 0); + init_pair(1, 2, 0); + init_pair(2, 3, 0); + init_pair(3, 4, 0); + init_pair(4, 5, 0); + init_pair(5, 6, 0); + init_pair(6, 7, 0); } return s; } -static int int_log2(int n) -{ - int k = 0; - while (n) - ++k, n /= 2; - return k; -} - void gfx_draw(struct gfx_state *s, struct gamestate *g) { if (g->score_last) @@ -79,9 +64,9 @@ void gfx_draw(struct gfx_state *s, struct gamestate *g) for (x = 0; x < g->opts->grid_width; ++x) { if (g->grid[x][y]) { - wattron(s->window, COLOR_PAIR(int_log2(g->grid[x][y]) % NUMBER_OF_COLORS)); - mvwprintw(s->window, ypos, xpos, "%*d", g->print_width, g->grid[x][y]); - wattroff(s->window, COLOR_PAIR(int_log2(g->grid[x][y]) % NUMBER_OF_COLORS)); + wattron(s->window, COLOR_PAIR(g->grid[x][y] % NUMBER_OF_COLORS)); + mvwprintw(s->window, ypos, xpos, "%*ld", g->print_width, merge_value(g->grid[x][y])); + wattroff(s->window, COLOR_PAIR(g->grid[x][y] % NUMBER_OF_COLORS)); mvwprintw(s->window, ypos, xpos + g->print_width, " |"); } else { diff --git a/src/gfx_sdl.c b/src/gfx_sdl.c index c3b11cd..ac5bd99 100644 --- a/src/gfx_sdl.c +++ b/src/gfx_sdl.c @@ -102,7 +102,7 @@ void gfx_draw(struct gfx_state *s, struct gamestate *g) for (x = 0; x < g->opts->grid_width; ++x) { if (g->grid[x][y]) { - snprintf(string_buffer, buffer_length, "%*ld |", g->print_width, g->grid[x][y]); + snprintf(string_buffer, buffer_length, "%*ld |", g->print_width, merge_value(g->grid[x][y])); strncpy(line_buffer + line_index, string_buffer, buffer_length - line_index); line_index += strlen(string_buffer); } diff --git a/src/gfx_terminal.c b/src/gfx_terminal.c index cad79e5..d9a728c 100644 --- a/src/gfx_terminal.c +++ b/src/gfx_terminal.c @@ -45,7 +45,7 @@ void gfx_draw(struct gfx_state *s, struct gamestate *g) for (x = 0; x < g->opts->grid_width; ++x) { if (g->grid[x][y]) - printf("%*zd |", g->print_width, g->grid[x][y]); + printf("%*zd |", g->print_width, merge_value(g->grid[x][y])); else printf("%*s |", g->print_width, ""); } diff --git a/src/highscore.c b/src/highscore.c index 3ac9350..7d3a771 100644 --- a/src/highscore.c +++ b/src/highscore.c @@ -64,6 +64,8 @@ void highscore_load(struct gamestate *g) void highscore_save(struct gamestate *g) { + /* Someone could make their own merge rules for highscores and this could be meaningless, + * howeverhighscores are in plaintext, so that isn't that much of a concern */ if (g->score < g->score_high || g->opts->grid_width != 4 || g->opts->grid_height != 4) return; diff --git a/src/merge.c b/src/merge.c new file mode 100644 index 0000000..086bea7 --- /dev/null +++ b/src/merge.c @@ -0,0 +1,30 @@ +#include "assert.h" +#include "merge.h" + +#define MERGE_GOAL 11 + +:a +const long merge_values[] = { + 0, 2, 4, 8, 16, 32, 64, 128, 256, 512, + 1024, 2048 +}; + +inline long merge_value(const int v1) +{ + return v1 <= MERGE_GOAL ? merge_values[v1] : -1; +} + +inline long merge_goal(void) +{ + return MERGE_GOAL; +} + +inline int merge_possible(const int v1, const int v2) +{ + return v1 == v2; +} + +inline int merge_result(const int v1, const int v2) +{ + return merge_possible(v1, v2) ? v1 + 1 : -1; +} diff --git a/src/merge.h b/src/merge.h new file mode 100644 index 0000000..37cf30a --- /dev/null +++ b/src/merge.h @@ -0,0 +1,21 @@ +#ifndef MERGE_H +#define MERGE_H + +/* When defining a new set of rules, remember that the grid values (and the + * therefore the arguments v1, v2) are index values and should be treated as + * such */ + +/* This should return the value of a given index. 0 should default to 0 (empty) */ +long merge_value(const int v1); + +/* This should return the goal value */ +long merge_goal(void); + +/* Return if a merge is possible between two indices */ +int merge_possible(const int v1, const int v2); + +/* The result of a merge of two values. If the two parameters are not mergeable, + * then a -1 error code is the suggested return value */ +int merge_result(const int v1, const int v2); + +#endif diff --git a/src/merge_fib.c.nomk b/src/merge_fib.c.nomk new file mode 100644 index 0000000..68441fe --- /dev/null +++ b/src/merge_fib.c.nomk @@ -0,0 +1,30 @@ +#include "assert.h" +#include "merge.h" + +#define MERGE_GOAL 17 + +const long merge_values[] = { + 0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, + 233, 377, 610, 987, 1597, 2584 +}; + +inline long merge_value(const int v1) +{ + return v1 <= MERGE_GOAL ? merge_values[v1] : -1; +} + +inline long merge_goal(void) +{ + return MERGE_GOAL; +} + +inline int merge_possible(const int v1, const int v2) +{ + return v1 == v2 - 1 || v2 == v1 - 1; +} + +inline int merge_result(const int v1, const int v2) +{ + int max = v1 > v2 ? v1 : v2; + return merge_possible(v1, v2) ? max + 1 : -1; +} diff --git a/src/options.c b/src/options.c index 52d7d26..d3974e0 100644 --- a/src/options.c +++ b/src/options.c @@ -5,7 +5,7 @@ void print_usage(void) { - printf("usage: 2048 [-cCaArh] [-s SIZE] [-b RATE] [-g GOAL]\n"); + printf("usage: 2048 [-cCaArh] [-s SIZE] [-b RATE]\n"); } @@ -17,7 +17,6 @@ struct gameoptions* gameoptions_default(void) opt->grid_height = DEFAULT_GRID_HEIGHT; opt->grid_width = DEFAULT_GRID_WIDTH; - opt->goal = DEFAULT_GOAL; opt->spawn_value = DEFAULT_SPAWN_VALUE; opt->spawn_rate = DEFAULT_SPAWN_RATE; opt->enable_color = DEFAULT_COLOR_TOGGLE; @@ -34,7 +33,7 @@ void gameoptions_destroy(struct gameoptions *opt) struct gameoptions* parse_options(struct gameoptions *opt, int argc, char **argv) { int c; - while ((c = getopt(argc, argv, "aArcChg:s:b:")) != -1) { + while ((c = getopt(argc, argv, "aArcCh:s:b:")) != -1) { switch (c) { case 'a': opt->animate = 1; @@ -48,9 +47,6 @@ struct gameoptions* parse_options(struct gameoptions *opt, int argc, char **argv case 'C': opt->enable_color = 0; break; - case 'g': - opt->goal = strtol(optarg, NULL, 10); - break; case 's':; /* Stick with square for now */ int optint = strtol(optarg, NULL, 10); diff --git a/src/options.h b/src/options.h index 3b80f64..f5e36e1 100644 --- a/src/options.h +++ b/src/options.h @@ -7,7 +7,6 @@ #define CONSTRAINT_GRID_MAX 20 #define DEFAULT_GRID_HEIGHT 4 #define DEFAULT_GRID_WIDTH 4 -#define DEFAULT_GOAL 2048 #define DEFAULT_SPAWN_VALUE 2 #define DEFAULT_SPAWN_RATE 1 #define DEFAULT_COLOR_TOGGLE 0 @@ -16,7 +15,6 @@ struct gameoptions { int grid_height; int grid_width; - long goal; long spawn_value; int spawn_rate; int enable_color;