-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseFEN.hs
55 lines (45 loc) · 1.56 KB
/
ParseFEN.hs
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module ParseFEN (
initialFen,
parseFen
) where
import Chess
import qualified Data.Char as Char
import qualified Data.Map as Map
initialFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
parsePos [x, y] = (Char.ord x - Char.ord 'a' + 1, read [y] :: Int)
parsePiece 'P' = Piece White Pawn
parsePiece 'N' = Piece White Knight
parsePiece 'B' = Piece White Bishop
parsePiece 'R' = Piece White Rook
parsePiece 'Q' = Piece White Queen
parsePiece 'K' = Piece White King
parsePiece 'p' = Piece Black Pawn
parsePiece 'n' = Piece Black Knight
parsePiece 'b' = Piece Black Bishop
parsePiece 'r' = Piece Black Rook
parsePiece 'q' = Piece Black Queen
parsePiece 'k' = Piece Black King
parseFenBoard board = parse board 1 8
where parse [] _ _ = Map.empty
parse (c:cs) x y
| c == '/' = parse cs 1 (y - 1)
| Char.isDigit c = parse cs (x + read [c]) y
| otherwise =
Map.insert (x, y) (parsePiece c) $ parse cs (x + 1) y
parseFenTurn "w" = White
parseFenTurn "b" = Black
parseFenCastle = concatMap castlePos
where castlePos 'K' = [(8, 1)]
castlePos 'Q' = [(1, 1)]
castlePos 'k' = [(8, 8)]
castlePos 'q' = [(1, 8)]
castlePos '-' = []
parseFenEnPassant "-" = Nothing
parseFenEnPassant pos = Just (parsePos pos)
parseFen :: String -> State
parseFen str =
let board : turn : castle : enPassant : _ = words str
in State (parseFenBoard board)
(parseFenTurn turn)
(parseFenCastle castle)
(parseFenEnPassant enPassant)