Skip to content

Instantly share code, notes, and snippets.

@velengel
Created August 4, 2015 03:15
Show Gist options
  • Save velengel/934f0dc37a4d9b1dae58 to your computer and use it in GitHub Desktop.
Save velengel/934f0dc37a4d9b1dae58 to your computer and use it in GitHub Desktop.
module Hands
(Hand
, toHand, fromHand
) where
import Cards
import Data.List
import Control.Monad
newtype Hand = Hand { fromHand :: [Card] } deriving (Show, Eq, Ord)
{-decision :: [Card] -> Maybe [Card]
decision l =
if length l == 5
then Just $ sort l
else Nothing-}
toHand :: [Card] -> Maybe Hand
toHand l =
if length l == 5
then Just $ Hand (sort l)
else Nothing
data PokerHand
= HighCards
| OnePair
| TwoPair
| ThreeOfAKind
| Straight
| Flush
| FullHouse
| FourOfAKind
| StraightFlush
deriving (Show, Read, Eq, Ord, Enum)
pokerHand :: Hand -> (PokerHand, Card)
pokerHand = undefined
straightFlush :: Hand -> Maybe (PokerHand, Card)
straightFlush = undefined
fourOfAKind :: Hand -> Maybe (PokerHand, Card)
fourOfAKind = undefined
fullHouse :: Hand -> Maybe (PokerHand, Card)
fullHouse = undefined
flush :: Hand -> Maybe (PokerHand, Card)
flush = undefined
straight :: Hand -> Maybe (PokerHand, Card)
straight = undefined
threeOfAKind :: Hand -> Maybe (PokerHand, Card)
threeOfAKind = undefined
twoPair :: Hand -> Maybe (PokerHand, Card)
twoPair = undefined
onePair :: Hand -> Maybe (PokerHand, Card)
onePair = undefined
--straightHint :: Hand -> Maybe Card
flushHint :: Hand -> Maybe Card
flushHint (Hand (x:xs)) =
if all((cardSuit x==).cardSuit) xs then Just (last xs) else Nothing
nOfKindHint :: Int -> Hand -> Maybe [[Card]]
nOfKindHint n (Hand h) = if cards /= [] then Just cards else Nothing
where
cards :: [[Card]]
cards = filter ((==n).length)
$ groupBy(\x y -> cardNumber x == cardNumber y) h
cardStrength :: Card -> Int
cardStrength (Card n _) = n
{- extractCardNumber :: [Card] -> [(Int, Card)]
extractCardNumber f cs = map (\c -> (cardStrength c, c)) cs
extract :: (Card -> Int) -> [Card] -> [(Int, Card])]
extract f cs = map(\c = (f c, c)) cs-}
extract :: (Card -> Int) -> [Card] -> [(Int, Card)]
extract f cs = map (\c -> (f c, c)) cs
straightHint :: Hand -> Maybe Card
straightHint (Hand l) =
(judgeStraight . extract cardStrength $ l)
`mplus`
(judgeStraight . sort . extract cardNumber $ l)
where
isStraight :: [Int] -> Bool
isStraight xs@(x:_) = xs == [x .. x + 4]
isStraight _ = False
judgeStraight :: [(Int, Card)] -> Maybe Card
judgeStraight =
if isStraight $ fmap fst l
then Just . snd . last $ l
else Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment