summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2019-10-26 22:25:53 -0400
committerAndrew Opalach <andrew@akon.city> 2019-10-26 22:25:53 -0400
commit06701c4619678f8f34ce9c66c20c73645c85912e (patch)
tree3032c4725470dbe65b421460716320184be7226c /lib
parente6be4377a3a078addb73983e4cde179f2acda061 (diff)
downloadcetris-06701c4619678f8f34ce9c66c20c73645c85912e.tar.gz
cetris-06701c4619678f8f34ce9c66c20c73645c85912e.tar.bz2
cetris-06701c4619678f8f34ce9c66c20c73645c85912e.zip
add piece queue and hold fix memory
Diffstat (limited to 'lib')
-rw-r--r--lib/cetris.h31
1 files changed, 16 insertions, 15 deletions
diff --git a/lib/cetris.h b/lib/cetris.h
index 152f78c..e5a7a7e 100644
--- a/lib/cetris.h
+++ b/lib/cetris.h
@@ -108,12 +108,13 @@ typedef struct {
// constant queue of all 7 possible tetrimino
uint8_t piece_queue[7];
+ uint8_t next_queue[7];
+ uint8_t current_index;
// current tetrimino
tetrimino current;
tetrimino held;
bool piece_held;
- uint8_t current_index;
// internal game tick
ctick tick;
@@ -212,18 +213,12 @@ static void set_piece(uint8_t type, tetrimino* mino) {
mino->pos.y = (type == MINO_I) ? 17 : 16;
}
-static void init_queue(cetris_game *g) {
- for (int i = 0; i < 7; i++) {
- g->piece_queue[i] = i;
- }
-}
-
static void shuffle_queue(cetris_game *g) {
for (int i = 0; i < 7; i++) {
- int rand_index = rand() % 7;
- uint8_t tmp = g->piece_queue[i];
- g->piece_queue[i] = g->piece_queue[rand_index];
- g->piece_queue[rand_index] = tmp;
+ uint8_t rand_index = rand() % 7;
+ uint8_t tmp = g->next_queue[i];
+ g->next_queue[i] = g->next_queue[rand_index];
+ g->next_queue[rand_index] = tmp;
}
}
@@ -346,13 +341,14 @@ static void next_piece(cetris_game *g) {
if (check_matrix(g, &g->current.m) <= 0) {
g->game_over = true;
}
- g->current_index++;
if (!g->game_over) {
move_current(g, DOWN);
}
-
- if (g->current_index >= 7) {
+
+ g->current_index++;
+ if (g->current_index == 7) {
+ memcpy(&g->piece_queue, &g->next_queue, sizeof(g->piece_queue));
g->current_index = 0;
shuffle_queue(g);
}
@@ -626,7 +622,12 @@ CETRIS_EXPORT void init_game(cetris_game *g) {
g->level = CETRIS_STARTING_LEVEL;
g->highest_line = CETRIS_BOARD_Y;
- init_queue(g);
+ for (int i = 0; i < 7; i++) {
+ g->next_queue[i] = i;
+ }
+
+ shuffle_queue(g);
+ memcpy(&g->piece_queue, &g->next_queue, sizeof(g->piece_queue));
shuffle_queue(g);
next_piece(g);