Use enum instead of magic int constants
This commit is contained in:
parent
59beaa5cc9
commit
4659f308a2
1 changed files with 33 additions and 19 deletions
52
CCard.hs
52
CCard.hs
|
@ -1,10 +1,7 @@
|
||||||
module CCard where
|
module CCard where
|
||||||
|
|
||||||
import Data.Word (Word64)
|
import Data.Word (Word64)
|
||||||
import Data.Bits (setBit, (.|.), (.&.), shift, xor)
|
import Data.Bits (setBit, (.|.), (.&.), shift, xor, testBit)
|
||||||
|
|
||||||
newtype Card = Card Int
|
|
||||||
type Pack = Word64
|
|
||||||
|
|
||||||
data Flower =
|
data Flower =
|
||||||
Pine
|
Pine
|
||||||
|
@ -21,40 +18,57 @@ data Flower =
|
||||||
| Paulownia
|
| Paulownia
|
||||||
deriving (Eq, Ord, Enum, Show)
|
deriving (Eq, Ord, Enum, Show)
|
||||||
|
|
||||||
flower :: Card -> Flower
|
data Card =
|
||||||
flower (Card n) = toEnum $ n `div` 4
|
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
|
type Pack = Word64
|
||||||
set = foldl setBit 0
|
|
||||||
|
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 :: Pack
|
||||||
inoshikacho = set [23, 27, 39]
|
inoshikacho = set [Butterflies, Boar, Deer]
|
||||||
|
|
||||||
animals :: Pack
|
animals :: Pack
|
||||||
animals = set [7, 15, 19, 30, 35, 42] .|. inoshikacho
|
animals = set [BushWarbler, Cuckoo, EightPlankBridge, Geese, SakeCup, Swallow] .|. inoshikacho
|
||||||
|
|
||||||
blue :: Pack
|
blue :: Pack
|
||||||
blue = set [22, 37, 41]
|
blue = set [PeonyBlue, ChrysanthemumBlue, MapleBlue]
|
||||||
|
|
||||||
poetry :: Pack
|
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 :: Pack
|
||||||
lights = set [3, 11, 31, 43, 47]
|
lights = set [Crane, CampCurtain, FullMoon, RainMan, Phoenix]
|
||||||
|
|
||||||
lastIndex :: Int
|
|
||||||
lastIndex = 47
|
|
||||||
|
|
||||||
plain :: Pack
|
plain :: Pack
|
||||||
plain = pack `xor` (lights .|. ribbons .|. animals) .&. pack
|
plain = pack `xor` (lights .|. ribbons .|. animals) .&. pack
|
||||||
|
|
||||||
pack :: Pack
|
pack :: Pack
|
||||||
pack = 1 `shift` (lastIndex + 1) - 1
|
pack = 1 `shift` (fromEnum Phoenix + 1) - 1
|
||||||
|
|
||||||
pair :: Card -> Card -> Bool
|
pair :: Card -> Card -> Bool
|
||||||
pair card1 card2 = flower card1 == flower card2
|
pair card1 card2 = flower card1 == flower card2
|
||||||
|
|
||||||
cards :: [Card]
|
cards :: [Card]
|
||||||
cards = map Card [0..lastIndex]
|
cards = [Pine0 .. Phoenix]
|
||||||
|
|
Loading…
Reference in a new issue