summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/.gitignore3
-rw-r--r--src/Makefile15
-rw-r--r--src/runchess.hs35
-rw-r--r--test/1924-wilson-ewing.pgn22
-rw-r--r--test/1956-byrne-fischer.pgn43
5 files changed, 118 insertions, 0 deletions
diff --git a/src/.gitignore b/src/.gitignore
new file mode 100644
index 0000000..c181314
--- /dev/null
+++ b/src/.gitignore
@@ -0,0 +1,3 @@
+*.hi
+*.o
+runchess
diff --git a/src/Makefile b/src/Makefile
new file mode 100644
index 0000000..6e140a1
--- /dev/null
+++ b/src/Makefile
@@ -0,0 +1,15 @@
+EXE:=runchess
+AUX:=$(addsuffix .o,$(EXE)) $(addsuffix .hi,$(EXE))
+GHC:=ghc
+GHCFLAGS:=-O
+
+all: $(EXE)
+
+run: $(EXE)
+ ./$(EXE)
+
+$(EXE): %: %.hs
+ $(GHC) $(GHCFLAGS) $< -o $@
+
+clean:
+ $(RM) $(EXE) $(AUX)
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
diff --git a/test/1924-wilson-ewing.pgn b/test/1924-wilson-ewing.pgn
new file mode 100644
index 0000000..f8b87ed
--- /dev/null
+++ b/test/1924-wilson-ewing.pgn
@@ -0,0 +1,22 @@
+[Event "CAN ch"]
+[Site "Hamilton"]
+[Date "1924.08.20"]
+[Round "6"]
+[White "Wilson, Stanley B"]
+[Black "Ewing, John M"]
+[Result "0-1"]
+[ECO "B01"]
+[PlyCount "118"]
+[EventDate "1924.08.18"]
+
+1. e4 d5 2. exd5 Qxd5 3. Nc3 Qd8 4. Nf3 c6 5. b3 Nf6 6. Bb2 Bf5 7. Be2 e6 8.
+O-O Bd6 9. Nh4 Bg6 10. h3 Nbd7 11. Rc1 Nd5 12. Nxd5 Qxh4 13. Ne3 O-O 14. d4 Nf6
+15. Bf3 Nd5 16. Nxd5 exd5 17. c4 dxc4 18. bxc4 Be4 19. Bxe4 Qxe4 20. Re1 Qf4
+21. g3 Qf5 22. Kg2 Rfe8 23. Qb3 b6 24. Re3 Rxe3 25. Qxe3 Qe6 26. Qf3 Rc8 27. d5
+Qd7 28. dxc6 Qxc6 29. Qd5 Qxd5+ 30. cxd5 Rxc1 31. Bxc1 f5 32. Kf3 Kf7 33. Ke2
+Ke7 34. Kd3 Bc5 35. Be3 Kd6 36. Kc4 Bxe3 37. fxe3 Ke5 38. a4 a6 39. a5 bxa5 40.
+Kc5 a4 41. d6 Ke6 42. Kc6 a3 43. d7 a2 44. d8=Q a1=Q 45. Qd5+ Ke7 46. Qd7+ Kf8
+47. Qd6+ (47. Qxf5+ Qf6+ 48. Qxf6+ gxf6 49. Kb6) 47... Kf7 48. Qd7+ Kg6 49.
+Qe8+ Kh6 50. Qe6+ g6 51. Kb6 Qa4 52. Qe5 a5 53. Qxa5 Qxa5+ 54. Kxa5 Kg5 55. Kb4
+h5 56. Kc3 h4 57. gxh4+ Kxh4 58. Kd3 Kxh3 59. Ke2 59... Kg2 {(Le Pion 1924)}
+0-1
diff --git a/test/1956-byrne-fischer.pgn b/test/1956-byrne-fischer.pgn
new file mode 100644
index 0000000..3fe5a09
--- /dev/null
+++ b/test/1956-byrne-fischer.pgn
@@ -0,0 +1,43 @@
+[Event "Third Rosenwald Trophy"]
+[Site "New York, NY USA"]
+[Date "1956.10.17"]
+[EventDate "1956.10.07"]
+[Round "8"]
+[Result "0-1"]
+[White "Donald Byrne"]
+[Black "Robert James Fischer"]
+[ECO "D92"]
+[WhiteElo "?"]
+[BlackElo "?"]
+[PlyCount "82"]
+
+1. Nf3 Nf6 2. c4 g6 3. Nc3 Bg7 4. d4 O-O 5. Bf4 d5 6. Qb3 dxc4
+7. Qxc4 c6 8. e4 Nbd7 9. Rd1 Nb6 10. Qc5 Bg4 11. Bg5 {11. Be2
+followed by 12. O-O would have been more prudent. The bishop
+move played allows a sudden crescendo of tactical points to be
+uncovered by Fischer. -- Wade} Na4 {!} 12. Qa3 {On 12. Nxa4
+Nxe4 and White faces considerable difficulties.} Nxc3 {At
+first glance, one might think that this move only helps White
+create a stronger pawn center; however, Fischer's plan is
+quite the opposite. By eliminating the Knight on c3, it
+becomes possible to sacrifice the exchange via Nxe4 and smash
+White's center, while the King remains trapped in the center.}
+13. bxc3 Nxe4 {The natural continuation of Black's plan.}
+14. Bxe7 Qb6 15. Bc4 Nxc3 16. Bc5 Rfe8+ 17. Kf1 Be6 {!! If
+this is the game of the century, then 17...Be6!! must be the
+counter of the century. Fischer offers his queen in exchange
+for a fierce attack with his minor pieces. Declining this
+offer is not so easy: 18. Bxe6 leads to a 'Philidor Mate'
+(smothered mate) with ...Qb5+ 19. Kg1 Ne2+ 20. Kf1 Ng3+
+21. Kg1 Qf1+ 22. Rxf1 Ne2#. Other ways to decline the queen
+also run into trouble: e.g., 18. Qxc3 Qxc5} 18. Bxb6 Bxc4+
+19. Kg1 Ne2+ 20. Kf1 Nxd4+ {This tactical scenario, where a
+king is repeatedly revealed to checks, is sometimes called a
+"windmill."} 21. Kg1 Ne2+ 22. Kf1 Nc3+ 23. Kg1 axb6 24. Qb4
+Ra4 25. Qxb6 Nxd1 26. h3 Rxa2 27. Kh2 Nxf2 28. Re1 Rxe1
+29. Qd8+ Bf8 30. Nxe1 Bd5 31. Nf3 Ne4 32. Qb8 b5 {Every piece
+and pawn of the black camp is defended. The white queen has
+nothing to do.} 33. h4 h5 34. Ne5 Kg7 35. Kg1 Bc5+ 36. Kf1
+Ng3+ {Now Byrne is hopelessly entangled in Fischer's mating
+net.} 37. Ke1 Bb4+ 38. Kd1 Bb3+ 39. Kc1 Ne2+ 40. Kb1 Nc3+
+41. Kc1 Rc2# 0-1