go-chess-models, v1.3
Posted on

The library that implements checking and generating of chess moves.
Parsing models from Forsyth–Edwards Notation and improving moves checking.
Change Log
- parsing from Forsyth–Edwards Notation:
- of a piece kind;
- of a piece color;
- of a board;
- checking for king capture (including during moves generating);
- fixing the bug with checking bishop moves.
Features
- representing the board as an associative array of pieces with their positions as keys;
- immutable applicating moves to the board via copying the latter;
- checkings of moves:
- universal;
- individual for all types of pieces;
- generating moves via filtering from all possible ones;
- using an abstraction of a piece;
- parsing from Forsyth–Edwards Notation:
- of a piece kind;
- of a piece color;
- of a board.
Examples
chessmodels.ParseKind():
package main
import (
"fmt"
models "github.com/thewizardplusplus/go-chess-models"
)
func main() {
kind, _ := models.ParseKind('r')
fmt.Printf("%v\n", kind)
// Output: 2
}
chessmodels.ParseColor():
package main
import (
"fmt"
models "github.com/thewizardplusplus/go-chess-models"
)
func main() {
color := models.ParseColor('B')
fmt.Printf("%v\n", color)
// Output: 1
}
chessmodels.ParseBoard():
package main
import (
"fmt"
"sort"
models "github.com/thewizardplusplus/go-chess-models"
"github.com/thewizardplusplus/go-chess-models/pieces"
)
type ByPosition []models.Piece
func (group ByPosition) Len() int {
return len(group)
}
func (group ByPosition) Swap(i, j int) {
group[i], group[j] = group[j], group[i]
}
func (group ByPosition) Less(
i, j int,
) bool {
a := group[i].Position()
b := group[j].Position()
if a.File == b.File {
return a.Rank < b.Rank
}
return a.File < b.File
}
func main() {
const fen = "8/8/8/8/3B4/2r5/8/8"
storage, _ := models.ParseBoard(fen, pieces.NewPiece)
pieces := storage.Pieces()
sort.Sort(ByPosition(pieces))
fmt.Printf("%+v\n", pieces)
// Output: [{Base:{kind:2 color:0 position:{File:2 Rank:2}}} {Base:{kind:3 color:1 position:{File:3 Rank:3}}}]
}
Repository
Link: https://github.com/thewizardplusplus/go-chess-models/tree/v1.3.
Content: code.
License: MIT.