From 0571b86640178f2977519dca3f73b3b15ba3d883 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 4 Jan 2012 00:14:44 -0500 Subject: [PATCH] Put Mana into its own module --- Magic.hs | 37 +------------------------------------ Mana.hs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 36 deletions(-) create mode 100644 Mana.hs diff --git a/Magic.hs b/Magic.hs index af52235..e05b686 100644 --- a/Magic.hs +++ b/Magic.hs @@ -1,41 +1,6 @@ module Magic where -------------------------------------------------------------------------------- --- 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' - ]) +import Mana ------------------------------------------------------------------------------- -- Cards diff --git a/Mana.hs b/Mana.hs new file mode 100644 index 0000000..d2873e9 --- /dev/null +++ b/Mana.hs @@ -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 +