World Conquest Chronicles

World Conquest Chronicles

go-chess-models, v1.5

The library that implements checking and generating of chess moves.

Extracting working with Forsyth–Edwards Notation to the separate package and refactoring.

Change Log

Examples

uci.DecodePiece():

package main

import (
    "fmt"

    "github.com/thewizardplusplus/go-chess-models/pieces"
    "github.com/thewizardplusplus/go-chess-models/uci"
)

func main() {
    piece, _ := uci.DecodePiece('B', pieces.NewPiece)
    fmt.Printf("%+v\n", piece)

    // Output: {Base:{kind:3 color:1 position:{File:0 Rank:0}}}
}

uci.EncodePiece():

package main

import (
    "fmt"

    models "github.com/thewizardplusplus/go-chess-models"
    "github.com/thewizardplusplus/go-chess-models/pieces"
    "github.com/thewizardplusplus/go-chess-models/uci"
)

func main() {
    fen := uci.EncodePiece(pieces.NewBishop(models.White, models.Position{}))
    fmt.Printf("%v\n", fen)

    // Output: B
}

uci.DecodePieceStorage():

package main

import (
    "fmt"
    "sort"

    models "github.com/thewizardplusplus/go-chess-models"
    "github.com/thewizardplusplus/go-chess-models/pieces"
    "github.com/thewizardplusplus/go-chess-models/uci"
)

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, _ := uci.DecodePieceStorage(fen, pieces.NewPiece, models.NewBoard)
    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}}}]
}

uci.EncodePieceStorage():

package main

import (
    "fmt"

    models "github.com/thewizardplusplus/go-chess-models"
    "github.com/thewizardplusplus/go-chess-models/pieces"
    "github.com/thewizardplusplus/go-chess-models/uci"
)

func main() {
    board := models.NewBoard(models.Size{Width: 5, Height: 5}, []models.Piece{
        pieces.NewRook(models.Black, models.Position{File: 2, Rank: 2}),
        pieces.NewBishop(models.White, models.Position{File: 3, Rank: 3}),
    })
    fen := uci.EncodePieceStorage(board)
    fmt.Printf("%v\n", fen)

    // Output: 5/3B1/2r2/5/5
}

Repository

Link: https://github.com/thewizardplusplus/go-chess-models/tree/v1.5.

Content: code.

License: MIT.