World Conquest Chronicles

World Conquest Chronicles

go-chess-models, v1.7.1

The library that implements checking and generating of chess moves.

Moving the chessmodels.perft() function from the tests to the main code; adding the go-chess-perft tool and the example for the chessmodels.MoveGenerator.MovesForColor() method; bug fixing.

Change Log

  • new features:
    • transform the chessmodels.Board.CheckMove() method to an independent function;
    • the chessmodels.Perft() function:
      • move the chessmodels.perft() function from the tests to the main code;
      • pass a handler to all levels in the chessmodels.Perft() function;
      • pass a move generator to the chessmodels.Perft() function via an interface;
    • add the go-chess-perft tool:
      • counting all possible moves:
        • parameters:
          • position;
          • color that moves first;
          • analysis deep;
      • profiling:
        • targets:
          • CPU usage;
          • memory usage;
        • storing the results to a file;
  • bug fixing:
    • fix rune conversion from an integer to a string in the uci.EncodePosition() function;
    • fix incorrect calculation of the last file in the uci.EncodePieceStorage() function;
  • refactoring:
    • simplify the utility functions in the pieces package;
    • merge the chessmodels.pieceGroup type with the chessmodels.Board structure;
  • unit testing:
    • complete the tests:
      • of the pieces.NewPiece() function;
      • of the uci.DecodePiece() function;
      • of the uci.EncodePiece() function;
    • add the long-playing tests to the Travis CI configuration;
  • examples:
    • remove the examples:
      • remove the examples with a mock piece;
      • remove the redundant examples;
    • add the example for the chessmodels.MoveGenerator.MovesForColor() method.

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;
  • perft function;
  • using an abstraction of a piece;
  • Forsyth–Edwards Notation:
    • parsing:
      • of a position;
      • of a move;
      • of a piece kind;
      • of a piece color;
      • of a board;
    • serialization:
      • of a position;
      • of a move;
      • of a piece kind;
      • of a piece color;
      • of a board;
  • utilities:
    • utility for counting all possible moves (based on the perft function):
      • counting all possible moves:
        • parameters:
          • position;
          • color that moves first;
          • analysis deep;
      • profiling:
        • targets:
          • CPU usage;
          • memory usage;
        • storing the results to a file.

Examples

chessmodels.MoveGenerator.MovesForColor():

package main

import (
    "fmt"
    "sort"

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

func main() {
    board := models.NewBoard(models.Size{Width: 5, Height: 5}, []models.Piece{
        pieces.NewRook(models.Black, models.Position{File: 2, Rank: 2}),
        pieces.NewKnight(models.White, models.Position{File: 3, Rank: 3}),
        pieces.NewPawn(models.White, models.Position{File: 4, Rank: 3}),
    })

    var generator models.MoveGenerator
    moves, _ := generator.MovesForColor(board, models.White)

    // sorting only by the final point will be sufficient for the reproducibility
    // of this example
    sort.Slice(moves, func(i int, j int) bool {
        a, b := moves[i].Finish, moves[j].Finish
        if a.File == b.File {
            return a.Rank < b.Rank
        }

        return a.File < b.File
    })

    for _, move := range moves {
        fmt.Printf("%+v\n", move)
    }

    // Output:
    // {Start:{File:3 Rank:3} Finish:{File:1 Rank:2}}
    // {Start:{File:3 Rank:3} Finish:{File:1 Rank:4}}
    // {Start:{File:3 Rank:3} Finish:{File:2 Rank:1}}
    // {Start:{File:3 Rank:3} Finish:{File:4 Rank:1}}
    // {Start:{File:4 Rank:3} Finish:{File:4 Rank:4}}
}

Installation of the go-chess-perft Tool

$ go get github.com/thewizardplusplus/go-chess-models/cmd/go-chess-perft

Usage of the go-chess-perft Tool

$ go-chess-perft -h | -help | --help
$ go-chess-perft [options]

Options:

  • -h, -help, --help — show the help message and exit;
  • -fen STRING — board in Forsyth–Edwards Notation (default: rnbqk/ppppp/5/PPPPP/RNBQK, i.e., Gardner's minichess);
  • -color {black|white} — color that moves first (default: white);
  • -deep INTEGER — analysis deep (default: 5);
  • -cpuProfile STRING — file for CPU profile writing;
  • -memoryProfile STRING — file for memory profile writing.

Repository

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

Content: code.

License: MIT.