From 8ad9ab01e2dcf7e77387e41ee2df11e1d87429ec Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Fri, 27 Jun 2025 16:32:11 -0400 Subject: refactor: macros take in full state (rolls) --- yhtz.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/yhtz.c b/yhtz.c index cf556db..43899c8 100644 --- a/yhtz.c +++ b/yhtz.c @@ -20,9 +20,10 @@ typedef struct { uint16_t seed; } yhtz_state; +/* Note: all macros take in a yhtz_state* as input 's' */ /* ROLLS */ -#define GET_ROLL(s) ((uint8_t)((s & 0xC0) >> 6)) /* top 2 bits */ -#define SET_ROLL(s, x) (s & 0x3F | ((uint8_t)x << 6)) +#define GET_ROLL(s) ((uint8_t)((s->topflag_w_roll & 0xC0) >> 6)) /* top 2 bits */ +#define SET_ROLL(s, x) (s->topflag_w_roll = (s->topflag_w_roll & 0x3F | ((uint8_t)x << 6))) /* TOP NUMBERS */ #define GET_NUM(s, x) ((uint8_t)((s & (1 << x)) >> x)) @@ -113,7 +114,7 @@ void ask_for_rerolls(yhtz_state* state) { } void do_turn(yhtz_state* state) { - int roll = GET_ROLL(state->topflag_w_roll); + int roll = GET_ROLL(state); bool reset = false; // if we're out of rolls, reset @@ -127,7 +128,7 @@ void do_turn(yhtz_state* state) { state->dice = SET_D(state->dice, i, roll_die(&(state->seed))); } } - state->topflag_w_roll = SET_ROLL(state->topflag_w_roll, ++roll); + SET_ROLL(state, ++roll); } void init_yhtz_state(yhtz_state* state) { @@ -155,7 +156,7 @@ void main() { do_turn(&y); - printf("new roll: %d\n", GET_ROLL(y.topflag_w_roll)); + 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)); @@ -166,7 +167,7 @@ void main() { ask_for_rerolls(&y); do_turn(&y); - printf("newer roll: %d\n", GET_ROLL(y.topflag_w_roll)); + 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)); -- cgit v1.2.3