forked from notnil/chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fen.go
145 lines (137 loc) · 3.39 KB
/
fen.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package chess
import (
"fmt"
"strconv"
"strings"
)
// Decodes FEN notation into a GameState. An error is returned
// if there is a parsing error. FEN notation format:
// rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
func decodeFEN(fen string) (*Position, error) {
fen = strings.TrimSpace(fen)
parts := strings.Split(fen, " ")
if len(parts) != 6 {
return nil, fmt.Errorf("chess: fen invalid notation %s must have 6 sections", fen)
}
b, err := fenBoard(parts[0])
if err != nil {
return nil, err
}
turn, ok := fenTurnMap[parts[1]]
if !ok {
return nil, fmt.Errorf("chess: fen invalid turn %s", parts[1])
}
rights, err := formCastleRights(parts[2])
if err != nil {
return nil, err
}
sq, err := formEnPassant(parts[3])
if err != nil {
return nil, err
}
halfMoveClock, err := strconv.Atoi(parts[4])
if err != nil || halfMoveClock < 0 {
return nil, fmt.Errorf("chess: fen invalid half move clock %s", parts[4])
}
moveCount, err := strconv.Atoi(parts[5])
if err != nil || moveCount < 1 {
return nil, fmt.Errorf("chess: fen invalid move count %s", parts[5])
}
return &Position{
board: b,
turn: turn,
castleRights: rights,
enPassantSquare: sq,
halfMoveClock: halfMoveClock,
moveCount: moveCount,
}, nil
}
// generates board from fen format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
func fenBoard(boardStr string) (*Board, error) {
rankStrs := strings.Split(boardStr, "/")
if len(rankStrs) != 8 {
return nil, fmt.Errorf("chess: fen invalid board %s", boardStr)
}
m := map[Square]Piece{}
for i, rankStr := range rankStrs {
rank := Rank(7 - i)
fileMap, err := fenFormRank(rankStr)
if err != nil {
return nil, err
}
for file, piece := range fileMap {
m[NewSquare(file, rank)] = piece
}
}
return NewBoard(m), nil
}
func fenFormRank(rankStr string) (map[File]Piece, error) {
count := 0
m := map[File]Piece{}
err := fmt.Errorf("chess: fen invalid rank %s", rankStr)
for _, r := range rankStr {
c := fmt.Sprintf("%c", r)
piece := fenPieceMap[c]
if piece == NoPiece {
skip, err := strconv.Atoi(c)
if err != nil {
return nil, err
}
count += skip
continue
}
m[File(count)] = piece
count++
}
if count != 8 {
return nil, err
}
return m, nil
}
func formCastleRights(castleStr string) (CastleRights, error) {
// check for duplicates aka. KKkq right now is valid
for _, s := range []string{"K", "Q", "k", "q", "-"} {
if strings.Count(castleStr, s) > 1 {
return "-", fmt.Errorf("chess: fen invalid castle rights %s", castleStr)
}
}
for _, r := range castleStr {
c := fmt.Sprintf("%c", r)
switch c {
case "K", "Q", "k", "q", "-":
default:
return "-", fmt.Errorf("chess: fen invalid castle rights %s", castleStr)
}
}
return CastleRights(castleStr), nil
}
func formEnPassant(enPassant string) (Square, error) {
if enPassant == "-" {
return NoSquare, nil
}
sq := strToSquareMap[enPassant]
if sq == NoSquare || !(sq.Rank() == Rank3 || sq.Rank() == Rank6) {
return NoSquare, fmt.Errorf("chess: fen invalid En Passant square %s", enPassant)
}
return sq, nil
}
var (
fenPieceMap = map[string]Piece{
"K": WhiteKing,
"Q": WhiteQueen,
"R": WhiteRook,
"B": WhiteBishop,
"N": WhiteKnight,
"P": WhitePawn,
"k": BlackKing,
"q": BlackQueen,
"r": BlackRook,
"b": BlackBishop,
"n": BlackKnight,
"p": BlackPawn,
}
fenTurnMap = map[string]Color{
"w": White,
"b": Black,
}
)