From 17bb3989fbefb39c9ec1db3ab910c4928e4ff85e Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Fri, 2 Mar 2012 21:15:06 -0500 Subject: [PATCH] Card parser --- CardParser.hs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ Magic.hs | 14 +++++++------- ManaParser.hs | 1 - 3 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 CardParser.hs diff --git a/CardParser.hs b/CardParser.hs new file mode 100644 index 0000000..568efe6 --- /dev/null +++ b/CardParser.hs @@ -0,0 +1,53 @@ +module CardParser where + +import Magic +import Mana +import ManaParser +import Text.ParserCombinators.Parsec + +test_card = unlines $ [ "AEther Adept" + , "1UU" + , "Creature -- Human Wizard" + , "2/2" + , "When AEther Adept enters the battlefield, return target creature to its owner's hand." + , "M11-C, M12-U" + ] + +card :: Parser Card +card = do + name' <- line + mana' <- (option (Cost [] [] []) (mana_line)) + abilities' <- (manyTill line (try $ lookAhead sets_line)) + sets' <- sets_line + let t = snd $ last sets' + return $ Card t name' mana' abilities' + +line :: Parser String +line = do + text <- many (noneOf "\n") + char '\n' + return text + +mana_line :: Parser Cost +mana_line = do + mana' <- mana + char '\n' + return mana' + +sets_line :: Parser [(String, Rarity)] +sets_line = do + sets <- sepBy set (string ", ") + return sets + +set :: Parser (String, Rarity) +set = do + set' <- many1 (noneOf "\n-") + char '-' + rarity' <- oneOf "LCURMS" + case rarity' of + 'L' -> return (set', Land) + 'C' -> return (set', Common) + 'U' -> return (set', Uncommon) + 'R' -> return (set', Rare) + 'M' -> return (set', Mythic) + 'S' -> return (set', Special) diff --git a/Magic.hs b/Magic.hs index 4959184..199d0a6 100644 --- a/Magic.hs +++ b/Magic.hs @@ -14,22 +14,22 @@ data Rarity = Land | Special deriving (Show, Eq) -data Card = Card Rarity String Cost +data Card = Card Rarity String Cost [String] deriving (Show, Eq) instance Mana Card where - colors (Card _ _ cost) = colors cost - converted (Card _ _ cost) = converted cost + colors (Card _ _ cost _) = colors cost + converted (Card _ _ cost _) = converted cost data Deck = Deck [Card] curve :: Deck -> [(Int, Int)] curve (Deck cards) = do let largest = maximum (map converted cards) - map (\x -> (x, length (filter (\(Card rarity _ cost) -> rarity /= Land && converted cost == x) cards))) [0..largest] + map (\x -> (x, length (filter (\(Card rarity _ cost _) -> rarity /= Land && converted cost == x) cards))) [0..largest] -cards = concat $ [ replicate 13 (Card Land "Swamp" (Cost [] [] [])) - , replicate 13 (Card Land "Plains" (Cost [] [] [])) - , replicate 4 (Card Mythic "Jace Beleren" (Cost [Standard 1 Colorless, Standard 2 Blue] [] [])) +cards = concat $ [ replicate 13 (Card Land "Swamp" (Cost [] [] []) []) + , replicate 13 (Card Land "Plains" (Cost [] [] []) []) + , replicate 4 (Card Mythic "Jace Beleren" (Cost [Standard 1 Colorless, Standard 2 Blue] [] []) []) ] deck = Deck cards diff --git a/ManaParser.hs b/ManaParser.hs index 3520162..56b17c2 100644 --- a/ManaParser.hs +++ b/ManaParser.hs @@ -9,7 +9,6 @@ mana = do colored' <- (many colored) hybrid' <- (many hybrid) phyrexian' <- (many phyrexian) - eof return $ Cost (colorless' ++ colored') hybrid' phyrexian' standard :: Parser Standard