diff options
author | June McEnroe <june@causal.agency> | 2019-08-26 13:22:40 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2019-08-26 13:22:40 -0400 |
commit | ba30ddcf633f28185f8d92275c53fa5764f3d8c1 (patch) | |
tree | 826549b46b26bf5e6b71db8016c351973a2e65fc | |
parent | Clean up naming (diff) | |
download | wep-ba30ddcf633f28185f8d92275c53fa5764f3d8c1.tar.gz wep-ba30ddcf633f28185f8d92275c53fa5764f3d8c1.zip |
Simplify card and stack function names
Diffstat (limited to '')
-rw-r--r-- | freecell.c | 48 | ||||
-rw-r--r-- | sol.c | 44 | ||||
-rw-r--r-- | stack.h | 28 |
3 files changed, 59 insertions, 61 deletions
diff --git a/freecell.c b/freecell.c index eae8413..62cc81a 100644 --- a/freecell.c +++ b/freecell.c @@ -70,7 +70,7 @@ static void enqueue(uint dst, uint src) { static void dequeue(void) { struct Move move = queue.moves[queue.r++ % QueueLen]; - stackPush(&stacks[move.dst], stackPop(&stacks[move.src])); + push(&stacks[move.dst], pop(&stacks[move.src])); if (move.dst <= Foundation4) { kingIndex = Cards_KingRight; } else if (move.dst <= Cell4) { @@ -83,7 +83,7 @@ static bool undo(void) { if (!len || len > QueueLen) return false; for (uint i = len - 1; i < len; --i) { struct Move move = queue.moves[(queue.u + i) % QueueLen]; - stackPush(&stacks[move.src], stackPop(&stacks[move.dst])); + push(&stacks[move.src], pop(&stacks[move.dst])); } queue.r = queue.w = queue.u; return true; @@ -91,12 +91,12 @@ static bool undo(void) { static void deal(void) { for (uint i = 0; i < StacksLen; ++i) { - stackClear(&stacks[i]); + clear(&stacks[i]); } struct Stack deck = Deck; - stackShuffle(&deck); + shuffle(&deck); for (uint i = Tableau1; i <= Tableau8; ++i) { - stackFlipTo(&stacks[i], &deck, (i < Tableau5 ? 7 : 6)); + flipTo(&stacks[i], &deck, (i < Tableau5 ? 7 : 6)); } kingIndex = Cards_KingRight; } @@ -109,16 +109,16 @@ static bool win(void) { } static bool valid(uint dst, Card card) { - Card top = stackTop(&stacks[dst]); - if (dst >= Foundation1 && dst <= Foundation4) { - if (!top) return cardRank(card) == Cards_A; - return cardSuit(card) == cardSuit(top) - && cardRank(card) == cardRank(top) + 1; + Card top = peek(&stacks[dst]); + if (dst <= Foundation4) { + if (!top) return rank(card) == Cards_A; + return suit(card) == suit(top) + && rank(card) == rank(top) + 1; } if (!top) return true; - if (dst >= Tableau1 && dst <= Tableau8) { - return cardColor(card) != cardColor(top) - && cardRank(card) == cardRank(top) - 1; + if (dst >= Tableau1) { + return color(card) != color(top) + && rank(card) == rank(top) - 1; } return false; } @@ -128,17 +128,17 @@ static void autoEnqueue(void) { for (uint i = Cell1; i <= Tableau8; ++i) { for (uint j = 0; j < stacks[i].len; ++j) { Card card = stacks[i].cards[j]; - if (cardRank(card) < min[cardColor(card)]) { - min[cardColor(card)] = cardRank(card); + if (rank(card) < min[color(card)]) { + min[color(card)] = rank(card); } } } for (uint src = Cell1; src <= Tableau8; ++src) { - Card card = stackTop(&stacks[src]); + Card card = peek(&stacks[src]); if (!card) continue; - if (cardRank(card) > Cards_2) { - if (min[!cardColor(card)] < cardRank(card)) continue; + if (rank(card) > Cards_2) { + if (min[!color(card)] < rank(card)) continue; } for (uint dst = Foundation1; dst <= Foundation4; ++dst) { if (valid(dst, card)) { @@ -150,7 +150,7 @@ static void autoEnqueue(void) { } static void moveSingle(uint dst, uint src) { - if (valid(dst, stackTop(&stacks[src]))) { + if (valid(dst, peek(&stacks[src]))) { queue.u = queue.w; enqueue(dst, src); } @@ -161,8 +161,8 @@ static uint moveDepth(uint src) { if (stack.len < 2) return stack.len; uint n = 1; for (uint i = stack.len - 2; i < stack.len; --i, ++n) { - if (cardColor(stack.cards[i]) == cardColor(stack.cards[i + 1])) break; - if (cardRank(stack.cards[i]) != cardRank(stack.cards[i + 1]) + 1) break; + if (color(stack.cards[i]) == color(stack.cards[i + 1])) break; + if (rank(stack.cards[i]) != rank(stack.cards[i + 1]) + 1) break; } return n; } @@ -475,7 +475,7 @@ static void renderStack(uint stack) { revealRect = rect; } bool hilite = (stack == fromStack && i == stacks[stack].len - 1); - renderCard(rect, card, hilite || cardRank(card) == hiliteRank); + renderCard(rect, card, hilite || rank(card) == hiliteRank); rect.y += StackDeltaY; } if (revealCard) { @@ -485,9 +485,9 @@ static void renderStack(uint stack) { static void renderStacks(void) { for (uint i = Foundation1; i <= Cell4; ++i) { - Card card = stackTop(&stacks[i]); + Card card = peek(&stacks[i]); if (!card) continue; - bool hilite = (i == fromStack || cardRank(card) == hiliteRank); + bool hilite = (i == fromStack || rank(card) == hiliteRank); renderCard(rects[i], card, hilite); } for (uint i = Tableau1; i <= Tableau8; ++i) { diff --git a/sol.c b/sol.c index f16c7b6..5622395 100644 --- a/sol.c +++ b/sol.c @@ -82,28 +82,26 @@ static bool gameUndo(void) { static void gameDeal(void) { undo.fn = NULL; for (uint i = 0; i < StacksLen; ++i) { - stackClear(&stacks[i]); + clear(&stacks[i]); } - for (Card i = 1; i <= 52; ++i) { - stackPush(&stacks[Stock], -i); - } - stackShuffle(&stacks[Stock]); + stacks[Stock] = Deck; + shuffle(&stacks[Stock]); for (uint i = Tableau1; i <= Tableau7; ++i) { - stackMoveTo(&stacks[i], &stacks[Stock], i - Tableau1); - stackFlipTo(&stacks[i], &stacks[Stock], 1); + moveTo(&stacks[i], &stacks[Stock], i - Tableau1); + flipTo(&stacks[i], &stacks[Stock], 1); } } static void gameDraw(void) { - stackMoveTo(&stacks[Waste1], &stacks[Waste3], stacks[Waste3].len); + moveTo(&stacks[Waste1], &stacks[Waste3], stacks[Waste3].len); if (stacks[Stock].len) { if (game.draw > 1) { - gameDo(stackFlipTo, Waste3, Stock, game.draw); + gameDo(flipTo, Waste3, Stock, game.draw); } else { - gameDo(stackFlipTo, Waste1, Stock, 1); + gameDo(flipTo, Waste1, Stock, 1); } } else { - gameDo(stackFlipTo, Stock, Waste1, stacks[Waste1].len); + gameDo(flipTo, Stock, Waste1, stacks[Waste1].len); } } @@ -121,7 +119,7 @@ static bool gameAvail(Card card) { if (card < 0) return false; if (!gameFind(&stack, &index, card)) return false; - bool top = (card == stackTop(&stacks[stack])); + bool top = (card == peek(&stacks[stack])); if (stack == Waste1) { return top && !stacks[Waste3].len; } else if (stack == Waste3) { @@ -139,9 +137,9 @@ static bool gameReveal(Card card) { uint stack, index; if (!gameFind(&stack, &index, card)) return false; if (stack < Tableau1 || stack > Tableau7) return false; - if (card != stackTop(&stacks[stack])) return false; + if (card != peek(&stacks[stack])) return false; if (card > 0) return false; - gameDo(stackFlipTo, stack, stack, 1); + gameDo(flipTo, stack, stack, 1); return true; } @@ -151,22 +149,22 @@ static bool gameMove(uint dest, Card card) { if (source == dest) return false; uint count = stacks[source].len - index; - Card destTop = stackTop(&stacks[dest]); + Card destTop = peek(&stacks[dest]); if (dest >= Foundation1 && dest <= Foundation4) { if (count > 1) return false; - if (!destTop && cardRank(card) != Cards_A) return false; - if (destTop && cardSuit(card) != cardSuit(destTop)) return false; - if (destTop && cardRank(card) != cardRank(destTop) + 1) return false; - gameDo(stackMoveTo, dest, source, 1); + if (!destTop && rank(card) != Cards_A) return false; + if (destTop && suit(card) != suit(destTop)) return false; + if (destTop && rank(card) != rank(destTop) + 1) return false; + gameDo(moveTo, dest, source, 1); return true; } if (dest >= Tableau1 && dest <= Tableau7) { - if (!destTop && cardRank(card) != Cards_K) return false; - if (destTop && cardColor(card) == cardColor(destTop)) return false; - if (destTop && cardRank(card) != cardRank(destTop) - 1) return false; - gameDo(stackMoveTo, dest, source, count); + if (!destTop && rank(card) != Cards_K) return false; + if (destTop && color(card) == color(destTop)) return false; + if (destTop && rank(card) != rank(destTop) - 1) return false; + gameDo(moveTo, dest, source, count); return true; } diff --git a/stack.h b/stack.h index 3d3a43b..c315729 100644 --- a/stack.h +++ b/stack.h @@ -33,7 +33,7 @@ typedef Sint8 Card; -static inline int cardSuit(Card card) { +static inline int suit(Card card) { card = abs(card); if (card > Cards_Spade) { return Cards_Spade; @@ -46,12 +46,12 @@ static inline int cardSuit(Card card) { } } -static inline int cardColor(Card card) { - return cardSuit(card) == Cards_Diamond || cardSuit(card) == Cards_Heart; +static inline int color(Card card) { + return suit(card) == Cards_Diamond || suit(card) == Cards_Heart; } -static inline int cardRank(Card card) { - return abs(card) - cardSuit(card); +static inline int rank(Card card) { + return abs(card) - suit(card); } struct Stack { @@ -69,36 +69,36 @@ static const struct Stack Deck = { }, }; -static inline void stackClear(struct Stack *stack) { +static inline void clear(struct Stack *stack) { stack->len = 0; } -static inline void stackPush(struct Stack *stack, Card card) { +static inline void push(struct Stack *stack, Card card) { assert(stack->len < STACK_CAP); stack->cards[stack->len++] = card; } -static inline Card stackPop(struct Stack *stack) { +static inline Card pop(struct Stack *stack) { if (!stack->len) return 0; return stack->cards[--stack->len]; } -static inline Card stackTop(const struct Stack *stack) { +static inline Card peek(const struct Stack *stack) { if (!stack->len) return 0; return stack->cards[stack->len - 1]; } -static inline void stackFlipTo(struct Stack *dst, struct Stack *src, Uint8 n) { +static inline void flipTo(struct Stack *dst, struct Stack *src, Uint8 n) { if (n > src->len) n = src->len; for (Uint8 i = 0; i < n; ++i) { - stackPush(dst, -stackPop(src)); + push(dst, -pop(src)); } } -static inline void stackMoveTo(struct Stack *dst, struct Stack *src, Uint8 n) { +static inline void moveTo(struct Stack *dst, struct Stack *src, Uint8 n) { if (n > src->len) n = src->len; for (Uint8 i = 0; i < n; ++i) { - stackPush(dst, src->cards[src->len - n + i]); + push(dst, src->cards[src->len - n + i]); } src->len -= n; } @@ -110,7 +110,7 @@ static inline int randUniform(int bound) { } } -static inline void stackShuffle(struct Stack *stack) { +static inline void shuffle(struct Stack *stack) { for (Uint8 i = stack->len - 1; i > 0; --i) { Uint8 j = randUniform(i + 1); Card x = stack->cards[i]; |