From f03ba8b175a7179a12677a525963949dc6ee50ae Mon Sep 17 00:00:00 2001 From: girst Date: Wed, 20 Apr 2022 19:18:08 +0200 Subject: [PATCH] make 'no take from empty' more dry oof, that's an awful macro. --- sol.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/sol.c b/sol.c index 6d20b8d..eb382f7 100644 --- a/sol.c +++ b/sol.c @@ -1088,34 +1088,33 @@ from_l: print_table(&active, &inactive); /* prevent taking from empty tableu pile: */ if (is_tableu(*from) && f.t[*from][0] == NO_CARD) return CMD_INVAL; +/* NOTE: this macro is mis-using operator precedence with PILES: +passing in *f.f results in code equivalent to *(f.f[x])! */ +#define taking_from_empty(TYPE, PILES) ( \ + /* basic test and direct addressing: */ \ + (*from == TYPE && !(PILES[0]||PILES[1]||PILES[2]||PILES[3])) || \ + /* cursor keys addressing: */ \ + (active.pile == TYPE && !(PILES[active.opt])) || \ + /* mouse addressing: */ \ + (inactive.pile == TYPE && inactive.opt > -1 && !(PILES[inactive.opt])) \ +) + /* prevent taking from empty stock: */ #ifdef KLONDIKE if (*from == WASTE && f.w == -1) return CMD_INVAL; #elif defined FREECELL - /* basic test and direct addressing: */ - if (*from == STOCK && !(f.s[0]||f.s[1]||f.s[2]||f.s[3])) - return CMD_INVAL; - /* cursor keys addressing: */ - if (active.pile == STOCK && f.s[active.opt] == NO_CARD) - return CMD_INVAL; - /* mouse addressing: */ - if (inactive.pile == STOCK && inactive.opt > -1 && f.s[inactive.opt] == NO_CARD) + if (taking_from_empty(STOCK, f.s)) return CMD_INVAL; #endif /* prevent taking from empty foundation pile: */ #ifndef SPIDER - /* basic test and direct addressing: */ - if (*from == FOUNDATION && !(*f.f[0]||*f.f[1]||*f.f[2]||*f.f[3])) - return CMD_INVAL; - /* cursor keys addressing: */ - if (active.pile == FOUNDATION && f.f[active.opt] == NO_CARD) - return CMD_INVAL; - /* mouse addressing: */ - if (inactive.pile == FOUNDATION && inactive.opt > -1 && *f.f[inactive.opt] == NO_CARD) + if (taking_from_empty(FOUNDATION, *f.f)) return CMD_INVAL; #endif +#undef taking_from_empty /* killing this abomination before it lays eggs */ + #ifndef FREECELL if (*from == STOCK) { *to = WASTE; -- 2.39.3