2015-02-25 10:54:13 +00:00
|
|
|
#include <assert.h>
|
2014-12-04 21:27:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2015-02-22 23:32:29 +00:00
|
|
|
#include "merge.h"
|
2015-02-21 02:36:15 +00:00
|
|
|
#include "engine.h"
|
2015-02-21 22:29:15 +00:00
|
|
|
#include "highscore.h"
|
2014-12-04 21:27:25 +00:00
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
/* Utilize block counter to improve some of the functions so they can run
|
|
|
|
* quicker */
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* This function will move all blocks in the given game the given direction.
|
|
|
|
* The callback function is called after each single move. It can be used to
|
|
|
|
* animate the movement of the board. */
|
|
|
|
static void gravitate(struct gfx_state *s, struct gamestate *g, int d, void (*callback)(struct gfx_state *s, struct gamestate *g))
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
#define swap_if_space(xoff, yoff)\
|
|
|
|
do {\
|
|
|
|
if (g->grid[x][y] == 0 && g->grid[x+xoff][y+yoff] != 0) {\
|
|
|
|
g->grid[x][y] = g->grid[x+xoff][y+yoff];\
|
|
|
|
g->grid[x+xoff][y+yoff] = 0;\
|
|
|
|
done = 0;\
|
|
|
|
g->moved = 1;\
|
|
|
|
}\
|
|
|
|
} while (0)
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
int x, y;
|
2014-12-04 21:27:25 +00:00
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
if (d == dir_left) {
|
|
|
|
while (!done) {
|
|
|
|
done = 1;
|
|
|
|
for (x = 0; x < g->opts->grid_width - 1; ++x) {
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
|
|
|
swap_if_space(1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (callback)
|
2015-02-21 02:36:15 +00:00
|
|
|
callback(s, g);
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_right) {
|
|
|
|
while (!done) {
|
|
|
|
done = 1;
|
|
|
|
for (x = g->opts->grid_width - 1; x > 0; --x) {
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
|
|
|
swap_if_space(-1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (callback)
|
2015-02-21 02:36:15 +00:00
|
|
|
callback(s, g);
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_down) {
|
|
|
|
while (!done) {
|
|
|
|
done = 1;
|
|
|
|
for (y = g->opts->grid_height - 1; y > 0; --y) {
|
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
|
|
|
swap_if_space(0, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (callback)
|
2015-02-21 02:36:15 +00:00
|
|
|
callback(s, g);
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_up) {
|
|
|
|
while (!done) {
|
|
|
|
done = 1;
|
|
|
|
for (y = 0; y < g->opts->grid_height - 1; ++y) {
|
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
|
|
|
swap_if_space(0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (callback)
|
2015-02-21 02:36:15 +00:00
|
|
|
callback(s, g);
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fatal("Invalid direction passed to gravitate()");
|
|
|
|
/* Not reached */
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef swap_if_space
|
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* The merge function will combine adjacent blocks with the same value for
|
|
|
|
* the given direction. Note, left and right merges will merge in a different
|
|
|
|
* order, so they are not identical in all cases.
|
|
|
|
*
|
|
|
|
* Consider 2 2 2 0
|
|
|
|
*
|
|
|
|
* Right merging: 4 0 2 0
|
|
|
|
* Left merging: 2 0 4 0
|
|
|
|
*/
|
|
|
|
static void merge(struct gfx_state *s, struct gamestate *g, int d, void (*callback)(struct gfx_state *s, struct gamestate *g))
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
#define merge_if_equal(xoff, yoff)\
|
|
|
|
do {\
|
2015-02-22 23:32:29 +00:00
|
|
|
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]);\
|
2014-12-04 21:27:25 +00:00
|
|
|
g->grid[x+xoff][y+yoff] = 0;\
|
2014-12-07 09:09:22 +00:00
|
|
|
g->blocks_in_play -= 1;\
|
2015-02-22 23:32:29 +00:00
|
|
|
g->score_last += merge_value(g->grid[x][y]);\
|
|
|
|
g->score += merge_value(g->grid[x][y]);\
|
2014-12-04 21:27:25 +00:00
|
|
|
g->moved = 1;\
|
|
|
|
}\
|
|
|
|
} while (0)
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
int x, y;
|
2014-12-05 23:02:12 +00:00
|
|
|
g->score_last = 0;
|
2015-02-21 02:36:15 +00:00
|
|
|
|
2014-12-04 21:27:25 +00:00
|
|
|
if (d == dir_left) {
|
|
|
|
for (x = 0; x < g->opts->grid_width - 1; ++x) {
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
|
|
|
merge_if_equal(1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_right) {
|
|
|
|
for (x = g->opts->grid_width - 1; x > 0; --x) {
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
|
|
|
merge_if_equal(-1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_down) {
|
|
|
|
for (y = g->opts->grid_height - 1; y > 0; --y) {
|
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
|
|
|
merge_if_equal(0, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (d == dir_up) {
|
|
|
|
for (y = 0; y < g->opts->grid_height - 1; ++y) {
|
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
|
|
|
merge_if_equal(0, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fatal("Invalid direction passed to merge()");
|
|
|
|
/* Not reached */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback)
|
2015-02-21 02:36:15 +00:00
|
|
|
callback(s, g);
|
2014-12-04 21:27:25 +00:00
|
|
|
|
|
|
|
#undef merge_if_equal
|
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* Scan the current board and determine if an end outcome has been reached.
|
|
|
|
* -1 indicates a lose condition, 1 indicates a win condition, 0 indicates
|
|
|
|
* end has not yet been reached. */
|
|
|
|
int gamestate_end_condition(struct gamestate *g)
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
2014-12-06 00:43:00 +00:00
|
|
|
int ret = -1;
|
2015-02-21 02:36:15 +00:00
|
|
|
//size_t blocks_counted = 0;
|
2015-02-22 03:38:53 +00:00
|
|
|
int x, y;
|
2015-02-21 02:36:15 +00:00
|
|
|
|
2015-02-22 03:38:53 +00:00
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
2015-02-22 23:32:29 +00:00
|
|
|
if (g->grid[x][y] == merge_goal())
|
2015-02-21 02:36:15 +00:00
|
|
|
return 1;
|
2014-12-04 21:27:25 +00:00
|
|
|
if (!g->grid[x][y] || ((x + 1 < g->opts->grid_width) &&
|
2015-02-22 23:32:29 +00:00
|
|
|
merge_possible(g->grid[x][y], g->grid[x+1][y]))
|
2015-02-21 02:36:15 +00:00
|
|
|
|| ((y + 1 < g->opts->grid_height) &&
|
2015-02-22 23:32:29 +00:00
|
|
|
merge_possible(g->grid[x][y], g->grid[x][y+1])))
|
2014-12-06 00:43:00 +00:00
|
|
|
ret = 0;
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-06 00:43:00 +00:00
|
|
|
return ret;
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* Place a random block into the current grid */
|
|
|
|
void gamestate_new_block(struct gamestate *g)
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
2015-02-22 21:12:01 +00:00
|
|
|
/* Exit early if there are no spaces to place a block */
|
2015-02-25 10:54:13 +00:00
|
|
|
if (g->blocks_in_play >= g->gridsize) return;
|
2015-02-22 21:12:01 +00:00
|
|
|
|
2015-02-22 23:32:29 +00:00
|
|
|
int block_number = rand() % (g->gridsize - g->blocks_in_play);
|
2014-12-07 09:09:22 +00:00
|
|
|
|
2015-02-22 23:32:29 +00:00
|
|
|
int x, y, p = 0;
|
|
|
|
for (y = 0; y < g->opts->grid_height; ++y) {
|
|
|
|
for (x = 0; x < g->opts->grid_width; ++x) {
|
2015-02-25 10:54:13 +00:00
|
|
|
if (!g->grid[x][y]) {
|
|
|
|
if (p == block_number) {
|
|
|
|
g->grid[x][y] = rand() & 3 ? 1 : 2;
|
|
|
|
g->blocks_in_play += 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++p;
|
|
|
|
}
|
2015-02-22 23:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
2014-12-07 09:09:22 +00:00
|
|
|
|
2015-02-25 10:54:13 +00:00
|
|
|
/* This should never be reached; but just in case */
|
|
|
|
assert(0);
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
/* This returns the number of digits in the base10 rep of n. The ceiling is
|
|
|
|
* taken so this will be one greater than required */
|
2015-02-21 02:36:15 +00:00
|
|
|
static int digits_ceiling(unsigned int n)
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
|
|
|
int l = 0;
|
|
|
|
while (n) n /= 10, ++l;
|
2014-12-06 20:55:18 +00:00
|
|
|
return l + 1;
|
2014-12-04 21:27:25 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 21:47:41 +00:00
|
|
|
/* Return NULL if we couldn't allocate space for the gamestate. initializating the
|
|
|
|
* gamestate will parse the options internally, so any caller should pass argc and argv
|
|
|
|
* through this function */
|
|
|
|
struct gamestate* gamestate_init(int argc, char **argv)
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
2015-02-25 05:58:36 +00:00
|
|
|
struct gameoptions *opt = gameoptions_default();
|
2014-12-04 21:27:25 +00:00
|
|
|
if (!opt) return NULL;
|
|
|
|
|
2017-08-11 23:09:07 +00:00
|
|
|
if (argc != 0) parse_options(opt, argc, argv);
|
|
|
|
|
2015-02-22 23:32:29 +00:00
|
|
|
srand(time(NULL));
|
|
|
|
|
2014-12-04 21:27:25 +00:00
|
|
|
struct gamestate *g = malloc(sizeof(struct gamestate));
|
|
|
|
if (!g) goto gamestate_alloc_fail;
|
2014-12-07 09:09:22 +00:00
|
|
|
g->gridsize = opt->grid_width * opt->grid_height;
|
2014-12-04 21:27:25 +00:00
|
|
|
|
2015-02-22 23:32:29 +00:00
|
|
|
g->grid_data_ptr = calloc(g->gridsize, sizeof(int));
|
2015-02-21 02:36:15 +00:00
|
|
|
if (!g->grid_data_ptr) goto grid_data_alloc_fail;
|
|
|
|
|
2015-02-22 23:32:29 +00:00
|
|
|
g->grid = malloc(opt->grid_height * sizeof(int*));
|
2014-12-04 21:27:25 +00:00
|
|
|
if (!g->grid) goto grid_alloc_fail;
|
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
/* Switch to two allocation version */
|
2015-02-22 03:38:53 +00:00
|
|
|
int i;
|
2015-02-22 23:32:29 +00:00
|
|
|
int **iterator = g->grid;
|
2015-02-22 03:38:53 +00:00
|
|
|
for (i = 0; i < g->gridsize; i += opt->grid_width)
|
2015-02-21 02:36:15 +00:00
|
|
|
*iterator++ = &g->grid_data_ptr[i];
|
2014-12-07 09:09:22 +00:00
|
|
|
|
2014-12-04 21:27:25 +00:00
|
|
|
g->moved = 0;
|
|
|
|
g->score = 0;
|
|
|
|
g->score_high = 0;
|
|
|
|
g->score_last = 0;
|
2015-02-22 23:32:29 +00:00
|
|
|
g->print_width = digits_ceiling(merge_value(merge_goal()));
|
2014-12-07 09:09:22 +00:00
|
|
|
g->blocks_in_play = 0;
|
2014-12-04 21:27:25 +00:00
|
|
|
g->opts = opt;
|
|
|
|
|
2015-02-22 21:12:01 +00:00
|
|
|
/* Clamp spawn rate to maximum to avoid possible excessive calculation
|
|
|
|
* int generation of blocks */
|
|
|
|
if (g->opts->spawn_rate > g->gridsize)
|
|
|
|
g->opts->spawn_rate = g->gridsize;
|
|
|
|
|
2015-02-21 22:29:15 +00:00
|
|
|
highscore_load(g);
|
|
|
|
|
2014-12-07 09:09:22 +00:00
|
|
|
/* Initial 3 random blocks */
|
2015-02-21 02:36:15 +00:00
|
|
|
gamestate_new_block(g);
|
|
|
|
gamestate_new_block(g);
|
|
|
|
gamestate_new_block(g);
|
2014-12-04 21:27:25 +00:00
|
|
|
return g;
|
|
|
|
|
|
|
|
grid_alloc_fail:
|
2015-02-21 02:36:15 +00:00
|
|
|
free(g->grid_data_ptr);
|
|
|
|
grid_data_alloc_fail:
|
2014-12-04 21:27:25 +00:00
|
|
|
free(g);
|
|
|
|
gamestate_alloc_fail:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* A tick is a gravitate, merge then gravitate all in the same direction.
|
|
|
|
* the moved variable is set to 0 initially and if the gravitate of merge
|
|
|
|
* functions modify it, we can determine which action to take. */
|
|
|
|
int gamestate_tick(struct gfx_state *s, struct gamestate *g, int d, void (*callback)(struct gfx_state*, struct gamestate*))
|
2014-12-04 21:27:25 +00:00
|
|
|
{
|
2014-12-07 09:09:22 +00:00
|
|
|
/* Reset move. Altered by gravitate and merge if we do move */
|
2014-12-04 21:27:25 +00:00
|
|
|
g->moved = 0;
|
2015-02-21 02:36:15 +00:00
|
|
|
gravitate(s, g, d, callback);
|
|
|
|
merge(s, g, d, callback);
|
|
|
|
gravitate(s, g, d, callback);
|
2014-12-04 21:27:25 +00:00
|
|
|
return g->moved;
|
|
|
|
}
|
|
|
|
|
2015-02-21 02:36:15 +00:00
|
|
|
/* Free all data associated with the gamestate */
|
2014-12-04 21:27:25 +00:00
|
|
|
void gamestate_clear(struct gamestate *g)
|
|
|
|
{
|
2015-02-21 22:29:15 +00:00
|
|
|
highscore_save(g);
|
2015-02-21 02:36:15 +00:00
|
|
|
gameoptions_destroy(g->opts);
|
|
|
|
free(g->grid_data_ptr); /* Free grid data */
|
2014-12-07 09:09:22 +00:00
|
|
|
free(g->grid); /* Free pointers to data slots */
|
2014-12-04 21:27:25 +00:00
|
|
|
free(g);
|
|
|
|
}
|