Cards
Playing around with Objects & Cards
by secretgspot
JavaScript
var cards = {
suits: ['♠', '♥', '♣','♦'],
faces: ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K'],
deck: [],
generate_deck: function () {
for (i = this.suits.length; i-- > 0;) {
for (j = this.faces.length; j-- > 0;) {
this.deck.push(this.faces[j]
+ this.suits[i]);
}
}
return this.deck;
},
shuffle: function(deck) {
/*
Fisher-Yates Shuffle
http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
*/
var i = deck.length;
if ( i == 0 ) return false;
while ( --i ) {
var j = Math.floor( Math.random() * ( i + 1 ) );
var tempi = deck[i];
var tempj = deck[j];
deck[i] = tempj;
deck[j] = tempi;
}
return deck;
},
cut: function(deck){
var mid = Math.floor(Math.random()*53), top, bot;
/* split array into top & bottom */
this.deck = concat(bottom,top);
},
init: function(){
this.generate_deck();
this.shuffle(this.deck);
}
};
cards.init();
$('body').append("<pre>"+cards.deck.join("\n")+"</pre>");