Created
December 30, 2012 12:11
-
-
Save tuttlem/4412555 to your computer and use it in GitHub Desktop.
haskell - cards shuffle
This file contains 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
-- | Seeds a list of cards with a random value | |
seedCards :: StdGen -> [Card] -> [(Card, Int)] | |
seedCards g [] = [] | |
seedCards g (c:cs) = x:seedCards ng cs | |
where (seed, ng) = randomR(1, 10000) g :: (Int, StdGen) | |
x = (c, seed) | |
-- | Makes an ordered deck of cards | |
makeDeck :: [Card] | |
makeDeck = [Card v s | v <- [Ace .. King], s <- [Heart .. Spade]] | |
-- | Makes a randomly shuffled deck of cards | |
makeShuffledDeck :: StdGen -> [Card] | |
makeShuffledDeck g = [x | c <- sorted, let x = fst c] | |
where cards = seedCards g makeDeck | |
sorted = sortBy (compare `on` snd) cards |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment