Skip to content

Instantly share code, notes, and snippets.

@thefrontender
Created August 16, 2011 13:59

Revisions

  1. thefrontender revised this gist Aug 18, 2011. 1 changed file with 38 additions and 35 deletions.
    73 changes: 38 additions & 35 deletions dealer.js
    Original file line number Diff line number Diff line change
    @@ -1,50 +1,53 @@
    var Dealer = function (decks, suits, cards) {

    var Dealer = function (deckCount, suitCount) {

    var suits = ['♥', '♣', '♦', '♠'], // French suits as a default
    pack = [], // cards yet to be dealt (stock/shoe)
    played = [], // cards already dealt
    var pack = [], // cards yet to be dealt (stock/shoe)
    played = 0, // cards already dealt,
    suitsLen,
    cardsLen,
    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);
    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 (--deckCount) {
    pack = pack.concat(pack.slice(0, 52));
    };
    while (--decks) {
    pack = pack.concat(pack.slice(0, suitsLen * cardsLen));
    }

    return {
    // shuffle the new pack
    shuffle();

    // 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);
    },
    return {

    // 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;
    num = num || 1;
    played += num;
    return pack.slice(played - num, played);
    },

    // 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();
    }
    // put the played cards back in the deck and reshuffle
    resetDeck: function () {
    played = 0;
    shuffle();
    }
    };
    };
  2. thefrontender created this gist Aug 16, 2011.
    50 changes: 50 additions & 0 deletions dealer.js
    Original 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();
    }
    }
    };
    };