about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-03-24 14:36:16 -0400
committerJune McEnroe <june@causal.agency>2019-03-24 14:36:16 -0400
commitf56895f72e27bd1830a54a3ffbd4b5b24e95cec0 (patch)
treea823ab695e7bb7af16debe289c8d654cb7eac460
parentChoose card back randomly (diff)
downloadcards-f56895f72e27bd1830a54a3ffbd4b5b24e95cec0.tar.gz
cards-f56895f72e27bd1830a54a3ffbd4b5b24e95cec0.zip
Use Card typedef
-rw-r--r--sol.c16
-rw-r--r--stack.h20
2 files changed, 19 insertions, 17 deletions
diff --git a/sol.c b/sol.c
index 724aefd..c0882ac 100644
--- a/sol.c
+++ b/sol.c
@@ -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);
diff --git a/stack.h b/stack.h
index 4daf6ba..6267f23 100644
--- a/stack.h
+++ b/stack.h
@@ -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;
 	}