Skip to content

Instantly share code, notes, and snippets.

@wonderzombie
Created August 25, 2012 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wonderzombie/19916435df2b116e0edc to your computer and use it in GitHub Desktop.
Save wonderzombie/19916435df2b116e0edc to your computer and use it in GitHub Desktop.
Attempting to deal some cards.
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)
@paolino
Copy link

paolino commented Aug 25, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment