Get rid of random blockgenerator in favor of a linear time one. Changed a sleep in ai to use gfx_sleep

This commit is contained in:
Tiehuis 2015-02-25 23:54:13 +13:00
parent 05f2713179
commit dc9adea7b5
3 changed files with 16 additions and 18 deletions

View File

@ -1,10 +1,11 @@
#include "ai.h" #include "ai.h"
#include "engine.h" #include "engine.h"
#include "gfx.h"
const char moves[] = {'w', 'a', 's', 'd'}; const char moves[] = {'w', 'a', 's', 'd'};
int ai_move(struct gamestate *g) int ai_move(struct gamestate *g)
{ {
if (g->opts->interactive) usleep(50000); if (g->opts->interactive) gfx_sleep(50);
return moves[rand() % 4]; return moves[rand() % 4];
} }

View File

@ -1,3 +1,4 @@
#include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include "merge.h" #include "merge.h"
@ -176,32 +177,28 @@ int gamestate_end_condition(struct gamestate *g)
void gamestate_new_block(struct gamestate *g) void gamestate_new_block(struct gamestate *g)
{ {
/* Exit early if there are no spaces to place a block */ /* 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 block_number = rand() % (g->gridsize - g->blocks_in_play);
int x, y, p = 0; int x, y, p = 0;
for (y = 0; y < g->opts->grid_height; ++y) { for (y = 0; y < g->opts->grid_height; ++y) {
for (x = 0; x < g->opts->grid_width; ++x) { for (x = 0; x < g->opts->grid_width; ++x) {
if (p == block_number) { if (!g->grid[x][y]) {
g->grid[x][y] = rand() & 1 ? 1 : 2; if (p == block_number) {
g->blocks_in_play += 1; g->grid[x][y] = rand() & 3 ? 1 : 2;
return; 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 */ /* This should never be reached; but just in case */
int x, y; assert(0);
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 returns the number of digits in the base10 rep of n. The ceiling is /* This returns the number of digits in the base10 rep of n. The ceiling is

View File

@ -13,7 +13,7 @@ void draw_then_sleep(struct gfx_state *s, struct gamestate *g)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
struct gamestate *g = gamestate_init(argc, argv); struct gamestate *g = gamestate_init(argc, argv);
struct gfx_state *s; struct gfx_state *s;
if (g->opts->interactive) if (g->opts->interactive)