Poker hand analyser: Difference between revisions

m
Line 1,982:
parseCard :: String -> Maybe Card
parseCard [] = Nothing
parseCard xs = (\a b -> Card a b) <$> parsedSuitparseSuit <*> parsedRankparseRank
where parsedRankparseRank = case r of "a" -> Just Ace
"2" -> Just Two
"3" -> Just Three
"4" -> Just Four
"5" -> Just Five
"6" -> Just Six
"7" -> Just Seven
"8" -> Just Eight
"9" -> Just Nine
"10" -> Just Ten
"j" -> Just Jack
"q" -> Just Queen
"k" -> Just King
_ -> Nothing
where r = init xs
parsedSuitparseSuit = case s of '♥' -> Just Heart
'♦' -> Just Diamond
'♣' -> Just Club
'♠' -> Just Spade
_ -> Nothing
where s = head $ reverse xs
 
nameHand :: String -> String
nameHand s
| invalidHand = ": invalid"
| straight && flush = ": straight-flush"
| ofKind 4 = ": four-of-a-kind"
| uniqRanksofKind ==3 && ofKind 2 = ": full-house"
| flush = ": flush"
| straight = ": straight"
Line 2,019:
where cards = catMaybes $ map parseCard $ words s
sortedRank = sort $ map rank cards
sameRank xsranks = map rankCountrankCountTuple groupedByRankcards
where groupedByRankrankCountTuple xs = groupsortedGroups sortedRank$ map rankCount $ groupedByRank
rankCount c@(x:xs) =where (x,groupedByRank = lengthgroup c)sortedRank
rankCount c@(x:xs) = (x, length c)
ranks = sameRank cards
sortedGroups = sortBy (\(_, n) (_, n') -> compare n' n)
uniqRanks = length ranks
ofKind n = any (\(_, y) -> n == y) ranks
Line 2,028 ⟶ 2,029:
flush = and $ map (\c -> s == suit c) cards
where s = suit $ head cards
invalidHand = length dedup(nub cards) /= 5
where dedup = nub cards
 
testHands :: [String]
Anonymous user