From 4659f308a2b22a32c64ffbb71e0b56fdbd2a71c7 Mon Sep 17 00:00:00 2001 From: Sasha Date: Thu, 1 Feb 2018 12:23:44 +0100 Subject: [PATCH] Use enum instead of magic int constants --- CCard.hs | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/CCard.hs b/CCard.hs index 4aff7ff..5c4ac50 100644 --- a/CCard.hs +++ b/CCard.hs @@ -1,10 +1,7 @@ module CCard where import Data.Word (Word64) -import Data.Bits (setBit, (.|.), (.&.), shift, xor) - -newtype Card = Card Int -type Pack = Word64 +import Data.Bits (setBit, (.|.), (.&.), shift, xor, testBit) data Flower = Pine @@ -21,40 +18,57 @@ data Flower = | Paulownia deriving (Eq, Ord, Enum, Show) -flower :: Card -> Flower -flower (Card n) = toEnum $ n `div` 4 +data Card = + Pine0 | Pine1 | PinePoetry | Crane + | Plum0 | Plum1 | PlumPoetry | BushWarbler + | Cherry0 | Cherry1 | CherryPoetry | CampCurtain + | Wisteria0 | Wisteria1 | WisteriaRed | Cuckoo + | Iris0 | Iris1 | IrisRed | EightPlankBridge + | Peony0 | Peony1 | PeonyBlue | Butterflies + | BushClover0 | BushClover1 | BushCloverRed | Boar + | SusukiGrass0 | SusukiGrass1 | Geese | FullMoon + | Chrysanthemum0 | Chrysanthemum1 | ChrysanthemumBlue | SakeCup + | Maple0 | Maple1 | MapleBlue | Deer + | Lightning | WillowRed | Swallow | RainMan + | Paulownia0 | Paulownia1 | Sand | Phoenix + deriving (Eq, Ord, Enum) -set :: [Int] -> Pack -set = foldl setBit 0 +type Pack = Word64 + +flower :: Card -> Flower +flower = toEnum . (`div` 4) . fromEnum + +set :: [Card] -> Pack +set = foldl setBit 0 . map fromEnum + +contains :: Pack -> Card -> Bool +contains pack = testBit pack . fromEnum inoshikacho :: Pack -inoshikacho = set [23, 27, 39] +inoshikacho = set [Butterflies, Boar, Deer] animals :: Pack -animals = set [7, 15, 19, 30, 35, 42] .|. inoshikacho +animals = set [BushWarbler, Cuckoo, EightPlankBridge, Geese, SakeCup, Swallow] .|. inoshikacho blue :: Pack -blue = set [22, 37, 41] +blue = set [PeonyBlue, ChrysanthemumBlue, MapleBlue] poetry :: Pack -poetry = set [2, 6, 10] +poetry = set [PinePoetry, PlumPoetry, CherryPoetry] -ribbons = set [14, 18, 26, 41] .|. blue .|. poetry +ribbons = set [WisteriaRed, IrisRed, BushCloverRed, WillowRed] .|. blue .|. poetry lights :: Pack -lights = set [3, 11, 31, 43, 47] - -lastIndex :: Int -lastIndex = 47 +lights = set [Crane, CampCurtain, FullMoon, RainMan, Phoenix] plain :: Pack plain = pack `xor` (lights .|. ribbons .|. animals) .&. pack pack :: Pack -pack = 1 `shift` (lastIndex + 1) - 1 +pack = 1 `shift` (fromEnum Phoenix + 1) - 1 pair :: Card -> Card -> Bool pair card1 card2 = flower card1 == flower card2 cards :: [Card] -cards = map Card [0..lastIndex] +cards = [Pine0 .. Phoenix]