diff --git a/src/engine.c b/src/engine.c index f3bac09..42d35ac 100644 --- a/src/engine.c +++ b/src/engine.c @@ -183,6 +183,9 @@ void gamestate_new_block(struct gamestate *g) 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 @@ -252,6 +255,11 @@ struct gamestate* gamestate_init(struct gameoptions *opt) g->blocks_in_play = 0; g->opts = opt; + /* 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; + highscore_load(g); /* Initial 3 random blocks */ diff --git a/src/main.c b/src/main.c index a724ed2..949e3de 100644 --- a/src/main.c +++ b/src/main.c @@ -47,18 +47,18 @@ get_new_key:; /* Game will only end if 0 moves available */ if (game_running) { - /* Maybe change this behaviour so if we don't move, we still generate a block */ - if (gamestate_tick(s, g, direction, g->opts->animate ? draw_then_sleep : NULL)) + gamestate_tick(s, g, direction, g->opts->animate ? draw_then_sleep : NULL); + + int spawned; + for (spawned = 0; spawned < g->opts->spawn_rate; spawned++) gamestate_new_block(g); if (gamestate_end_condition(g)) { game_running = false; - goto game_end; } } } -game_end: gfx_destroy(s); gamestate_clear(g); return 0;