Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup FEN generation #150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions board.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ import (
"bytes"
"encoding/binary"
"errors"
"strconv"
"strings"
)

var fenReplacer = strings.NewReplacer(
"11111111", "8",
"1111111", "7",
"111111", "6",
"11111", "5",
"1111", "4",
"111", "3",
"11", "2",
)

// A Board represents a chess board and its relationship between squares and pieces.
type Board struct {
bbWhiteKing bitboard
Expand Down Expand Up @@ -128,27 +137,23 @@ func (b *Board) Draw() string {
// String implements the fmt.Stringer interface and returns
// a string in the FEN board format: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
func (b *Board) String() string {
fen := ""
var fenBuilder strings.Builder
for r := 7; r >= 0; r-- {
for f := 0; f < numOfSquaresInRow; f++ {
sq := NewSquare(File(f), Rank(r))
p := b.Piece(sq)
if p != NoPiece {
fen += p.getFENChar()
fenBuilder.WriteString(p.getFENChar())
} else {
fen += "1"
fenBuilder.WriteString("1")
}
}
if r != 0 {
fen += "/"
fenBuilder.WriteString("/")
}
}
for i := 8; i > 1; i-- {
repeatStr := strings.Repeat("1", i)
countStr := strconv.Itoa(i)
fen = strings.Replace(fen, repeatStr, countStr, -1)
}
return fen

return fenReplacer.Replace(fenBuilder.String())
}

// Piece returns the piece for the given square.
Expand Down
38 changes: 29 additions & 9 deletions board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,37 @@ import (
)

func TestBoardTextSerialization(t *testing.T) {
fen := "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
b := &Board{}
if err := b.UnmarshalText([]byte(fen)); err != nil {
t.Fatal("recieved unexpected error", err)
for _, fen := range []string{
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR",
"rnbqkb1r/pp1p1np1/4pp2/2pP3p/8/2N2NP1/PPP1PP1P/R1BQKBR1",
} {
t.Run(fen, func(t *testing.T) {
b := &Board{}
if err := b.UnmarshalText([]byte(fen)); err != nil {
t.Fatal("recieved unexpected error", err)
}
txt, err := b.MarshalText()
if err != nil {
t.Fatal("recieved unexpected error", err)
}
if fen != string(txt) {
t.Fatalf("fen expected board string %s but got %s", fen, string(txt))
}
})
}
txt, err := b.MarshalText()
if err != nil {
t.Fatal("recieved unexpected error", err)

}

func BenchmarkBoardTextSerialization(b *testing.B) {
fen := "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
board := &Board{}
if err := board.UnmarshalText([]byte(fen)); err != nil {
b.Fatal("recieved unexpected error", err)
}
if fen != string(txt) {
t.Fatalf("fen expected board string %s but got %s", fen, string(txt))

b.ResetTimer()
for i := 0; i < b.N; i++ {
board.String()
}
}

Expand Down
9 changes: 9 additions & 0 deletions fen.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,18 @@ var (
"n": BlackKnight,
"p": BlackPawn,
}
pieceFenMap = reversePieceMap()

fenTurnMap = map[string]Color{
"w": White,
"b": Black,
}
)

func reversePieceMap() map[Piece]string {
res := make(map[Piece]string)
for k, v := range fenPieceMap {
res[v] = k
}
return res
}
9 changes: 4 additions & 5 deletions piece.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,9 @@ var (
)

func (p Piece) getFENChar() string {
for key, piece := range fenPieceMap {
if piece == p {
return key
}
res, ok := pieceFenMap[p]
if !ok {
return ""
}
return ""
return res
}