Comment detail

ポーカーの役判定 (Nested Flatten)

ストレートの判定がなかなかきれいにまとまりませんね。ソートしておいて隣り合う数の差で判断してみましたが……うーん。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Data.List as List

hand :: String -> (String, Bool)
hand cards =
    (case kinds of
       [1, 4] -> "Four of a kind"
       [2, 3] -> "Full house"
       [1, 1, 3] -> "Three of a kind"
       [1, 2, 2] -> "Two pair"
       [1, 1, 1, 2] -> "One pair"
       otherwise ->
           case rdiff of
               [1, 1, 1, 1] | head ranks == 10 && isFlush -> "Royal"
                            | True -> "Straight"
               [1, 1, 1, 9] -> "Straight"
               otherwise -> "No pair"
    , isFlush)
    where suits = List.nub [cards !! n | n <- [0,2..8]]
          isFlush = length suits == 1
          ranks = List.sort [rankToInt $ cards !! n | n <- [1,3..9]]
          rankToInt r = case List.elemIndex r "23456789TJQKA" of Just n -> n+2
          kinds = List.sort $ map length $ List.group ranks
          rdiff = zipWith (-) (tail ranks) ranks

main = mapM test ["SQSJSASKST", "D9D7D6D5D8", "C2D2S2H3H2", "C2D3S2H3H2",
                  "S9S4S8STSJ", "C4H7D5S6H3", "S6H6C5DQC6", "S6HQC5DQC6",
                  "S6H4C5DQC6" ,"SJSQSKSAC2"]
    where test s = putStrLn (s ++ " => " ++ (showHand $ hand s))
          showHand ("No pair", True) = "Flush"
          showHand (s, True) = s ++ " flush"
          showHand (s, False) = s

Index

Feed

Other

Link

Pathtraq

loading...