Implement shuffle

This commit is contained in:
Sasha 2018-02-01 18:07:31 +01:00
parent 561df60b57
commit 77bc2faa4e
2 changed files with 18 additions and 1 deletions

View File

@ -31,7 +31,7 @@ data Card =
| Maple0 | Maple1 | MapleBlue | Deer
| Lightning | WillowRed | Swallow | RainMan
| Paulownia0 | Paulownia1 | Sand | Phoenix
deriving (Eq, Ord, Enum)
deriving (Eq, Ord, Enum, Show)
type Pack = Word64

View File

@ -2,6 +2,7 @@ module KoiKoi where
import CCard
import Data.Bits (popCount, (.&.))
import System.Random (randomRIO)
type Points = Int
@ -55,3 +56,19 @@ yakus = [
| n > 0 = Just (yaku, n)
| otherwise = Nothing
is set yaku pack = if set == pack then Just (yaku, 5) else Nothing
shuffle :: [a] -> IO [a]
shuffle l =
aux (length l) l
where
aux n [] = return []
aux n [a] = return [a]
aux n l = do
cut <- randomRIO (0, n - 1)
let (top, bottom) = splitAt cut l
shuffledTop <- aux cut top
shuffledBottom <- aux (n - cut) bottom
reorder <- randomRIO (False, True)
return $ if reorder
then shuffledTop ++ shuffledBottom
else shuffledBottom ++ shuffledTop