Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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