Commits

David Chambers  committed 27ff364

Updated Klondike.

  • Participants
  • Parent commits 998329f

Comments (0)

Files changed (2)

File klondike/klondike.coffee

   div.innerHTML = html
   div.firstChild
 
-chain = (arg) =>
+chain = (arg) ->
   if typeof arg is 'function'
     queue.push arg
   else

File klondike/klondike.js

 (function() {
+
   /*
-
+  
   Klondike
-
+  
   Crafted by an enthusiastic young web developer
   seeking a deeper understanding of JavaScript.
+  
+  https://bitbucket.org/davidchambers/klondike
+  */
 
-  https://bitbucket.org/davidchambers/klondike
+  var $, Card, Stack, body, card, cards, chain, columnOffset, data, dealing, deck, dropTargets, el, foundations, game, i, ii, index, j, jj, len, ol, pile, piles, queue, r, random, rank, ranks, s, store, suit, suits, tableau, waste, _i, _j, _len, _len2, _len3, _len4, _len5;
 
-  */  var $, Card, Stack, body, card, cards, chain, columnOffset, data, dealing, deck, dropTargets, el, foundations, game, i, ii, index, j, jj, len, ol, pile, piles, queue, r, random, rank, ranks, s, store, suit, suits, tableau, waste, _i, _j, _len, _len2, _len3, _len4, _len5;
-  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
   $ = function(html) {
     var div;
     div = document.createElement('div');
     div.innerHTML = html;
     return div.firstChild;
   };
-  chain = __bind(function(arg) {
+
+  chain = function(arg) {
     var delay, fn, ms, _base, _results;
     if (typeof arg === 'function') {
       return queue.push(arg);
     } else {
-      if (typeof (_base = queue.shift()) === "function") {
-        _base();
-      }
+      if (typeof (_base = queue.shift()) === "function") _base();
       if (ms = delay = arg) {
         _results = [];
         while (fn = queue.shift()) {
         return _results;
       }
     }
-  }, this);
+  };
+
   queue = [];
+
   data = function(el, card) {
     if (card != null) {
       return store[el.className] = card;
       return store[el.className];
     }
   };
+
   store = {};
+
   Card = (function() {
+
     function Card(rank, rankValue, suit, suitValue) {
       this.rank = rank;
       this.rankValue = rankValue;
       this.el = $("<li class='" + rank + " of " + suit + "' rel='face-down'></li>");
       data(this.el, this);
     }
+
     Card.prototype.inTableau = function(parent) {
       while (parent) {
-        if (parent === tableau) {
-          return true;
-        }
+        if (parent === tableau) return true;
         parent = parent.parentNode;
       }
       return false;
     };
+
     Card.prototype.isDraggable = function() {
       var parent;
       parent = this.el.parentNode;
       return !dealing && (parent === waste && this.el === parent.lastChild || this.isFaceUp() && this.inTableau(parent));
     };
+
     Card.prototype.isFaceUp = function() {
       return this.el.getAttribute('rel') === 'face-up';
     };
+
     Card.prototype.isFaceDown = function() {
       return this.el.getAttribute('rel') === 'face-down';
     };
+
     Card.prototype.flipFaceUp = function() {
       this.el.setAttribute('rel', 'face-up');
       return this.el.className = this.el.className;
     };
+
     Card.prototype.flipFaceDown = function() {
       this.el.setAttribute('rel', 'face-down');
       return this.el.className = this.el.className;
     };
+
     return Card;
+
   })();
+
   Stack = (function() {
+
     Stack.prototype.n = 20;
+
     function Stack(el) {
       var e, _i, _len, _ref;
       this.x = 0;
       }
       body.appendChild(this.el);
     }
+
     Stack.prototype.move = function(x, y) {
       this.el.style.left = (this.x += x) + 'px';
       return this.el.style.top = (this.y += y) + 'px';
     };
+
     Stack.prototype.morph = function(fromX, fromY, toX, toY, callback, n) {
       var x, y;
-      n != null ? n : n = this.n;
+      var _this = this;
+      if (n == null) n = this.n;
       fromX += (x = Math.round((toX - fromX) / n));
       fromY += (y = Math.round((toY - fromY) / n));
       this.el.style.left = fromX + 'px';
       this.el.style.top = fromY + 'px';
       if (--n) {
-        return window.setTimeout(__bind(function() {
-          return this.morph(fromX, fromY, toX, toY, callback, n);
-        }, this), Math.sqrt(x * x + y * y));
+        return window.setTimeout(function() {
+          return _this.morph(fromX, fromY, toX, toY, callback, n);
+        }, Math.sqrt(x * x + y * y));
       } else {
         return typeof callback === "function" ? callback() : void 0;
       }
     };
+
     Stack.prototype.slideInto = function(el, callback) {
       var dummy, e, toX, toY;
+      var _this = this;
       dummy = $('<li class="of" style="visibility:hidden"></li>');
       el.appendChild(dummy);
       toX = toY = 0;
         e = e.offsetParent;
       }
       el.removeChild(dummy);
-      return this.morph(this.x, this.y, toX, toY, __bind(function() {
+      return this.morph(this.x, this.y, toX, toY, function() {
         var card, _i, _len, _ref;
-        _ref = this.stack;
+        _ref = _this.stack;
         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
           card = _ref[_i];
           el.appendChild(card);
         }
-        body.removeChild(this.el);
+        body.removeChild(_this.el);
         return typeof callback === "function" ? callback() : void 0;
-      }, this));
+      });
     };
+
     return Stack;
+
   })();
+
   body = document.body;
+
   dropTargets = [];
+
   piles = new Array(7);
+
   ranks = 'ace deuce trey four five six seven eight nine ten jack queen king'.split(' ');
+
   suits = 'spades hearts clubs diamonds'.split(' ');
+
   foundations = $('<ul id="foundations"></ul>');
+
   for (_i = 0, _len = suits.length; _i < _len; _i++) {
     suit = suits[_i];
     el = $("<li><ol id='" + suit + "'></ol></li>");
     foundations.appendChild(el);
     dropTargets.push(el.firstChild);
   }
+
   window.onmousedown = function(event) {
     var card, parent, stack, target, x, y;
     event || (event = window.event);
-    if (event.button === 2) {
-      return;
-    }
+    if (event.button === 2) return;
     target = event.target || event.srcElement;
     parent = target.parentNode;
     card = data(target);
         return y = pageY;
       };
       return target.onmouseup = function(event) {
-        var canGoToFoundation, droppable, dummy, el, foundation, h, max, maxX, maxY, min, minX, minY, style, valid, w, _i, _len, _ref, _ref2;
+        var canGoToFoundation, droppable, dummy, el, foundation, h, max, maxX, maxY, min, minX, minY, style, valid, w, _j, _len2, _ref, _ref2;
         window.onmousemove = null;
         target.onmouseup = null;
         foundation = document.getElementById(card.suit);
         w = parseInt(style.getPropertyValue('width'), 10);
         h = parseInt(style.getPropertyValue('height'), 10);
         body.removeChild(dummy);
-        for (_i = 0, _len = dropTargets.length; _i < _len; _i++) {
-          el = dropTargets[_i];
+        for (_j = 0, _len2 = dropTargets.length; _j < _len2; _j++) {
+          el = dropTargets[_j];
           min = el.firstChild || el;
           minX = minY = 0;
           while (min) {
                 return true;
               }
             } else {
-              if (card.rank === 'king') {
-                return true;
-              }
+              if (card.rank === 'king') return true;
             }
           }
           return false;
       };
     }
   };
+
   tableau = $('<ul id="tableau"></ul>');
+
   for (index = 0, _len2 = piles.length; index < _len2; index++) {
     pile = piles[index];
     el = $("<li><ol id='pile:" + index + "'></ol></li>");
     dropTargets.push(ol);
     tableau.appendChild(el);
   }
+
   cards = [];
+
   for (s = 0, _len3 = suits.length; s < _len3; s++) {
     suit = suits[s];
     for (r = 0, _len4 = ranks.length; r < _len4; r++) {
       cards.push(new Card(rank, r, suit, s));
     }
   }
+
   random = Math.random;
+
   i = cards.length;
+
   while (i) {
     j = random() * i-- >> 0;
     ii = cards[i];
     cards[i] = jj;
     cards[j] = ii;
   }
+
   deck = $('<ol id="deck"></ol>');
+
   for (_j = 0, _len5 = cards.length; _j < _len5; _j++) {
     card = cards[_j];
     deck.appendChild(card.el);
   }
+
   deck.onclick = function() {
-    var card, dealing, el, els, i, _fn, _i, _j, _len, _len2, _results;
+    var card, dealing, el, els, i, _fn, _k, _l, _len6, _len7, _results;
     if (this.childNodes.length) {
       dealing = true;
       els = [];
       el = this.lastChild;
       while (el) {
         els.push(el);
-        if (els.length === 3) {
-          break;
-        }
+        if (els.length === 3) break;
         el = el.previousSibling;
       }
       _fn = function(el) {
           });
         });
       };
-      for (_i = 0, _len = els.length; _i < _len; _i++) {
-        el = els[_i];
+      for (_k = 0, _len6 = els.length; _k < _len6; _k++) {
+        el = els[_k];
         _fn(el);
       }
       chain(function() {
             (function() {
               var _ref, _results;
               _results = [];
-              for (i = 0, _ref = object.length; (0 <= _ref ? i < _ref : i > _ref); (0 <= _ref ? i += 1 : i -= 1)) {
+              for (i = 0, _ref = object.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
                 _results.push(object[i]);
               }
               return _results;
         }
       })();
       _results = [];
-      for (_j = 0, _len2 = cards.length; _j < _len2; _j++) {
-        card = cards[_j];
+      for (_l = 0, _len7 = cards.length; _l < _len7; _l++) {
+        card = cards[_l];
         data(card).flipFaceDown();
         _results.push(this.appendChild(card));
       }
       return _results;
     }
   };
+
   waste = $('<ol id="waste"></ol>');
+
   game = $('<div id="game"></div>');
+
   game.appendChild(deck);
+
   game.appendChild(waste);
+
   game.appendChild(foundations);
+
   game.appendChild(tableau);
+
   body.appendChild(game);
+
   dealing = true;
+
   i = columnOffset = 0;
+
   len = piles.length;
+
   while (columnOffset < len) {
     (function(i, columnOffset) {
       return chain(function() {
         el = deck.lastChild;
-        if (i === columnOffset) {
-          data(el).flipFaceUp();
-        }
+        if (i === columnOffset) data(el).flipFaceUp();
         return new Stack(el).slideInto(piles[i]);
       });
     })(i, columnOffset);
-    if (++i === len) {
-      i = ++columnOffset;
-    }
+    if (++i === len) i = ++columnOffset;
   }
+
   chain(function() {
     return dealing = false;
   });
+
   chain(100);
+
   document.onselectstart = function() {
     return false;
   };
+
 }).call(this);