fathom

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2023 License: MIT Imports: 5 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-fathom

Go Reference

Syzygy tablebase probe implementation for Go.

This package wraps a fork of an original Fathom tool written in C: https://github.com/jdart1/Fathom.

Also it uses well-known external package https://github.com/notnil/chess as a public communication layer.

Interface methods allow to probe DTZ and WDL tables (*.rtbw and *.rtbz Syzygy tablebases).

Tablebase files

If you don't have tablebase files yet, follow this link and explore suggested TXT. These TXT files contain URLs to tablebase files you should download.

Example: play chess.Game

We are going to call fathom.ProbeRoot(...) and apply the suggested move in a loop until the game is over.

package main

import (
	"fmt"

	gofathom "github.com/bbars/go-fathom"
	"github.com/notnil/chess"
)

func main() {
	fen := "4k3/7q/8/8/8/8/B61/4K3 b - - 0 1"
	
	fathom, err := gofathom.NewFathom("./path/to/tables/chess/")
	if err != nil {
		panic(err)
	}
	
	fenOption, err := chess.FEN(fen)
	if err != nil {
		panic(err)
	}
	game := chess.NewGame(fenOption)
	game.AddTagPair("FEN", fen)
	fmt.Printf("FEN: %q\n\n", game.Position())
	
	// generate moves until game is over
	for game.Outcome() == chess.NoOutcome {
		tbMove, _, err := fathom.ProbeRoot(game.Position())
		if err != nil {
			panic(err)
		}
		// select a best move
		move := tbMove.Move()
		fmt.Printf("%s move %s -> ", game.Position().Turn(), &move)
		err = game.Move(&move)
		if err != nil {
			panic(err)
		}
		fmt.Printf("%s\n", game.Position())
	}
	// print outcome and game PGN
	fmt.Println(game.Position().Board().Draw())
	fmt.Printf("Game completed. %s by %s.\n\n", game.Outcome(), game.Method())
	fmt.Println("PGN:\n")
	fmt.Println(game.String())
}

Example: call all available funcs

Here we are going to invoke all available funcs to analyze the state.

package main

import (
	"fmt"

	gofathom "github.com/bbars/go-fathom"
	"github.com/notnil/chess"
)

func main() {
	fen := "4k3/7q/8/8/8/8/B61/4K3 b - - 0 1"
	
	fathom, err := gofathom.NewFathom("./path/to/tables/chess/")
	if err != nil {
		panic(err)
	}
	
	pos := &chess.Position{}
	err = pos.UnmarshalText([]byte(fen))
	if err != nil {
		panic(err)
	}
	
	fmt.Println("# fathom.ProbeWDL")
	res1, _ := fathom.ProbeWDL(pos)
	fmt.Printf("%#v = %s\n", res1, res1)
	
	fmt.Println("\n# fathom.ProbeRoot")
	tbMove, res2, _ := fathom.ProbeRoot(pos)
	move := tbMove.Move()
	fmt.Printf("move: %#v\tstring: %q\n", move, &move)
	fmt.Println("move#\tWDL\tDTZ")
	for _, tbRes := range res2 {
		move := tbRes.Move()
		fmt.Printf("%s\t%s\t%v\n", move.String(), tbRes.WDL(), tbRes.DTZ())
	}
	
	fmt.Println("\n# fathom.ProbeRootDTZ")
	res3, _ := fathom.ProbeRootDTZ(pos, true)
	fmt.Println("move#\tRank\tScore\tPV")
	for _, tbRes := range res3 {
		move := tbRes.Move()
		fmt.Printf("%s\t%v\t%v\t%v\n", move.String(), tbRes.Rank(), tbRes.Score(), tbRes.PV())
	}
	
	fmt.Println("\n# fathom.ProbeRootWDL")
	fmt.Println("move#\tRank\tScore\tPV")
	res4, _ := fathom.ProbeRootWDL(pos, true)
	for _, tbRes := range res4 {
		move := tbRes.Move()
		fmt.Printf("%s\t%v\t%v\t%v\n", move.String(), tbRes.Rank(), tbRes.Score(), tbRes.PV())
	}
}

Documentation

Overview

Package fathom is a Syzygy tablebase probe implementation.

This package wraps a fork of an original Fathom tool written in C: https://github.com/jdart1/Fathom.

Also it uses well-known external package https://github.com/notnil/chess as a public communication layer.

Methods allow to probe DTZ and WDL tables (*.rtbw and *.rtbz Syzygy tablebases).

Index

Constants

View Source
const (
	WDLUnknown  = WDL(-1)
	Loss        = WDL(C.TB_LOSS)
	BlessedLoss = WDL(C.TB_BLESSED_LOSS)
	Draw        = WDL(C.TB_DRAW)
	CursedWin   = WDL(C.TB_CURSED_WIN)
	Win         = WDL(C.TB_WIN)
)

Variables

View Source
var ErrCheckmate = errors.New("go-fathom: checkmate")

ErrCheckmate unable to dig, because the game is over: checkmate

View Source
var ErrNoTablebases = errors.New("go-fathom: no tablebase files are found")

ErrNoTablebases no tablebase files are found within specified directory

View Source
var ErrStalemate = errors.New("go-fathom: stalemate")

ErrStalemate unable to dig, because the game is over: stalemate

Functions

This section is empty.

Types

type Fathom

type Fathom interface {
	// ProbeWDL probes the win-draw-loss (WDL) table.
	//
	// Returns one of: [Loss], [BlessedLoss], [Draw], [CursedWin] or [Win].
	// When error occurs, WDL results in [WDLUnknown].
	//
	// NOTES:
	// - Engines should use this function during search.
	ProbeWDL(chessPosition *chess.Position) (WDL, error)

	// ProbeRoot probes the Distance-To-Zero (DTZ) table.
	//
	// The suggested move is guaranteed to preserve the WDL value.
	// Possible errors: [ErrCheckmate], [ErrStalemate] (and other).
	//
	// NOTES:
	// - Engines can use this function to probe at the root. This function should
	//   not be used during search.
	// - DTZ tablebases can suggest unnatural moves, especially for losing
	//   positions. Engines may prefer to perform traditional search combined with WDL
	//   move filtering using the alternative results array.
	// - This function is NOT thread safe. For engines this function should only
	//   be called once at the root per search.
	ProbeRoot(chessPosition *chess.Position) (TbMove, []TbResult, error)

	// ProbeRootDTZ uses the DTZ tables to rank and score all root moves.
	ProbeRootDTZ(chessPosition *chess.Position, useRule50 bool) ([]TbRootMove, error)

	// ProbeRootWDL uses the WDL tables to rank and score all root moves.
	//
	// NOTES:
	// - This is a fallback for the case that some or all DTZ tables are missing.
	ProbeRootWDL(chessPosition *chess.Position, useRule50 bool) ([]TbRootMove, error)

	// Close frees any resources allocated by an instance.
	Close()
}

func NewFathom

func NewFathom(tbDir string) (Fathom, error)

NewFathom creates a new Fathom reader. If no tablebase files are found, then error is returned.

Possible errors: ErrNoTablebases

If you don't have tablebase files yet, follow this link (https://github.com/niklasf/shakmaty-syzygy/tree/master/tables) and explore suggested TXT. These TXT files contain URLs to tablebase files you should download.

type TbMove

type TbMove interface {
	Move() chess.Move
}

type TbResult

type TbResult interface {
	TbMove

	WDL() WDL
	DTZ() int
}

TbResult is a result value comprising: suggested move, the WDL value and the DTZ value.

type TbRootMove

type TbRootMove interface {
	TbMove

	// PV stands for Principal Variation
	PV() []chess.Move
	Score() int
	Rank() int
}

TbRootMove suggests a move, a rank, a score, and a predicted principal variation.

type WDL

type WDL int

WDL win-draw-lose result status.

func (WDL) MarshalJSON added in v0.9.1

func (wdl WDL) MarshalJSON() ([]byte, error)

func (WDL) String

func (wdl WDL) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址