From dc9adea7b5bd83468e61735791b29cec99738b43 Mon Sep 17 00:00:00 2001 From: Tiehuis Date: Wed, 25 Feb 2015 23:54:13 +1300 Subject: [PATCH] Get rid of random blockgenerator in favor of a linear time one. Changed a sleep in ai to use gfx_sleep --- src/ai.c | 3 ++- src/engine.c | 29 +++++++++++++---------------- src/main.c | 2 +- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/ai.c b/src/ai.c index ed82d41..8c7f9e9 100644 --- a/src/ai.c +++ b/src/ai.c @@ -1,10 +1,11 @@ #include "ai.h" #include "engine.h" +#include "gfx.h" const char moves[] = {'w', 'a', 's', 'd'}; int ai_move(struct gamestate *g) { - if (g->opts->interactive) usleep(50000); + if (g->opts->interactive) gfx_sleep(50); return moves[rand() % 4]; } diff --git a/src/engine.c b/src/engine.c index 5ae9346..b74f340 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,3 +1,4 @@ +#include #include #include #include "merge.h" @@ -176,32 +177,28 @@ int gamestate_end_condition(struct gamestate *g) void gamestate_new_block(struct gamestate *g) { /* Exit early if there are no spaces to place a block */ - if (g->blocks_in_play == g->gridsize) return; + if (g->blocks_in_play >= g->gridsize) return; -#ifdef FIX int block_number = rand() % (g->gridsize - 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; + 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; + } } - - if (!g->grid[x][y]) p++; } - - if (y == g->opts->grid_height - 1) y = 0; } -#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() & 1 ? 1 : 2; - g->blocks_in_play += 1; + /* This should never be reached; but just in case */ + assert(0); } /* This returns the number of digits in the base10 rep of n. The ceiling is diff --git a/src/main.c b/src/main.c index 1045f8a..87df44e 100644 --- a/src/main.c +++ b/src/main.c @@ -13,7 +13,7 @@ void draw_then_sleep(struct gfx_state *s, struct gamestate *g) int main(int argc, char **argv) { - struct gamestate *g = gamestate_init(argc, argv); + struct gamestate *g = gamestate_init(argc, argv); struct gfx_state *s; if (g->opts->interactive)