-
Notifications
You must be signed in to change notification settings - Fork 0
/
fen.go
240 lines (205 loc) · 6.21 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package chesskimo
import (
"errors"
"strconv"
"strings"
)
var (
// ErrFENFieldsInvalid indicates that a FEN record has invalid fields encoding.
ErrFENFieldsInvalid = errors.New("FEN has invalid fields encoding")
// ErrFENRanksInvalid indicates that a FEN record has an invalid encoding.
ErrFENRanksInvalid = errors.New("FEN has invalid rank encoding")
// ErrFENColorInvalid indicates that a FEN record has an invalid color encoding.
ErrFENColorInvalid = errors.New("FEN has invalid color field")
// ErrFENSquareInvalid indicates that a FEN record contains an invalid square.
ErrFENSquareInvalid = errors.New("FEN has invalid square")
// ErrFENMoveNumInvalid indicates that a FEN record contains an invalid move number.
ErrFENMoveNumInvalid = errors.New("FEN has invalid move number")
// FENMap maps FEN piece symbols (plus empty ' ') to the internal definition.
FENMap = map[rune]Piece{
'P': WPAWN,
'N': WKNIGHT,
'B': WBISHOP,
'R': WROOK,
'Q': WQUEEN,
'K': WKING,
'p': BPAWN,
'n': BKNIGHT,
'b': BBISHOP,
'r': BROOK,
'q': BQUEEN,
'k': BKING,
' ': EMPTY,
}
)
// // ValidateFEN validates a FEN string.
// TODO
// func ValidateFEN(fen string) bool {
// // Must have 6 fields, split by ONE space.
// fields := strings.Split(fen, " ")
// if len(fields) != 6 {
// return ErrFENFieldsInvalid
// }
// // Field 0 contains the pieces setup of the board.
// // Must have 8 ranks and only specific characters are allowed inside.
// rank := "[prnbqkPRNBQK12345678]+"
// pattern := fmt.Sprintf("(%s)/(%s)/(%s)/(%s)/(%s)/(%s)/(%s)/(%s)", rank, rank, rank, rank, rank, rank, rank, rank)
// reg := regexp.MustCompile(pattern)
// return true
// }
func ParseFEN(fen string) (MinBoard, error) {
mb := NewMinBoard()
// Extract fields.
fields, err := splitFENFields(fen)
if err != nil {
return mb, err
}
// Extract piece positions.
pieces, err := parseFENPieces(fields[0])
if err != nil {
return mb, err
}
// Extract active color.
color, err := parseFENColor(fields[1])
if err != nil {
return mb, err
}
// Extract castling rights.
short, long := parseFENCastlingRights(fields[2])
// Extract en passent target square.
epSq, err := parseFENEnPassent(fields[3])
if err != nil {
return mb, err
}
// Extract half moves since last capture or pawn movement.
halfMoves, err := parseFENMoveNumber(fields[4])
if err != nil {
return mb, err
}
// Extract full move number.
moveNum, err := parseFENMoveNumber(fields[5])
if err != nil {
return mb, err
}
mb.Squares = pieces
mb.Color = color
mb.CastleShort = short
mb.CastleLong = long
mb.EpSquare = epSq
mb.HalfMoves = halfMoves
mb.MoveNum = moveNum
return mb, nil
}
// SplitFields splits a FEN into its fields and returns them separated into a slice,
// or an error if the amount of fields is not equal 6.
func splitFENFields(fen string) ([]string, error) {
// Split for any number of whitespaces. Is fault tolerant to some malformed FENs.
fields := strings.Fields(fen)
if len(fields) != 6 {
return nil, ErrFENFieldsInvalid
}
return fields, nil
}
// ParsePieces parses the 'pieces' string, which is the first
// field of a FEN record and returns a simple board as slice.
// The returned board is a 1D slice and has the following structure:
//
// a b c d e f g h
// +----------------
// 8|56,.. 63] <- array index 63 is black rook @ H8
// 7|48,..
// 6|40,..
// 5|32,..
// 4|24,..
// 3|16,..
// 2| 8,..
// 1|[0,1,2,3,4,5,6,7, <- array index 0 is white rook @ A1
// +-----------------
//
func parseFENPieces(pieces string) ([64]Piece, error) {
board := [64]Piece{}
// Reverse ranks to transform from FEN to internal representation.
ranks := strings.Split(pieces, "/")
if len(ranks) != 8 {
return board, ErrFENRanksInvalid
}
ranks[0], ranks[7] = ranks[7], ranks[0]
ranks[1], ranks[6] = ranks[6], ranks[1]
ranks[2], ranks[5] = ranks[5], ranks[2]
ranks[3], ranks[4] = ranks[4], ranks[3]
// Put it back together so old code below can just continue to work.
pieces = ranks[0] + ranks[1] + ranks[2] + ranks[3] + ranks[4] + ranks[5] + ranks[6] + ranks[7]
// Replace numbers with an equal amount of spaces
// so we have 64 characters in total, mapping to
// a whole board of chess.
for i := 0; i <= 8; i++ {
// replace all possibilities of numbers (1-8)
pieces = strings.Replace(pieces, strconv.Itoa(i), strings.Repeat(" ", i), -1)
}
// Check if resulting board is valid in its size.
if len(pieces) != 64 {
return board, ErrFENRanksInvalid
}
for i, r := range pieces {
// Test if the FEN code contains a valid piece symbol at position 'i'.
if piece, ok := FENMap[rune(r)]; ok {
board[i] = piece
} else {
// Unknown symbol..
return board, ErrFENRanksInvalid
}
}
return board, nil
}
// ParseColor parses the active color from the 'color' string.
func parseFENColor(color string) (Color, error) {
// color = strings.ToLower(color)
switch color {
case "w":
return WHITE, nil
case "b":
return BLACK, nil
default:
return 0, ErrFENColorInvalid
}
}
// ParseCastlingRights parses which player still has rights to castle short and long.
// The returned arrays describe the castling rights as follows:
// short:[BLACK, WHITE], long:[BLACK, WHITE]
func parseFENCastlingRights(castle string) ([2]bool, [2]bool) {
short := [2]bool{strings.Contains(castle, "k"), strings.Contains(castle, "K")}
long := [2]bool{strings.Contains(castle, "q"), strings.Contains(castle, "Q")}
// Invalid characters are ignored and treated as if no player had castling rights.
return short, long
}
// ParseEnPassent parses the current possible en passent capture square, if there is one.
func parseFENEnPassent(ep string) (Square, error) {
if ep == "-" {
return OTB, nil
}
sq, err := parseFENSquare(ep)
return sq, err
}
// ParseMoveNumber parses a move number from the 'num' string.
func parseFENMoveNumber(num string) (uint16, error) {
n, err := strconv.ParseInt(num, 10, 16)
if err != nil {
return 0, err
}
if n < 0 {
return 0, ErrFENMoveNumInvalid
}
return uint16(n), nil
}
func parseFENSquare(sq string) (Square, error) {
if len(sq) != 2 {
return OTB, ErrFENSquareInvalid
}
r, f := sq[0], sq[1]
rank, file := Square(r)-97, Square(f)-49
idx := Square(rank + file*8)
if idx < 0 || idx > 63 {
return OTB, ErrFENSquareInvalid
}
return idx, nil
}