secret
Created

Attempting to deal some cards.

  • Download Gist
poker.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
type DealerState = State [Card] [[Card]]
 
-- |Cards.
 
deck :: [Card]
deck = [ (s, r) | s <- suits, r <- ranks ]
 
shuffleDeck :: Int -> RVar [Card]
shuffleDeck n = shuffle $ concat $ replicate n deck
 
deal :: Int -> ([[Card]] -> DealerState)
deal n = \xs -> state $ \s -> (xs ++ [take n s], drop n s)
 
-- |Deal a number of hands a number of cards each.
dealHands :: Int -> Int -> ([[Card]] -> DealerState)
dealHands hs cs = foldr1 (<=<) $ replicate hs (deal cs)

import Control.Monad.State
import Data.Random

type DealerState m a = StateT [Card] m a

-- |Cards.

data Suit = Hearts | Diamonds | Clubs | Spades deriving (Enum, Bounded, Show)

data Rank = Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Woman | King deriving (Enum, Bounded, Show)

type Card = (Rank, Suit)

deck :: [Card]
deck = [ (s, r) | s <- [minBound .. maxBound] , r <- [minBound .. maxBound]]

shuffleDeck :: Int -> RVar [Card]
shuffleDeck n = shuffle . concat $ replicate n deck

deal :: (Functor m, Monad m) => Int -> DealerState m [Card]
deal n = do
(gs,rs') <- splitAt n fmap get
put rs'
return gs

-- |Deal a number of hands a number of cards each.
dealHands :: (Functor m, Monad m) => Int -> Int -> DealerState m [[Card]]
dealHands m n = replicateM m $ deal n

bootGame :: (Functor m, Monad m, RandomSource m s) => s -> Int -> Int -> Int -> DealerState m [[Card]]
bootGame s t m n = do
rs <- lift $ runRVar (shuffleDeck t) s
put rs
dealHands m n

bootGameIO :: Int -> Int -> Int -> DealerState IO [[Card]]
bootGameIO t m n = bootGame StdRandom t m n

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.