-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUci2Chess.hs
76 lines (71 loc) · 2.49 KB
/
Uci2Chess.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module Uci2Chess where
import Chess
import UCI
import Data.Word
import Data.Maybe
import Data.List
import FEN
columnCharToWord :: Char -> Word8
columnCharToWord c = (toEnum . fromJust)(elemIndex c "abcdefgh")
runUciPosition' :: UciQuery -> Board
runUciPosition' (UciPosition p moves) = foldl' (\b m ->
doMove (convertMove b m) b
) (convertPosition p) moves
runUciPosition :: UciPos -> [UciMove] -> Board
runUciPosition p moves = foldl' (\b m ->
doMove (convertMove b m) b
) (convertPosition p) moves
convertMove :: Board -> UciMove -> Move
convertMove board (UciNormalMove x1 y1 x2 y2) =
let
yy1 = (toEnum $ 8-y1)::Word8
xx1 = (columnCharToWord x1)::Word8
yy2 = (toEnum $ 8-y2)::Word8
xx2 = (columnCharToWord x2)::Word8
piece = getPiece (xx1,yy1) board
in (piece, (xx1,yy1), (xx2, yy2))
convertMove board (UciPromotionMove x1 y1 x2 y2 p) =
let
yy1 = (toEnum $ 8-y1)::Word8
xx1 = (columnCharToWord x1)::Word8
yy2 = (toEnum $ 8-y2)::Word8
xx2 = (columnCharToWord x2)::Word8
piece = case (whoseTurn board, p) of
(True, 'q') -> wQ
(True, 'r') -> wR
(True, 'b') -> wB
(True, 'n') -> wN
(False, 'q') -> bQ
(False, 'r') -> bR
(False, 'b') -> bB
(False, 'n') -> bN
_ -> 0
in (piece, (xx1,yy1), (xx2, yy2))
convertMove board UciNullmove = error "NULL MOVE"
convertPosition :: UciPos -> Board
convertPosition UciStartpos = startingBoard
convertPosition (UciFen s1 s2 s3 s4 s5 s6) = splitFenToBoard s1 s2 s3 s4 s5 s6
moveToUci :: Move -> Board -> String
moveToUci (piece, (x1,y1), (x2,y2)) board =
("abcdefgh" !! fromEnum x1) :
("87654321" !! fromEnum y1) :
("abcdefgh" !! fromEnum x2) :
("87654321" !! fromEnum y2) :
if piece == getPiece (x1,y1) board
then ""
else (" pnbrqk pnbrqk" !! fromEnum piece) : ""
uciMovesToPgn string = helper 1 startingBoard (parseUciMoves string)
where
helper i board [] = ""
helper i board (ply:[]) = show i ++ ". " ++ showMoveInContext board (convertMove board ply)
helper i board (wply:bply:restOfMoves) =
let
cwply = convertMove board wply
board' = doMove cwply board
cbply = convertMove board' bply
board'' = doMove cbply board'
in
show i ++ ". " ++
showMoveInContext board cwply ++ " " ++
showMoveInContext board' cbply ++ " " ++
helper (i+1) board'' restOfMoves