From 0eb8c7dcf744f9d4b711dcb7319d01efab1249d4 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Thu, 4 Apr 2019 01:26:45 -0400 Subject: Add listClear --- freecell.c | 33 +++++++++++++++------------------ layout.h | 8 ++++++-- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/freecell.c b/freecell.c index da8729d..b7f6378 100644 --- a/freecell.c +++ b/freecell.c @@ -47,14 +47,14 @@ enum { static struct Stack stacks[StacksLen]; -static uint kingFace = Cards_KingRight; - static struct { bool avail; uint dst; uint src; } undo; +static uint kingFace = Cards_KingRight; + static void gameDeal(void) { for (uint i = 0; i < StacksLen; ++i) { stackClear(&stacks[i]); @@ -64,14 +64,11 @@ static void gameDeal(void) { stackPush(&deck, i); } stackShuffle(&deck); - for (uint i = Tableau1; i <= Tableau4; ++i) { - stackMoveTo(&stacks[i], &deck, 7); - } - for (uint i = Tableau5; i <= Tableau8; ++i) { - stackMoveTo(&stacks[i], &deck, 6); + for (uint i = Tableau1; i <= Tableau8; ++i) { + stackMoveTo(&stacks[i], &deck, (i < Tableau5 ? 7 : 6)); } - kingFace = Cards_KingRight; undo.avail = false; + kingFace = Cards_KingRight; } static bool gameWin(void) { @@ -81,25 +78,25 @@ static bool gameWin(void) { return true; } -static bool gameFind(uint *stack, uint *index, Card card) { +static bool gameFind(uint *stack, Card card) { for (*stack = 0; *stack < StacksLen; ++*stack) { - for (*index = 0; *index < stacks[*stack].len; ++*index) { - if (stacks[*stack].cards[*index] == card) return true; + for (uint i = 0; i < stacks[*stack].len; ++i) { + if (stacks[*stack].cards[i] == card) return true; } } return false; } static bool gameAvail(Card card) { - uint stack, index; - if (!gameFind(&stack, &index, card)) return false; - if (stack >= Foundation1 && stack <= Foundation4) return false; + uint stack; + assert(gameFind(&stack, card)); + if (stack <= Foundation4) return false; return card == stackTop(&stacks[stack]); } static bool gameMove(uint dst, Card card) { - uint src, index; - if (!gameFind(&src, &index, card)) return false; + uint src; + assert(gameFind(&src, card)); Card top = stackTop(&stacks[dst]); if (src == dst) return false; @@ -261,7 +258,7 @@ static bool keyDown(SDL_KeyboardEvent key) { static bool keyUp(SDL_KeyboardEvent key) { (void)key; if (!reveal.len) return false; - reveal.len = 0; + listClear(&reveal); return true; } @@ -279,7 +276,7 @@ static bool mouseButtonUp(SDL_MouseButtonEvent button) { if (button.button == SDL_BUTTON_RIGHT) { if (!reveal.len) return false; - reveal.len = 0; + listClear(&reveal); return true; } diff --git a/layout.h b/layout.h index a9b3917..cbff6df 100644 --- a/layout.h +++ b/layout.h @@ -38,6 +38,10 @@ struct List { struct Item items[LAYOUT_CAP]; }; +static inline void listClear(struct List *list) { + list->len = 0; +} + static inline void listPush(struct List *list, const struct SDL_Rect *rect, Card card) { assert(list->len < LAYOUT_CAP); @@ -61,8 +65,8 @@ struct Layout { }; static inline void layoutClear(struct Layout *layout) { - layout->main.len = 0; - layout->drag.len = 0; + listClear(&layout->main); + listClear(&layout->drag); } struct Style { -- cgit 1.4.1 -04 23:22:48 -0400'>2017-08-04Use $() in snapshot.shJune McEnroe 2017-08-02Move in insert direction on ~June McEnroe 2017-08-02Preserve color on replaceJune McEnroe 2017-08-02Split tile access and modifyJune McEnroe 2017-08-01Madvise RANDOM and NOSYNCJune McEnroe 2017-07-31Fix removing clients from clientCastJune McEnroe 2017-07-31Build with debug infoJune McEnroe 2017-07-31Don't dump tiles in coreJune McEnroe 2017-07-31Use designated initializers for messagesJune McEnroe 2017-07-31Show other clients' cursorsJune McEnroe 2017-07-31Perform enter as two moves rather than a loopJune McEnroe 2017-07-31Track tile access countsJune McEnroe 2017-07-31Adjust move speed in clientJune McEnroe 2017-07-31Handle large movesJune McEnroe 2017-07-30Optimize builds for chrootJune McEnroe 2017-07-30Persist bright across color changesJune McEnroe 2017-07-30Add index.htmlJune McEnroe 2017-07-30Add snapshot.shJune McEnroe 2017-07-30Fix termcap patch for background colorsJune McEnroe 2017-07-30Update helpJune McEnroe 2017-07-30Fix help to track colorJune McEnroe 2017-07-30Support background colorsJune McEnroe 2017-07-30Track color only client-sideJune McEnroe 2017-07-30Add ostensible support for background colorsJune McEnroe 2017-07-30Add tile create and access timestampsJune McEnroe 2017-07-30Assert stable struct Tile field offsetsJune McEnroe 2017-07-30Add chroot.shJune McEnroe 2017-07-30Add ` commandJune McEnroe 2017-07-30Add sshd_configJune McEnroe 2017-07-30Add termcap patchJune McEnroe.0&id=2d386e227e0b75474efcfe82e9fec7d0f21c6df4&follow=1'>t0110: Chain together using &&Jason A. Donenfeld 2015-08-12about: always ensure page has a trailing slashJason A. Donenfeld 2015-08-12filters: apply HTML escapingLazaros Koromilas 2015-08-12git: update to v2.5.0Christian Hesse 2015-08-12Fix processing of repo.hide and repo.ignoreDaniel Reichelt