Skip to content

Instantly share code, notes, and snippets.

@craigquincy
Created August 29, 2017 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigquincy/7a07682cb6f247e4ac0e1daa6e55592d to your computer and use it in GitHub Desktop.
Save craigquincy/7a07682cb6f247e4ac0e1daa6e55592d to your computer and use it in GitHub Desktop.
var suits = ["Hearts", "Diamonds", "Spades", "Clovers"];
var faces = ["Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "Kings", "Joker"]
var deck = [];
for(suit of suits){
for(face of faces){
deck.push(face + " of " + suit);
}
}
function checkIfShuffled(deck) {
count = 0;
lastSuit = null;
var thisCard = null;
var thisSuit = null;
for(var c = 0; c < deck.length; c++) {
thisCard = deck[c];
thisSuit = thisCard.split(" ")[2];
if ((lastSuit !== null) && (lastSuit === thisSuit)) {
count++;
} else {
count = 0;
}
lastSuit = thisSuit;
if (count > 2) {
return false;
}
}
return true;
}
isShuffled = false;
while (isShuffled === false) {
swapFrom = Math.floor(Math.random() * deck.length);
swapTo = Math.floor(Math.random() * deck.length);
toHolder = deck[swapTo];
deck[swapTo] = deck[swapFrom];
deck[swapFrom] = toHolder;
isShuffled = checkIfShuffled(deck);
}
deck.sort()
console.log(deck)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment