ポーカー遊び完結編

前回のコードに加え、リストをシャッフルする汎用関数(トランプデッキにも適用可能)などを実装しました。デッキを人数分に分けるdistributeも作りました。

randompick [] = return Nothing
randompick xs = do
                x <- randomRIO (0,length xs - 1) :: IO Int
                return (Just (xs !! x))
fromJust (Just a) = a
pickcard deck = do
                x <- randompick deck
                if x /= Nothing then return (x,delete (fromJust x) deck)
                                else return (Nothing,[])
shuffle deck = do
               (card,d) <- pickcard deck
               if card /= Nothing then
                do
                  f <- shuffle d
                  return ((fromJust card):f)
                  else return []
addone [] _ = []
addone [[]] (d:deck) =  [[d]]
addone (x:xs) [] = (x:xs)
addone (x:xs) (d:deck)  = (d:x):(addone xs deck)

distribute n deck = let s = replicate n []
                        distribi l [] = l
                        distribi l deck = let xs = addone l deck
                                          in
                                          distribi xs (drop n deck)
                    in
                    distribi s deck
draw deck 0 = []
draw (x:xs) n = x:(draw xs (n-1))
prepeat p n = do
     x <- shuffle deck
     let d = suitsort (draw x 5)
         (PR s _) = culcPrize d
     if s == p then do print p
                       print d
                       print n
               else prepeat p (n+1)

ポーカーをこれでちょっと遊べて満足したので、ひとまずはこれでおしまいにします。次は画像処理がしたい。