-
-
Save colezlaw/5578767 to your computer and use it in GitHub Desktop.
This file contains 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 characters
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