Put Mana into its own module

This commit is contained in:
Correl Roush 2012-01-04 00:14:44 -05:00
parent 10157dcade
commit 0571b86640
2 changed files with 43 additions and 36 deletions

View file

@ -1,41 +1,6 @@
module Magic where module Magic where
------------------------------------------------------------------------------- import Mana
-- Mana
-------------------------------------------------------------------------------
data Color = Colorless
| Black
| White
| Red
| Blue
| Green
deriving (Show, Eq)
data Mana = Mana Int Color
deriving (Show, Eq)
data Cost = Cost [Mana]
deriving (Show, Eq)
class ManaCost a where
converted :: a -> Int
instance ManaCost Cost where
converted (Cost []) = 0
converted (Cost cost) = (foldl (\t (Mana x _) -> t + x) 0) cost
color :: Color -> Cost -> Int
color c (Cost cost) = converted (Cost (filter (\(Mana _ col) -> col == c) cost))
pretty :: Cost -> String
pretty c = concat ( [show (color Colorless c)
, replicate (color Black c) 'B'
, replicate (color White c) 'W'
, replicate (color Red c) 'R'
, replicate (color Blue c) 'U'
, replicate (color Green c) 'G'
])
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
-- Cards -- Cards

42
Mana.hs Normal file
View file

@ -0,0 +1,42 @@
module Mana where
data Color = Colorless
| Black
| White
| Red
| Blue
| Green
deriving (Show, Eq)
data Mana = Mana { amount :: Int
, color :: Color
} deriving (Show, Eq)
data Cost = Cost [Mana]
deriving (Eq)
instance Show Cost where
show c = pretty c
class ManaCost a where
converted :: a -> Int
instance ManaCost Cost where
converted (Cost []) = 0
converted (Cost cost) = (foldl (\t (Mana x _) -> t + x) 0) cost
pretty :: Cost -> String
pretty c = do
let filtered c (Cost cost) = converted $ Cost $ filter (\x -> color x == c) cost
let colored = concat [ replicate (filtered Black c) 'B'
, replicate (filtered White c) 'W'
, replicate (filtered Red c) 'R'
, replicate (filtered Blue c) 'U'
, replicate (filtered Green c) 'G'
]
let colorless = filtered Colorless c
if length colored > 0 && colorless == 0 then
colored
else
(show colorless) ++ colored