Commits

Shlomi Fish  committed 456e97e

gen-ms-fc-board.js is working now.

  • Participants
  • Parent commits c57dfb8

Comments (0)

Files changed (2)

File fc-solve/site/wml/src/js-fc-solve/text/index.html.wml

 <script type="text/javascript" src="$(ROOT)/js/web-fc-solve.js"></script>
 <script type="text/javascript" src="$(ROOT)/js/joose.mini.js"></script>
 <script type="text/javascript" src="$(ROOT)/js/ms-rand.js"></script>
+<script type="text/javascript" src="$(ROOT)/js/gen-ms-fc-board.js"></script>
 </page_extra_head_elements>
 
 <h2 id="about">About</h2>

File fc-solve/site/wml/src/js/gen-ms-fc-board.js

+"use strict";
+/*
+ * Microsoft Windows Freecell / Freecell Pro boards generation.
+ *
+ * See:
+ *
+ * - http://rosettacode.org/wiki/Deal_cards_for_FreeCell
+ *
+ * - http://www.solitairelaboratory.com/mshuffle.txt
+ *
+ * Under MIT/X11 Licence.
+ *
+ * */
+Class('MSRand', {
+    has: {
+        seed: { is: rw, },
+    },
+    methods: {
+        rand: function() {
+            this.setSeed((this.getSeed() * 214013 + 2531011) & 0x7FFFFFFF);
+            return ((this.getSeed() >> 16) & 0x7fff);
+        },
+        max_rand: function(mymax) {
+            return this.rand() % mymax;
+        },
+        shuffle: function(deck) {
+            if (deck.length) {
+                var i = deck.length;
+                while (--i) {
+                    var j = this.max_rand(i+1);
+                    var tmp = deck[i];
+                    deck[i] = deck[j];
+                    deck[j] = tmp;
+                }
+            }
+            return deck;
+        },
+    },
+});
+
+function deal_ms_fc_board(seed) {
+    var randomizer = new MSRand({ seed: seed });
+    var num_cols = 8;
+
+    var _perl_range = function(start, end) {
+        var ret = [];
+
+        for (var i = start; i <= end; i++) {
+            ret.push(i);
+        }
+
+        return ret;
+    };
+
+    var columns = _perl_range(0, num_cols-1).map(function () { return []; });
+    var deck = _perl_range(0, 4*13-1);
+
+    randomizer.shuffle(deck);
+
+    deck = deck.reverse()
+
+    for (var i = 0; i < 52; i++) {
+        columns[i % num_cols].push(deck[i]);
+    }
+
+    var render_card = function (card) {
+        var suit = (card % 4);
+        var rank = Math.floor(card / 4);
+
+        return "A23456789TJQK".charAt(rank) + "CDHS".charAt(suit);
+    }
+
+    var render_column = function(col) {
+        return ": " + col.map(render_card).join(" ") + "\n";
+    }
+
+    return columns.map(render_column).join("");
+}