diff options
Diffstat (limited to 'src/runchess.hs')
-rw-r--r-- | src/runchess.hs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/runchess.hs b/src/runchess.hs new file mode 100644 index 0000000..27a6da8 --- /dev/null +++ b/src/runchess.hs @@ -0,0 +1,35 @@ +-- vim: sw=2 ts=2 et: +import Control.Monad +import Control.Monad.Trans.State.Lazy + +import Data.Attoparsec.ByteString.Char8 +import Data.ByteString.Char8 (pack) +import Data.Maybe + +import Chess +import Chess.FEN +import Chess.PGN + +main = readPGN >>= mapM_ applyPGN + +readPGN :: IO [PGN] +readPGN = do + pgn <- pack <$> getContents + case parseOnly pgnParser pgn of + Left err -> error err + Right [] -> error "no games" + Right games -> return games + +applyPGN :: PGN -> IO () +applyPGN pgn = printGame (foldM (flip moveSAN) defaultBoard $ moves pgn) pgn + +printGame :: Either MoveError Board -> PGN -> IO () +printGame b pgn = printPGN pgn >> printBoard b + where + printBoard (Left e) = putStrLn $ show e + printBoard (Right b) = putStr $ show b + + printPGN :: PGN -> IO () + printPGN pgn = do + putStrLn $ site pgn ++ " " ++ date pgn ++ ": " ++ whitePlayer pgn ++ " vs. " ++ blackPlayer pgn + putStrLn $ show $ result pgn |