Browse Source

Use Card typedef

master
Causal Agent 8 months ago
parent
commit
f56895f72e
Signed by: Curtis McEnroe <june@causal.agency> GPG Key ID: CEA2F97ADCFCD77C
2 changed files with 19 additions and 17 deletions
  1. +8
    -8
      sol.c
  2. +11
    -9
      stack.h

+ 8
- 8
sol.c View File

@@ -102,7 +102,7 @@ static void gameDraw(void) {
}
}

static bool gameFind(uint *stack, uint *index, Sint8 card) {
static bool gameFind(uint *stack, uint *index, Card card) {
for (*stack = 0; *stack < StacksLen; ++*stack) {
for (*index = 0; *index < stacks[*stack].len; ++*index) {
if (stacks[*stack].cards[*index] == card) return true;
@@ -111,7 +111,7 @@ static bool gameFind(uint *stack, uint *index, Sint8 card) {
return false;
}

static bool gameAvail(Sint8 card) {
static bool gameAvail(Card card) {
uint stack, index;
if (card < 0) return false;
if (!gameFind(&stack, &index, card)) return false;
@@ -130,7 +130,7 @@ static bool gameAvail(Sint8 card) {
}
}

static bool gameReveal(Sint8 card) {
static bool gameReveal(Card card) {
uint stack, index;
if (!gameFind(&stack, &index, card)) return false;
if (stack < Tableau1 || stack > Tableau7) return false;
@@ -140,13 +140,13 @@ static bool gameReveal(Sint8 card) {
return true;
}

static bool gameMove(uint dest, Sint8 card) {
static bool gameMove(uint dest, Card card) {
uint source, index;
if (!gameFind(&source, &index, card)) return false;
if (source == dest) return false;

uint count = stacks[source].len - index;
Sint8 destTop = stackTop(&stacks[dest]);
Card destTop = stackTop(&stacks[dest]);

if (dest >= Foundation1 && dest <= Foundation4) {
if (count > 1) return false;
@@ -204,7 +204,7 @@ enum {
struct Item {
SDL_Rect rect;
uint texture;
Sint8 card;
Card card;
};

struct List {
@@ -241,7 +241,7 @@ static void layoutClear(void) {
layout.drag.len = 0;
}

static void layoutCard(struct List **list, SDL_Rect *rect, Sint8 card) {
static void layoutCard(struct List **list, SDL_Rect *rect, Card card) {
if (card == layout.dragItem.card) {
*list = &layout.drag;
*rect = layout.dragItem.rect;
@@ -300,7 +300,7 @@ static void layoutTableau(void) {
struct List *list = &layout.main;
SDL_Rect rect = base;
for (uint j = 0; j < stacks[i].len; ++j) {
Sint8 card = stacks[i].cards[j];
Card card = stacks[i].cards[j];
layoutCard(&list, &rect, card);
rect.x += TableauDeltaX;
rect.y += (card > 0 ? TableauDeltaYFront : TableauDeltaYBack);

+ 11
- 9
stack.h View File

@@ -23,7 +23,9 @@

#include "cards.h"

static Sint8 cardSuit(Sint8 card) {
typedef Sint8 Card;

static int cardSuit(Card card) {
card = abs(card);
if (card > Cards_Spade) {
return Cards_Spade;
@@ -36,34 +38,34 @@ static Sint8 cardSuit(Sint8 card) {
}
}

static Sint8 cardColor(Sint8 card) {
static int cardColor(Card card) {
return cardSuit(card) == Cards_Diamond || cardSuit(card) == Cards_Heart;
}

static Sint8 cardRank(Sint8 card) {
static int cardRank(Card card) {
return abs(card) - cardSuit(card);
}

struct Stack {
Uint8 len;
Sint8 cards[52];
Card cards[52];
};

static inline void stackClear(struct Stack *stack) {
stack->len = 0;
}

static inline void stackPush(struct Stack *stack, Sint8 card) {
static inline void stackPush(struct Stack *stack, Card card) {
assert(stack->len < 52);
stack->cards[stack->len++] = card;
}

static inline Sint8 stackPop(struct Stack *stack) {
static inline Card stackPop(struct Stack *stack) {
if (!stack->len) return 0;
return stack->cards[--stack->len];
}

static inline Sint8 stackTop(struct Stack *stack) {
static inline Card stackTop(struct Stack *stack) {
if (!stack->len) return 0;
return stack->cards[stack->len - 1];
}
@@ -85,7 +87,7 @@ static inline void stackMoveTo(struct Stack *dst, struct Stack *src, Uint8 n) {

static inline void stackDeck(struct Stack *stack) {
stackClear(stack);
for (Sint8 i = 1; i <= 52; ++i) {
for (Card i = 1; i <= 52; ++i) {
stackPush(stack, -i);
}
}
@@ -100,7 +102,7 @@ static inline int randUniform(int bound) {
static inline void stackShuffle(struct Stack *stack) {
for (Uint8 i = stack->len - 1; i > 0; --i) {
Uint8 j = randUniform(i + 1);
Sint8 x = stack->cards[i];
Card x = stack->cards[i];
stack->cards[i] = stack->cards[j];
stack->cards[j] = x;
}

Loading…
Cancel
Save