Skip to content

Instantly share code, notes, and snippets.

@colezlaw
Forked from anonymous/cards.js
Last active December 17, 2015 08:18
Show Gist options
  • Save colezlaw/5578767 to your computer and use it in GitHub Desktop.
Save colezlaw/5578767 to your computer and use it in GitHub Desktop.
var cltnc = (function() {
return {
CardDeck: function(ary) {
if ("string" == typeof(ary)) {
this.ary = ary.split("");
} else {
this.ary = ary;
}
this.remaining = ary.length;
this.nextCard = function() {
if (this.remaining <= 0) {
throw new RangeError('All the cards have been dealt');
}
// Take a random card out of all
// available, then move this card to the end
var idx = Math.floor(Math.random() * this.remaining);
var ret = this.ary[idx];
this.ary[idx] = this.ary[this.remaining - 1];
this.ary[this.remaining - 1] = ret;
this.remaining--;
return ret;
};
this.randomized = function() {
var ret = [];
while (true) {
try {
ret.push(this.nextCard());
} catch(e) {
break;
}
}
return ret;
}
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment