From f8588b9415d7266567c028a9da5e5a18e9179dc5 Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Fri, 27 Jun 2025 16:34:36 -0400 Subject: refactor: macros take in full state (dice) --- yhtz.c | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/yhtz.c b/yhtz.c index 43899c8..9f1ff36 100644 --- a/yhtz.c +++ b/yhtz.c @@ -42,20 +42,9 @@ typedef struct { #define SET_FIVE(s) (SET_NUM(s, 4)) #define SET_SIX(s) (SET_NUM(s, 5)) -/* DICE */ -#define GET_D(s, x) ((uint8_t)((s & (0x07 << (3 * x))) >> (3 * x))) -#define GET_D0(s) (GET_D(s, 0)) -#define GET_D1(s) (GET_D(s, 1)) -#define GET_D2(s) (GET_D(s, 2)) -#define GET_D3(s) (GET_D(s, 3)) -#define GET_D4(s) (GET_D(s, 4)) - -#define SET_D(s, x, y) (s & (0xFFFF ^ (0x0007 << (3 * x))) | (y << (3 * x))) -#define SET_D0(s, y) (SET_D(s, 0, y)) -#define SET_D1(s, y) (SET_D(s, 1, y)) -#define SET_D2(s, y) (SET_D(s, 2, y)) -#define SET_D3(s, y) (SET_D(s, 3, y)) -#define SET_D4(s, y) (SET_D(s, 4, y)) +/* DICE -- x is die number (0-4), y is value (1-6) */ +#define GET_DIE(s, x) ((uint8_t)((s->dice & (0x07 << (3 * x))) >> (3 * x))) +#define SET_DIE(s, x, y) (s->dice = (s->dice & (0xFFFF ^ (0x0007 << (3 * x))) | (y << (3 * x)))) /* 16-bit prng, with thanks/apologies to Dr. Lemire (https://lemire.me/blog/2019/07/03/a-fast-16-bit-random-number-generator/) */ uint32_t hash16(uint32_t input, uint32_t key) { @@ -94,7 +83,7 @@ void print_dice(yhtz_state* state) { printf(label, i); printf("\n%s %s %s %s %s\n", top, top, top, top, top); for (int i = 0; i < 5; i++) - printf(mid, GET_D(state->dice, i)); + printf(mid, GET_DIE(state, i)); printf("\n%s %s %s %s %s\n", bot, bot, bot, bot, bot); } @@ -109,7 +98,7 @@ void ask_for_rerolls(yhtz_state* state) { for (int i = 0; i < 5; i++) { if (dice[i] == -1) break; - state->dice = SET_D(state->dice, dice[i], 0); + SET_DIE(state, dice[i], 0); } } @@ -124,8 +113,8 @@ void do_turn(yhtz_state* state) { } for (int i = 0; i < 5; i++) { - if (reset || (GET_D(state->dice, i) == 0)) { - state->dice = SET_D(state->dice, i, roll_die(&(state->seed))); + if (reset || (GET_DIE(state, i) == 0)) { + SET_DIE(state, i, roll_die(&(state->seed))); } } SET_ROLL(state, ++roll); @@ -157,20 +146,11 @@ void main() { do_turn(&y); printf("new roll: %d\n", GET_ROLL((&y))); - printf("new d1: %d\n", GET_D0(y.dice)); - printf("new d2: %d\n", GET_D1(y.dice)); - printf("new d3: %d\n", GET_D2(y.dice)); - printf("new d4: %d\n", GET_D3(y.dice)); - printf("new d5: %d\n", GET_D4(y.dice)); print_dice(&y); ask_for_rerolls(&y); do_turn(&y); printf("newer roll: %d\n", GET_ROLL((&y))); - printf("newer d1: %d\n", GET_D0(y.dice)); - printf("newer d2: %d\n", GET_D1(y.dice)); - printf("newer d3: %d\n", GET_D2(y.dice)); - printf("newer d4: %d\n", GET_D3(y.dice)); - printf("newer d5: %d\n", GET_D4(y.dice)); + print_dice(&y); } -- cgit v1.2.3