Poker hand analyser: Difference between revisions

m
Line 1,991:
parseCard xs = Card <$> parseSuit <*> parseRank
where
parseRank = case rinit xs of "a" -> Just Ace
"a2" -> Just AceTwo
"23" -> Just TwoThree
"34" -> Just ThreeFour
"45" -> Just FourFive
"56" -> Just FiveSix
"67" -> Just SixSeven
"78" -> Just SevenEight
"89" -> Just EightNine
"910" -> Just NineTen
"10j" -> Just TenJack
"jq" -> Just JackQueen
"qk" -> Just QueenKing
_ -> Nothing
"k" -> Just King
 
_ -> Nothing
parseSuit = case slast xs of '♥' -> Just Heart
where r = init xs
'♦' -> Just Diamond
parseSuit = case s of
'' -> Just HeartClub
'' -> Just DiamondSpade
_ -> Nothing
'♣' -> Just Club
'♠' -> Just Spade
_ -> Nothing
where s = last xs
 
nameHand :: String -> String
Line 2,029 ⟶ 2,026:
cards = mapMaybe parseCard (words s)
sortedRank = sort $ rank <$> cards
rankCounts = sortBy (compare `on` snd) $ (\c@(r : _) -> (r, length c)) <$> group sortedRank
ranks = sortedGroups $ rankCount <$> groupedByRank
uniqRanks = length ranksrankCounts
where
ofKind n = any ((==n) . snd) ranksrankCounts
groupedByRank = group sortedRank
straight = isSucc sortedRank || sortedRank == acesHigh
rankCount c@(x : _) = (x, length c)
sortedGroups flush = sortByall (compare(== `on`firstSuit) snd. suit) cards
where firstSuit = suit $ head cards
uniqRanks = length ranks
ofKind n = any ((==n) . snd) ranks
straight = isSucc sortedRank || sortedRank == acesHigh
flush = all ((== firstSuit) . suit) cards
where firstSuit = suit $ head cards
invalidHand = length (nub cards) /= 5
 
Anonymous user