summary refs log tree commit diff
path: root/freecell.c
diff options
context:
space:
mode:
Diffstat (limited to 'freecell.c')
-rw-r--r--freecell.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/freecell.c b/freecell.c
index 3400306..0e74793 100644
--- a/freecell.c
+++ b/freecell.c
@@ -89,16 +89,34 @@ static bool undo(void) {
 	return true;
 }
 
+static uint lcgState = 1;
+static uint lcgRand(void) {
+	lcgState = (214013 * lcgState + 2531011) % (1 << 31);
+	return lcgState / (1 << 16);
+}
+
+// <https://rosettacode.org/wiki/Deal_cards_for_FreeCell>
 static void deal(void) {
+	kingIndex = Cards_KingRight;
 	for (uint i = 0; i < StacksLen; ++i) {
 		clear(&stacks[i]);
 	}
-	struct Stack deck = Deck;
-	shuffle(&deck);
-	for (uint i = Tableau1; i <= Tableau8; ++i) {
-		flipTo(&stacks[i], &deck, (i < Tableau5 ? 7 : 6));
+
+	struct Stack deck = {0};
+	for (Card i = Cards_A; i <= Cards_K; ++i) {
+		push(&deck, Cards_Club + i);
+		push(&deck, Cards_Diamond + i);
+		push(&deck, Cards_Heart + i);
+		push(&deck, Cards_Spade + i);
+	}
+
+	uint stack = 0;
+	while (deck.len) {
+		uint i = lcgRand() % deck.len;
+		Card card = deck.cards[i];
+		deck.cards[i] = deck.cards[--deck.len];
+		push(&stacks[Tableau1 + stack++ % 8], card);
 	}
-	kingIndex = Cards_KingRight;
 }
 
 static bool win(void) {