JavaScript – Simulating Two Handed Card Shuffle
June 14, 2009 by: Allen SanfordI decided that I would write a tutorial on how to simulate a two handed card shuffle on a deck of cards. Most of us know what a two handed card shuffle is, and most of use this method to shuffle cards. To explain for those of you still not sure what a to handed shuffle is here you go. When most of us sit down to play cards and someone says “Well shuffle up those cards up and deal them already”, we will grab the deck and split the cards into two piles, one for the left hand the other in the right hand. Now we will begin to “thumb” random numbers of cards from the left and right hand piles of cards into one pile of cards. Now everyone should be on the same page here.
Most programmers out there will probably be saying “why do I want to simulate shuffling cards this way, when I can just randomize the card array or what every structure they have chosen to use.” Well if you feel that way close your browser or hit the back button and be on your way. For those of you interested in learning you can continue to read. Now I don’t claim that this is the best solution out there, but I would like to think it is pretty darn close, that being said if you would like to contribute to my example please feel free to leave a comment and I will look it over.
OK, so my approach for the tutorial here is simple, I will be implementing a Deck object. I will be generating and array to hold the and deck each element of the array will be an array holding both the face value and the suit of each card. By the way I will be implementing a way to have both a fresh deck and an old rusty deck. After the deck has been initialed we will implement a function to shuffle the deck. And finally we will print the deck out to the screen.
Now I have commented the code for the shuffling portion pretty well so I will just let you read it for your self. Should anyone have any questions please leave them in the comments section and I will address them in the comments section below thanks.
function Deck(newDeck) { this.deck = new Array(); values = new Array("A","1","2","3","4","5","6","7","8","9","10","J","Q","K"); suits = new Array("S","H","D","C"); if (!newDeck) { values.sort(function() {return 0.5 - Math.random()}); suits.sort(function() {return 0.5 - Math.random()}); } for (i=0; i<suits.length; i++) { for (j=0; j<values.length; j++) { card = new Array(values[j], suits[i]); this.deck.push(card); } } } Deck.prototype.twoHandShuffle = function(times) { for (i=0; i<(1*times); i++) { // Find the rough mid point of the deck // And set the upper and lower bounds for cutting the deck mid = Math.floor(this.deck.length / 2); upper = Math.floor(mid / 2) + mid; lower = mid - Math.floor(mid / 2); // Cut the deck somewhere between 1/4 and 3/4 of the deck cut = Math.floor((upper-(lower-1))*Math.random()) + lower; leftHand = this.deck.slice(0, cut); rightHand = this.deck.slice(cut); tempDeck = new Array(); // Now we simulate the two hand shuffle while (leftHand.length > 0 || rightHand.length > 0) { // Swapping hands gives it a more life like touch. // Simulates double drops from one hand as well // as other possible random artifacts // Randomly decide if we are going to swap. swapHands = Math.round(Math.random()); if (swapHands) { tempHand = leftHand; leftHand = rightHand; rightHand = tempHand; } // We randomly pick between 1 and five cards // each loop to drop in from each hand. randL = Math.floor((5-(1))*Math.random()) + 1; randR = Math.floor((5-(1))*Math.random()) + 1; if (leftHand.length >= randL) { for (j=0; j<randL; j++) tempDeck.push(leftHand.shift()); } else if (leftHand.length > 0) { for (j=0; j<leftHand.length; j++) tempDeck.push(leftHand.shift()); } if (rightHand.length >= randR) { for (j=0; j<randR; j++) tempDeck.push(rightHand.shift()); } else if (rightHand.length > 0) { for (j=0; j<rightHand.length; j++) tempDeck.push(rightHand.shift()); } } this.deck = tempDeck; } } Deck.prototype.print = function() { for (i=0; i <this.deck.length; i++) { card = this.deck[i]; document.write(card[0]+card[1]+" "); } } // Get a Deck of cards // Pass true into Deck(true) for bradn new deck deck = new Deck(); // Enter the number of times to shuffle the deck deck.twoHandShuffle(1); // Display what the deck looks like deck.print(); |
See a PHP version here
Well there you have it! And Have a Good’n!











