summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-08-26 13:32:23 -0400
committerJune McEnroe <june@causal.agency>2019-08-26 13:32:23 -0400
commit3bc4ec7c997a238206d206ca6669d34cdaff7f47 (patch)
tree8f2467ff410c6fdc137323b31df686151b51a568
parentSimplify card and stack function names (diff)
downloadwep-3bc4ec7c997a238206d206ca6669d34cdaff7f47.tar.gz
wep-3bc4ec7c997a238206d206ca6669d34cdaff7f47.zip
Move as deep a stack as possible to empty columns
-rw-r--r--freecell.c16
1 files changed, 8 insertions, 8 deletions
diff --git a/freecell.c b/freecell.c
index 62cc81a..3400306 100644
--- a/freecell.c
+++ b/freecell.c
@@ -167,18 +167,22 @@ static uint moveDepth(uint src) {
 	return n;
 }
 
-static uint freeCells(uint list[], uint dst) {
+static uint freeCells(uint cells[], uint dst) {
 	uint len = 0;
 	for (uint i = Cell1; i <= Tableau8; ++i) {
 		if (i == dst) continue;
-		if (!stacks[i].len) list[len++] = i;
+		if (!stacks[i].len) cells[len++] = i;
 	}
 	return len;
 }
 
 static void moveColumn(uint dst, uint src) {
+	uint cells[StacksLen];
+	uint free = freeCells(cells, dst);
+
 	uint depth;
 	for (depth = moveDepth(src); depth; --depth) {
+		if (free < depth - 1) continue;
 		if (valid(dst, stacks[src].cards[stacks[src].len - depth])) break;
 	}
 	if (depth < 2 || dst <= Cell4) {
@@ -186,17 +190,13 @@ static void moveColumn(uint dst, uint src) {
 		return;
 	}
 
-	uint list[StacksLen];
-	uint free = freeCells(list, dst);
-	if (free < depth - 1) return;
-
 	queue.u = queue.w;
 	for (uint i = 0; i < depth - 1; ++i) {
-		enqueue(list[i], src);
+		enqueue(cells[i], src);
 	}
 	enqueue(dst, src);
 	for (uint i = depth - 2; i < depth - 1; --i) {
-		enqueue(dst, list[i]);
+		enqueue(dst, cells[i]);
 	}
 }