From 77bc2faa4e6d3b48537fc1d64c2a700a6c25f09a Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 1 Feb 2018 18:07:31 +0100 Subject: [PATCH] Implement shuffle --- CCard.hs | 2 +- KoiKoi.hs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CCard.hs b/CCard.hs index 5c4ac50..7b8e2be 100644 --- a/CCard.hs +++ b/CCard.hs @@ -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 diff --git a/KoiKoi.hs b/KoiKoi.hs index 8e1c446..00a393a 100644 --- a/KoiKoi.hs +++ b/KoiKoi.hs @@ -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