-
-
Save wonderzombie/19916435df2b116e0edc to your computer and use it in GitHub Desktop.
Attempting to deal some cards.
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
getput 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