Created
August 29, 2017 18:23
-
-
Save craigquincy/7a07682cb6f247e4ac0e1daa6e55592d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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