Card parser

This commit is contained in:
Correl Roush 2012-03-02 21:15:06 -05:00
parent 49d01e6520
commit 17bb3989fb
3 changed files with 60 additions and 8 deletions

53
CardParser.hs Normal file
View file

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

View file

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

View file

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