Various changes. Alter grid allocation. Add current block count

This commit is contained in:
Tiehuis 2014-12-07 22:09:22 +13:00
parent 3085bd4eb6
commit 0a76b9c452
3 changed files with 97 additions and 29 deletions

View File

@ -2,6 +2,9 @@
#include <time.h> #include <time.h>
#include "2048_engine.h" #include "2048_engine.h"
/* Utilize block counter to improve some of the functions so they can run
* quicker */
void gravitate(struct gamestate *g, direction d, void (*callback)(struct gamestate *g)) void gravitate(struct gamestate *g, direction d, void (*callback)(struct gamestate *g))
{ {
@ -72,7 +75,6 @@ void gravitate(struct gamestate *g, direction d, void (*callback)(struct gamesta
} }
#undef swap_if_space #undef swap_if_space
} }
void merge(struct gamestate *g, direction d, void (*callback)(struct gamestate *g)) void merge(struct gamestate *g, direction d, void (*callback)(struct gamestate *g))
@ -83,6 +85,7 @@ void merge(struct gamestate *g, direction d, void (*callback)(struct gamestate *
if (g->grid[x][y] && (g->grid[x][y] == g->grid[x+xoff][y+yoff])) {\ 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];\ g->grid[x][y] += g->grid[x+xoff][y+yoff];\
g->grid[x+xoff][y+yoff] = 0;\ g->grid[x+xoff][y+yoff] = 0;\
g->blocks_in_play -= 1;\
g->score_last += g->grid[x][y];\ g->score_last += g->grid[x][y];\
g->score += g->grid[x][y];\ g->score += g->grid[x][y];\
g->moved = 1;\ g->moved = 1;\
@ -136,10 +139,18 @@ void merge(struct gamestate *g, direction d, void (*callback)(struct gamestate *
int end_condition(struct gamestate *g) int end_condition(struct gamestate *g)
{ {
int ret = -1; int ret = -1;
size_t blocks_counted = 0;
size_t x, y; size_t x, y;
for (x = 0; x < g->opts->grid_width; ++x) { for (x = 0; x < g->opts->grid_width; ++x) {
for (y = 0; y < g->opts->grid_height; ++y) { for (y = 0; y < g->opts->grid_height; ++y) {
if (g->grid[x][y]) {
blocks_counted++;
if (g->grid[x][y] >= g->opts->goal)
return 1;
else if (blocks_counted >= g->blocks_in_play)
return ret;
}
if (g->grid[x][y] >= g->opts->goal) if (g->grid[x][y] >= g->opts->goal)
return 1; return 1;
if (!g->grid[x][y] || ((x + 1 < g->opts->grid_width) && if (!g->grid[x][y] || ((x + 1 < g->opts->grid_width) &&
@ -153,6 +164,9 @@ int end_condition(struct gamestate *g)
return ret; return ret;
} }
/* Find a better method for getting a random square. It would be useful to keep
* track of how many blocks are in play, which we can query here, end_condition
* to improve them. */
void random_block(struct gamestate *g) void random_block(struct gamestate *g)
{ {
/* pick random square, if it is full, then move forward until we find /* pick random square, if it is full, then move forward until we find
@ -164,22 +178,37 @@ void random_block(struct gamestate *g)
srand(time(NULL)); srand(time(NULL));
} }
size_t x = (size_t)rand() % g->opts->grid_width; /* Fix up this random number generator */
size_t y = (size_t)rand() % g->opts->grid_height; /* 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
*/
while (g->grid[x][y]) { /* Error here */
x++; #ifdef NULLO
if (x == g->opts->grid_width) { size_t block_position = (size_t)rand() % (
x = 0; y++; g->opts->grid_width * g->opts->grid_height - g->blocks_in_play);
if (y == g->opts->grid_height)
y = 0; 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++;
} }
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() & 3) ? g->opts->spawn_value : g->opts->spawn_value * 2;
g->blocks_in_play += 1;
} }
static int flog10(unsigned int n) /* This returns the number of digits in the base10 rep of n. The ceiling is
* taken so this will be one greater than required */
static int clog10(unsigned int n)
{ {
int l = 0; int l = 0;
while (n) n /= 10, ++l; while (n) n /= 10, ++l;
@ -192,28 +221,41 @@ struct gamestate* gamestate_init(struct gameoptions *opt)
struct gamestate *g = malloc(sizeof(struct gamestate)); struct gamestate *g = malloc(sizeof(struct gamestate));
if (!g) goto gamestate_alloc_fail; if (!g) goto gamestate_alloc_fail;
g->gridsize = opt->grid_width * opt->grid_height;
//long *grid_back = malloc(opt->grid_width * opt->grid_height * sizeof(long)); long *grid_back = calloc(g->gridsize, sizeof(long));
//if (!grid_back) goto grid_back_alloc_fail; if (!grid_back) goto grid_back_alloc_fail;
g->grid = malloc(opt->grid_width * sizeof(long*)); g->grid = malloc(opt->grid_height * sizeof(long*));
if (!g->grid) goto grid_alloc_fail; if (!g->grid) goto grid_alloc_fail;
/* Switch to two allocation version */
size_t i; size_t i;
for (i = 0; i < opt->grid_height; ++i) long **iterator = g->grid;
g->grid[i] = calloc(opt->grid_height, sizeof(long)); for (i = 0; i < g->gridsize; i += opt->grid_width)
*iterator++ = &grid_back[i];
/* Switch back to the two allocs, setting pointers by iterating though */
//size_t i;
//for (i = 0; i < opt->grid_height; ++i)
// g->grid[i] = calloc(opt->grid_height, sizeof(long));
g->moved = 0; g->moved = 0;
g->score = 0; g->score = 0;
g->score_high = 0; g->score_high = 0;
g->score_last = 0; g->score_last = 0;
g->print_width = flog10(opt->goal); g->print_width = clog10(opt->goal);
g->blocks_in_play = 0;
g->opts = opt; g->opts = opt;
/* Initial 3 random blocks */
random_block(g);
random_block(g);
random_block(g);
return g; return g;
grid_alloc_fail: grid_alloc_fail:
//grid_back_alloc_fail: grid_back_alloc_fail:
free(g); free(g);
gamestate_alloc_fail: gamestate_alloc_fail:
return NULL; return NULL;
@ -237,6 +279,7 @@ struct gameoptions* gameoptions_default(void)
int gamestate_tick(struct gamestate *g, direction d, void (*callback)(struct gamestate*)) int gamestate_tick(struct gamestate *g, direction d, void (*callback)(struct gamestate*))
{ {
/* Reset move. Altered by gravitate and merge if we do move */
g->moved = 0; g->moved = 0;
gravitate(g, d, callback); gravitate(g, d, callback);
merge(g, d, callback); merge(g, d, callback);
@ -247,8 +290,8 @@ int gamestate_tick(struct gamestate *g, direction d, void (*callback)(struct gam
void gamestate_clear(struct gamestate *g) void gamestate_clear(struct gamestate *g)
{ {
free(g->opts); free(g->opts);
free(g->grid[0]); free(g->grid[0]); /* Free grid data */
free(g->grid); free(g->grid); /* Free pointers to data slots */
free(g); free(g);
} }

View File

@ -39,11 +39,13 @@ struct gameoptions {
struct gamestate { struct gamestate {
/* Game state */ /* Game state */
long **grid; long **grid;
size_t gridsize;
int moved; int moved;
long score; long score;
long score_high; long score_high;
long score_last; long score_last;
int print_width; size_t print_width;
size_t blocks_in_play;
/* Options */ /* Options */
struct gameoptions *opts; struct gameoptions *opts;
}; };
@ -54,7 +56,7 @@ void merge(struct gamestate*, direction, void (*callback)(struct gamestate*));
int end_condition(struct gamestate *); int end_condition(struct gamestate *);
void random_block(struct gamestate *); void random_block(struct gamestate *);
int gamestate_tick(struct gamestate*, direction, void (*callback)(struct gamestate*)); int gamestate_tick(struct gamestate*, direction, void (*callback)(struct gamestate*));
void gamestate_clear(struct gamestate*);; void gamestate_clear(struct gamestate*);
struct gamestate* gamestate_init(struct gameoptions *); struct gamestate* gamestate_init(struct gameoptions *);
struct gameoptions* gameoptions_default(void); struct gameoptions* gameoptions_default(void);
#endif #endif

View File

@ -1,18 +1,34 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ncurses.h>
#include <termios.h>
#include <unistd.h>
#include "2048_engine.h" #include "2048_engine.h"
/* Need to alter termios settings before actually cross-platform. */
/* It will be something like this, maybe switch to using a time.h sleep
* method to get by os dependence, too? */
#if (defined _WIN32 || defined __WIN64)
#include <windows.h>
#define syssleep(s) Sleep(s)
#elif defined __unix__
#include <unistd.h>
#define syssleep(s) usleep((s) * 1000)
#else
#error "Unsupported os: no sleep function"
#endif
#ifdef HAVE_CURSES
#include <ncurses.h>
#else
#include <termios.h>
#endif
#define iterate(x, expr)\ #define iterate(x, expr)\
do {\ do {\
size_t i;\ size_t i;\
for (i = 0; i < x; ++i) { expr; }\ for (i = 0; i < x; ++i) { expr; }\
} while (0) } while (0)
#ifdef HAVE_CURSES #ifdef HAVE_CURSES /* PLATFORM/OUTPUT DEPENDENT */
void draw_screen(struct gamestate *g) void draw_screen(struct gamestate *g)
{ {
static WINDOW *gamewin; static WINDOW *gamewin;
@ -73,6 +89,9 @@ int get_keypress(void)
} }
#else /* vt100 and standard shared functions */ #else /* vt100 and standard shared functions */
/* Search for windows, blocking, non newline getchar */
/* Could bypass termios that way */
struct termios sattr; struct termios sattr;
void drawstate_clear() void drawstate_clear()
{ {
@ -83,13 +102,15 @@ void drawstate_init(void)
{ {
tcgetattr(STDIN_FILENO, &sattr); tcgetattr(STDIN_FILENO, &sattr);
/* alters terminal stdin to not echo and doesn't need \n before reading getchar */
struct termios tattr; struct termios tattr;
tcgetattr(STDIN_FILENO, &tattr); tcgetattr(STDIN_FILENO, &tattr);
tattr.c_lflag &= ~(ICANON | ECHO); tattr.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDOUT_FILENO, TCSANOW, &tattr); tcsetattr(STDOUT_FILENO, TCSANOW, &tattr);
} }
/* Need to check input queue, and remove all queued values on each getc to get
* by any caught chars during any sleeps which may occur. This is easy in the
* ncurses version (check until ERR), but requires extra tweaking here */
int get_keypress(void) int get_keypress(void)
{ {
return fgetc(stdin); return fgetc(stdin);
@ -98,6 +119,8 @@ int get_keypress(void)
void draw_screen(struct gamestate *g) void draw_screen(struct gamestate *g)
{ {
/* Clear the screen each draw if we are able to */ /* Clear the screen each draw if we are able to */
/* Check for windows clear screen */
#ifdef VT100_COMPATIBLE #ifdef VT100_COMPATIBLE
printf("\033[2J\033[H"); printf("\033[2J\033[H");
#endif #endif
@ -123,12 +146,12 @@ void draw_screen(struct gamestate *g)
iterate((g->print_width + 2) * g->opts->grid_width + 1, printf("-")); iterate((g->print_width + 2) * g->opts->grid_width + 1, printf("-"));
printf("\n\n"); printf("\n\n");
} }
#endif /* CURSES */ #endif /* PLATFORM/OUTPUT DEPENDENT */
void ddraw(struct gamestate *g) void ddraw(struct gamestate *g)
{ {
draw_screen(g); draw_screen(g);
usleep(40000); syssleep(40);
} }
int main(int argc, char **argv) int main(int argc, char **argv)