@@ 262,24 262,31 @@ struct Game {
enum GameState state;
int score;
+ struct drand48_data prng_state;
+
// player input
int active_piece;
int active_row;
int active_column;
};
-struct Piece random_piece() { return PIECES[random() % NPIECES]; }
+struct Piece random_piece(struct Game *g) {
+ long result;
+ lrand48_r(&g->prng_state, &result);
+
+ return PIECES[result % NPIECES];
+}
void fill_table(struct Game *g) {
- g->table[0] = random_piece();
+ g->table[0] = random_piece(g);
// No duplicate pieces
do {
- g->table[1] = random_piece();
+ g->table[1] = random_piece(g);
} while (g->table[0].field == g->table[1].field);
do {
- g->table[2] = random_piece();
+ g->table[2] = random_piece(g);
} while (g->table[0].field == g->table[2].field ||
g->table[1].field == g->table[2].field);
}
@@ 555,10 562,6 @@ void handle_quit(int score) {
}
void game_main() {
- long seed;
- time(&seed);
- srand(seed);
-
char header[] = " _ _ \n"
" ___ __ _ _ _ ___| | |\n"
" / __|/ _` | | | |/ _ \\ | |\n"
@@ 587,6 590,12 @@ void game_main() {
struct Game game = {0};
+ {
+ long seed;
+ time(&seed);
+ srand48_r(seed, &game.prng_state);
+ }
+
game.state = CHOOSING_PIECE;
fill_table(&game);