From 3615c29a47fa550202bce8a8f177a52759f0b8c8 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Thu, 22 Aug 2019 17:45:33 +0200 Subject: [PATCH] Add and expose more useful functions on Hanafuda cards and use them in Yakus computations --- src/Hanafuda.hs | 10 ++++++++++ src/Hanafuda/KoiKoi/Yaku.hs | 12 ++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Hanafuda.hs b/src/Hanafuda.hs index a19357e..924efba 100644 --- a/src/Hanafuda.hs +++ b/src/Hanafuda.hs @@ -5,8 +5,10 @@ module Hanafuda ( , Pack , add , cards + , cardsOf , cardsOfPack , contains + , difference , empty , flower , intersection @@ -16,6 +18,7 @@ module Hanafuda ( , sameMonth , shuffle , size + , union ) where import Data.Word (Word64) @@ -27,6 +30,7 @@ import Data.Bits ( , testBit , xor , (.&.) + , (.|.) , countTrailingZeros ) import System.Random (randomRIO) @@ -112,6 +116,12 @@ intersection = portBinary (.&.) difference :: Pack -> Pack -> Pack difference = portBinary (\a b -> a `xor` (a .&. b)) +union :: Pack -> Pack -> Pack +union = portBinary (.|.) + +cardsOf :: Flower -> Pack +cardsOf = Pack . shift 0xf . (* 4) . fromEnum + sameMonth :: Card -> Pack -> Pack sameMonth card (Pack p) = Pack $ (0xf `shift` (fromEnum card .&. 0xfc)) .&. p diff --git a/src/Hanafuda/KoiKoi/Yaku.hs b/src/Hanafuda/KoiKoi/Yaku.hs index 85a1b02..fb5501e 100644 --- a/src/Hanafuda/KoiKoi/Yaku.hs +++ b/src/Hanafuda/KoiKoi/Yaku.hs @@ -6,11 +6,15 @@ module Hanafuda.KoiKoi.Yaku ( , sumYakus ) where -import Hanafuda (Card(..), Monthly, Pack, add, contains, intersection, packOfCards, size) +import Hanafuda ( + Card(..), Monthly, Pack + , add, cardsOf, cardsOfPack, contains, difference, intersection, packOfCards + , size + ) import Hanafuda.Player (Points) import qualified Data.Map as M (Map, empty, insert, unionWith, (!)) import qualified Data.Set as S (Set, empty, singleton, union) -import Control.Monad.Reader (reader) +import Control.Monad.Reader (asks) data Yaku = Lights @@ -73,7 +77,7 @@ index (yaku, cards, scorer) = finders :: Monthly YakusByCard finders = do - monthCardPlus <- reader $ (+) . (4*) . fromEnum + monthCards <- cardsOfPack <$> asks cardsOf return $ foldl (\yakusByCard -> M.unionWith S.union yakusByCard . index) M.empty [ (Lights, [Crane, CampCurtain, FullMoon, RainMan, Phoenix], lights) , (InoShikaCho, inoshikacho, fixed 5) @@ -84,7 +88,7 @@ finders = do , (Kasu, plains, moreThan 9) , (TsukimiZake, [SakeCup, FullMoon], fixed 3) , (HanamiZake, [SakeCup, CampCurtain], fixed 3) - , (TsukiFuda, map (toEnum . monthCardPlus) [0..3], fixed 5) + , (TsukiFuda, monthCards, fixed 5) ] where inoshikacho = [Butterflies, Boar, Deer]