forked from notnil/chess
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fen.go
170 lines (162 loc) · 4.11 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
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 notiation %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[getSquare(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) {
if len(castleStr) > 4 {
return "-", fmt.Errorf("chess: fen invalid castle rights %s", castleStr)
}
// check for duplicates aka. KKkq right now is valid
for _, s := range []string{"K", "Q", "k", "q", "-", "A", "B", "C", "D", "E", "F", "G", "H", "a", "b", "c", "d", "e", "f", "g", "h"} {
if strings.Count(castleStr, s) > 1 {
return "-", fmt.Errorf("chess: fen invalid castle rights %s", castleStr)
}
}
upperCount := 0
lowerCount := 0
otherCount := 0
for _, r := range castleStr {
c := fmt.Sprintf("%c", r)
switch c {
case "K", "Q", "A", "B", "C", "D", "E", "F", "G", "H":
upperCount += 1
case "k", "q", "a", "b", "c", "d", "e", "f", "g", "h":
lowerCount += 1
case "-":
otherCount += 1
default:
return "-", fmt.Errorf("chess: fen invalid castle rights %s", castleStr)
}
}
if upperCount > 2 || lowerCount > 2 || otherCount > 1 || (otherCount == 1 && len(castleStr) != 1) {
return "-", fmt.Errorf("chess: fen invalid castle rights %s", castleStr)
}
// TODO: Verify other illegal combinations, like QA or KH
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 (
fenSkipMap = map[int]string{
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
}
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,
}
)