opening

package
v3.0.0-beta.4 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 9 Imported by: 0

README

opening

opening provides interactivity to opening books such as Encyclopaedia of Chess Openings (ECO) which is loadable from the package. Source: https://github.com/lichess-org/chess-openings

Visual

Advance Variation subtree of the French Defense:

subtree

Example

package main

import (
    "fmt"

    "github.com/corentings/chess"
    "github.com/corentings/chess/opening"
)

func main(){
    g := chess.NewGame()
	g.MoveText("e4", chess.SAN(), nil)
	g.MoveText("e6", chess.SAN(), nil)

	// print French Defense
	book := opening.NewBookECO()
	o := book.Find(g.Moves())
	fmt.Println(o.Title())
}

Documentation

Overview

Package opening implements chess opening determination and exploration.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Book

type Book interface {
	// Find returns the most specific opening for the list of moves. If no opening is found, Find returns nil.
	// Use Find for performance-sensitive opening detection paths.
	Find(moves []chess.Move) *Entry
	// Possible returns the possible openings after the moves given. If moves is empty or nil all openings are returned.
	// Use Possible for performance-sensitive opening exploration paths.
	Possible(moves []chess.Move) []*Entry
}

Book is an opening book that returns openings for move sequences.

type BookECO

type BookECO struct {
	// contains filtered or unexported fields
}

BookECO represents the Encyclopedia of Chess Openings https://en.wikipedia.org/wiki/Encyclopaedia_of_Chess_Openings BookECO is safe for concurrent use.

func DefaultBook

func DefaultBook() (*BookECO, error)

DefaultBook returns the standard ECO opening book. The book is parsed lazily on first call and cached for subsequent calls. It is safe for concurrent use.

Example (Find)
package main

import (
	"fmt"

	"github.com/corentings/chess/v3"
	"github.com/corentings/chess/v3/opening"
)

func main() {
	g := chess.NewGame()
	_, _ = g.MoveText("e4", chess.SAN(), nil)
	_, _ = g.MoveText("e6", chess.SAN(), nil)

	// print French Defense
	book, err := opening.DefaultBook()
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	o := book.Find(g.Moves())
	fmt.Println(o.Title())

}
Output:
French Defense
Example (Possible)
package main

import (
	"fmt"

	"github.com/corentings/chess/v3"
	"github.com/corentings/chess/v3/opening"
)

func main() {
	g := chess.NewGame()
	_, _ = g.MoveText("e4", chess.SAN(), nil)
	_, _ = g.MoveText("d5", chess.SAN(), nil)

	// print all variantions of the Scandinavian Defense
	book, err := opening.DefaultBook()
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	for _, o := range book.Possible(g.Moves()) {
		if o.Title() == "Scandinavian Defense" {
			fmt.Println(o.Title())
		}
	}

}
Output:
Scandinavian Defense
Scandinavian Defense

func NewBook

func NewBook(r io.Reader) (*BookECO, error)

NewBook creates a new opening book from an ECO TSV reader. Use this for custom opening data or when you need isolation from the default book. NewBook validates the input during construction so malformed books fail before use. Entry.Game returns a caller-owned clone of the cached game.

func (*BookECO) Find

func (b *BookECO) Find(moves []chess.Move) *Entry

Find implements the Book interface. Use Find for performance-sensitive opening detection paths.

func (*BookECO) Possible

func (b *BookECO) Possible(moves []chess.Move) []*Entry

Possible implements the Book interface. Use Possible for performance-sensitive opening exploration paths.

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

A Entry represents a specific sequence of moves from the starting position.

func (*Entry) Code

func (o *Entry) Code() string

Code returns the Encyclopaedia of Chess Openings (ECO) code.

func (*Entry) Game

func (o *Entry) Game() *chess.Game

Game returns the opening as a caller-owned game.

The game is pre-computed when the opening is constructed (per ADR-005), so Game does not replay UCI moves. Each call returns an independent clone so callers may mutate the returned game without affecting the opening book.

func (*Entry) PGN

func (o *Entry) PGN() string

PGN returns the opening in PGN format.

func (*Entry) Title

func (o *Entry) Title() string

Title returns the Encyclopaedia of Chess Openings (ECO) title of the opening.

Jump to

Keyboard shortcuts

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