-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.hs
More file actions
34 lines (27 loc) · 977 Bytes
/
deck.hs
File metadata and controls
34 lines (27 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module Deck where
import System.IO.Unsafe
import System.Random
import Data.Map
import Data.List
import Cards
type Deck = [Card]
fullDeck :: Deck
fullDeck = [Card v s | v <-[Two .. Ace], s <-[Spades,Hearts,Diamonds,Clubs]]
shuffleDeck :: Deck -> Deck
shuffleDeck xs = parse_deck $ shuffle' xs (length xs)
where
shuffle' _ 0 = return []
shuffle' xs len =
do n <- randomRIO (0, len - 1)
let (y, ys) = choose n xs
ys' <- shuffle' ys (len - 1)
return (y:ys')
-- pick / choose method
choose _ [] = error "choose: index out of range"
choose 0 (x:xs) = (x, xs)
choose i (x:xs) = let (y, ys) = choose (i - 1) xs in (y, x:ys)
-- parse deck
parse_deck :: IO Deck -> Deck
parse_deck io = unsafePerformIO io
dealNCards :: Int -> Deck -> ([Card], Deck)
dealNCards n xxs = (sort $ take n xxs, shuffleDeck $ drop n xxs)