Added easy extension of custom merge results. Example fib merge rules added. Code altered to make use of this new style

This commit is contained in:
Tiehuis 2015-02-23 12:32:29 +13:00
parent 375cdeb7da
commit 6d247f282f
13 changed files with 127 additions and 82 deletions

View File

@ -1,4 +1,4 @@
CC ?= clang
CC := clang
CFLAGS += -g -Wall -Wextra
LFLAGS +=
DEFINES := -DVT100 $(shell pkg-config --cflags sdl2)

View File

@ -1,5 +1,6 @@
#include <stdlib.h>
#include <time.h>
#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;

View File

@ -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;

View File

@ -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 */

View File

@ -3,11 +3,6 @@
#include <unistd.h>
#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 {

View File

@ -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);
}

View File

@ -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, "");
}

View File

@ -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;

30
src/merge.c Normal file
View File

@ -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;
}

21
src/merge.h Normal file
View File

@ -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

30
src/merge_fib.c.nomk Normal file
View File

@ -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;
}

View File

@ -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);

View File

@ -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;