about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-04-04 01:26:45 -0400
committerJune McEnroe <june@causal.agency>2019-04-04 01:26:45 -0400
commit0eb8c7dcf744f9d4b711dcb7319d01efab1249d4 (patch)
treeaed4b09b9831054d83938ef505427394beeba217
parentTweak mouseButtonUp code a bit (diff)
downloadcards-0eb8c7dcf744f9d4b711dcb7319d01efab1249d4.tar.gz
cards-0eb8c7dcf744f9d4b711dcb7319d01efab1249d4.zip
Add listClear
-rw-r--r--freecell.c33
-rw-r--r--layout.h8
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 {