summaryrefslogtreecommitdiff
path: root/src/runchess.hs
blob: 27a6da8a281f59a942c602b0f245dfc187067f25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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