forked from AlexanderEkdahl/EDAN40
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vegas.hs
54 lines (44 loc) · 1.35 KB
/
vegas.hs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module Main where
import System.Random (randomRIO)
import Data.Array.IO
import Control.Monad
data Suit = Club | Diamond | Heart | Spade
deriving (Show, Enum)
data Value = Two | Three-- | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace
deriving (Show, Enum)
type Card = (Suit, Value)
type Deck = [Card]
next :: Suit -> Suit
next Spade = Club
next x = succ x
makeDeck :: Deck
makeDeck = [(suit, value) | suit <- [Club .. Spade], value <- [Two .. Three]]
shuffle :: [a] -> IO [a]
shuffle xs = do
ar <- newArray n xs
forM [1..n] $ \i -> do
j <- randomRIO (i,n)
vi <- readArray ar i
vj <- readArray ar j
writeArray ar j vi
return vj
where
n = length xs
newArray :: Int -> [a] -> IO (IOArray Int a)
newArray n xs = newListArray (1,n) xs
sequentialDeck :: Deck -> Bool
sequentialDeck d = f d Club
where f [] _ = True
f (c:cs) s =
if matchSuit c s
then f cs (next s)
else False
matchSuit :: Card -> Suit -> Bool
matchSuit (Club, _) Club = True
matchSuit (Diamond, _) Diamond = True
matchSuit (Heart, _) Heart = True
matchSuit (Spade, _) Spade = True
matchSuit _ _ = False
main :: IO ()
main = repeat 1
where repeat x = fmap sequentialDeck (shuffle makeDeck) >>= (\v -> if v then putStrLn (show x) else repeat (x+1))