Created
August 16, 2011 13:59
Revisions
-
thefrontender revised this gist
Aug 18, 2011 . 1 changed file with 38 additions and 35 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,50 +1,53 @@ var Dealer = function (decks, suits, cards) { var pack = [], // cards yet to be dealt (stock/shoe) played = 0, // cards already dealt, suitsLen, cardsLen, i, j, // Fisher-Yates shuffle - http://jsfromhell.com/array/shuffle shuffle = function () { for(var j, x, p = pack, i = p.length; i; j = ~~(Math.random() * i), x = p[--i], p[i] = p[j], p[j] = x); }; // pack defaults decks = decks || 1; suits = suits || ['♥', '♣', '♦', '♠']; cards = cards || ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K']; suitsLen = j = suits.length; cardsLen = cards.length; // generate single deck while (j--) { for (i = 0; i < cardsLen; i++) { pack.push(suits[j] + cards[i]); } } // put the right number of decks in the pack while (--decks) { pack = pack.concat(pack.slice(0, suitsLen * cardsLen)); } // shuffle the new pack shuffle(); return { // deals from the top of the deck, defaults to 1 card deal: function (num) { num = num || 1; played += num; return pack.slice(played - num, played); }, // put the played cards back in the deck and reshuffle resetDeck: function () { played = 0; shuffle(); } }; }; -
thefrontender created this gist
Aug 16, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ var Dealer = function (deckCount, suitCount) { var suits = ['♥', '♣', '♦', '♠'], // French suits as a default pack = [], // cards yet to be dealt (stock/shoe) played = [], // cards already dealt i, j; deckCount = deckCount || 1; suitCount = suitCount || 4; // generate basic 52-card deck for (j = 0; j < suitCount; j++) { for (i = 1; i < 14; i++) { pack.push(suits[j] + i); } }; // put the right number of decks in the pack while (--deckCount) { pack = pack.concat(pack.slice(0, 52)); }; return { // Fisher-Yates shuffle - http://jsfromhell.com/array/shuffle shuffle: function () { for(var j, x, v = pack, i = v.length; i; j = ~~(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x); }, // deals from the top of the deck, defaults to 1 card deal: function (num) { var cards = pack.splice(0, num || 1); played = played.concat(cards); return cards; }, // put the played cards back in the deck, defaults to keeping the original order recycleDeck: function (reshuffle) { pack = pack.concat(played); played = []; if (reshuffle) { this.shuffle(); } } }; };