Use enum instead of magic int constants

This commit is contained in:
Sasha 2018-02-01 12:23:44 +01:00
parent 59beaa5cc9
commit 4659f308a2

View file

@ -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]