Skip to content

Commit

Permalink
Complete shuffleDeck.js - with Fisher Yates shuffle
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 2, 2015
1 parent 686331e commit 46c533e
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions shuffleDeck/shuffleDeck.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,43 @@
* See https://www.dartmouth.edu/~chance/teaching_aids/books_articles/Mann.pdf .
*/

var shuffleDeck = function(deck) {
// Your code here
// Apparently, this is biased...
// var shuffleDeck = function (deck) {
// var len = deck.length;
// var i;

// for (i = 0; i < len; i += 1) {
// swap(deck, i, Math.floor( Math.random() * len ) );
// }

// return deck;
// };

var shuffleDeck = function (deck) {
var numUnshuffledCards = deck.length;
var randomIdx;

while ( numUnshuffledCards > 0 ) {
randomIdx = Math.floor( Math.random() * numUnshuffledCards );
numUnshuffledCards -= 1;
swap( deck, numUnshuffledCards, randomIdx );
}

return deck;
};

var swap = function (deck, i, j) {
var temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
};

// Ordered deck generator provided for your testing convenience
// (You may alter this function, but an unaltered copy will be used for tests.)
var orderedDeck = function() {
var suits = [ '♥', '♣', '♠', '♦' ];
var orderedDeck = function () {
var suits = [ '♥', '♣', '♠', '♦' ];
var values = [ 'A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K' ];
var deck = [];
var deck = [];

suits.forEach(function(suit) {
values.forEach(function(value) {
Expand Down

0 comments on commit 46c533e

Please sign in to comment.