Video Poker in JavaScript
An early JavaScript coding exercise turned into a not-too-awful video poker game.
When I started learning JavaScript my first project (aside from tutorials) was to build a video poker game. I decided to dust it off, fix a couple of things, and post it here for kicks.
The JavaScript is not exactly well-written or efficient, but as far as I can tell it works accurately. For the curious, it utilizes a Fisher-Yates shuffle and array comparisons to evaluate winning hands.
The Fisher-Yates shuffle (graciously borrowed from Mike Bostock):
function shuffle(deck) {
var m = deck.length, t, i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = deck[m];
deck[m] = deck[i];
deck[i] = t;
}
return deck;
};
And an example of a (poorly designed) evaluation test for straights:
function straightTest() {
cards.sort();
var str = '2345A 23456789 06789 0789J 089JQ 09JKQ 0AJKQ'
var find = cards.toString().replace(/,/g,'');
return str.indexOf(find) < 0 ? false : true;
};
Enjoy! (Click cards to hold, then use the button to draw/deal.)
Share your thoughts with me on Twitter @cschidle.
This is an ad-free site. Please consider supporting my writing with one of the support buttons below.