Implement shuffle
This commit is contained in:
parent
561df60b57
commit
77bc2faa4e
2 changed files with 18 additions and 1 deletions
2
CCard.hs
2
CCard.hs
|
@ -31,7 +31,7 @@ data Card =
|
||||||
| Maple0 | Maple1 | MapleBlue | Deer
|
| Maple0 | Maple1 | MapleBlue | Deer
|
||||||
| Lightning | WillowRed | Swallow | RainMan
|
| Lightning | WillowRed | Swallow | RainMan
|
||||||
| Paulownia0 | Paulownia1 | Sand | Phoenix
|
| Paulownia0 | Paulownia1 | Sand | Phoenix
|
||||||
deriving (Eq, Ord, Enum)
|
deriving (Eq, Ord, Enum, Show)
|
||||||
|
|
||||||
type Pack = Word64
|
type Pack = Word64
|
||||||
|
|
||||||
|
|
17
KoiKoi.hs
17
KoiKoi.hs
|
@ -2,6 +2,7 @@ module KoiKoi where
|
||||||
|
|
||||||
import CCard
|
import CCard
|
||||||
import Data.Bits (popCount, (.&.))
|
import Data.Bits (popCount, (.&.))
|
||||||
|
import System.Random (randomRIO)
|
||||||
|
|
||||||
type Points = Int
|
type Points = Int
|
||||||
|
|
||||||
|
@ -55,3 +56,19 @@ yakus = [
|
||||||
| n > 0 = Just (yaku, n)
|
| n > 0 = Just (yaku, n)
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
is set yaku pack = if set == pack then Just (yaku, 5) else 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
|
||||||
|
|
Loading…
Reference in a new issue