pgntools

package
v1.0.0-beta Latest Latest
Warning

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

Go to latest
Published: May 18, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

This package provides a number of simple services for accessing and handling chess games in PGN format

Index

Constants

View Source
const (
	BKING content = -6 + iota
	BQUEEN
	BROOK
	BBISHOP
	BKNIGHT
	BPAWN // -1: represent also black pieces
	BLANK //  0: empty
	WPAWN //  1: represent also white pieces
	WKNIGHT
	WBISHOP
	WROOK
	WQUEEN
	WKING
)

The different values of content are shown next. These symbols are intentionally exported so that other people can handle chess boards using the services provided by this package

Variables

This section is empty.

Functions

This section is empty.

Types

type PgnBoard

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

A PgnBoard consists simply of an array of 64 integers. In addition, the location of both kings has to be updated. This information is used to determine whether a piece is pinned or not

func NewPgnBoard

func NewPgnBoard() PgnBoard

Create a new board initialized with Caissa

func (PgnBoard) String

func (board PgnBoard) String() (output string)

show a graphical view of this chess board

func (*PgnBoard) UpdateBoard

func (board *PgnBoard) UpdateBoard(move PgnMove) (err error)

The following method updates the contents of the current board after making the given move. In case it is not possible to execute the given move, then an error is returned

type PgnCollection

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

A PgnCollection consists of an arbitrary number of PgnGames

func NewPgnCollection

func NewPgnCollection() PgnCollection

Return an empty collection of PGN games

func (*PgnCollection) Add

func (c *PgnCollection) Add(game PgnGame)

Add the given PgnGame to this collection

func (PgnCollection) Filter

func (c PgnCollection) Filter(expression string) (*PgnCollection, error)

Create a brand new PgnCollection with games found in this collection which satisfy the given expression

func (*PgnCollection) GamesToWriterFromTemplate

func (games *PgnCollection) GamesToWriterFromTemplate(dst io.Writer, templateFile string)

Writes into the specified writer the result of instantiating the given template file with information of all games in this collection. The template acknowledges all tags of a pgngame plus others. For a full description, see the manual.

func (*PgnCollection) GetGame

func (games *PgnCollection) GetGame(index int) PgnGame

Return the index-th game stored in this particular collection. The result is undefined in case an index out of bounds is passed

func (*PgnCollection) GetGames

func (games *PgnCollection) GetGames() []PgnGame

Return all games as instances of PgnGame that are stored in this particular collection

func (PgnCollection) GetHistogram

func (c PgnCollection) GetHistogram(spec string) (*PgnHistogram, error)

Return a histogram defined with the given specification criteria computed over all games in this collection. It returns any error found or nil in case the histogram was successfully computed

func (PgnCollection) GetPGN

func (c PgnCollection) GetPGN(writer io.Writer) error

Write all games in this collection in the specified io.Writer in PGN format. In case it was not possible it returns an error and nil otherwise

func (*PgnCollection) GetSlice

func (games *PgnCollection) GetSlice(fields ...any) []any

This is an auxiliary function used in text/templates to generate slices of strings to be given as argument to other methods

func (*PgnCollection) GetTable

func (games *PgnCollection) GetTable(specline string, fields []any) table.Table

Returns a table according to the specification given in first place. Columns are populated with the tags given in fields. It is intended to be used in ascii table templates

It is intended to be used in LaTeX templates

func (*PgnCollection) GetTagValue

func (games *PgnCollection) GetTagValue(name string) string

This function is used in text/templates and it is the equivalent to the homonym function defined for PgnGame.

It returns the empty string if the given name is not defined as a tag in the first game of this collection and the value of the tag (casted to a string) otherwise. This function assumes that all games within the same collection share the same tags.

It is intended to be used in LaTeX templates

func (PgnCollection) Len

func (games PgnCollection) Len() int

Return the number of items in the collection

func (PgnCollection) Play

func (c PgnCollection) Play(plies int, writer io.Writer)

Play this collection of games on the given writer showing the board repeteadly after the given number of plies on the specified writer, in case it is strictly positive.

func (*PgnCollection) ShowIndex

func (games *PgnCollection) ShowIndex() string

Return an index of the games in this collection. It assumes that each game is properly indexed with labels (with the usage of game.SetLabel ())

It is intended to be used in LaTeX templates

func (*PgnCollection) Sort

func (c *PgnCollection) Sort(spec string) (*PgnCollection, error)

Sort the games in this collection according to the specific criteria which consists of a semicolon separated list of pairs (direction var/bool expr). The direction can be either '<' (ascending order) or '>' (descending order), next either a variable or a bool expression can be used so that games are sorted according to the value of the variable or the result of the evaluation of the bool expr

The result is returned in a brand new collection of Pgn games

type PgnFile

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

A PgnFile contains a collection of chess games in PGN format. It stores no information related to the chess games contained in it and it should be used solely for creating a PgnCollection

func NewPgnFile

func NewPgnFile(filepath string) (*PgnFile, error)

A new instance of PgnFile can be created just by providing the file path (which is allowed also to contain the character '~'). In case the file does not exist, or it is not a regular file then an error is returned

func (PgnFile) Games

func (f PgnFile) Games() (*PgnCollection, error)

Return all games stored in the PgnFile f as a collection of PgnGames

func (PgnFile) ModTime

func (f PgnFile) ModTime() time.Time

Return the last modification time of the given PgnFile

func (PgnFile) Name

func (f PgnFile) Name() string

Return the filepath of a PgnFile

func (PgnFile) Size

func (f PgnFile) Size() int64

Return the size in bytes of the given PgnFile

func (PgnFile) String

func (f PgnFile) String() string

PgnFile are stringers. They just show the information of a PgnFile using a table

type PgnGame

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

A game consists just of a map that stores information of all PGN tags, the sequence of moves and finally the outcome.

func (*PgnGame) Filter

func (game *PgnGame) Filter(expression string) (bool, error)

Return whether the given expression is true or not for this specific game

func (*PgnGame) GetField

func (game *PgnGame) GetField(field string) string

A field is either a tag of the receiver game, or a value that can be extracted from it (such as "Moves" or "Result")

This function is to be used in LaTeX templates

func (*PgnGame) GetLaTeXMovesWithComments

func (game *PgnGame) GetLaTeXMovesWithComments() string

Produces a LaTeX string with the list of moves of this game along with the different annotations.

This method successively processes the moves in this PgnGame until a comment is found.

It is intended to be used in LaTeX templates

func (*PgnGame) GetLaTeXMovesWithCommentsTabular

func (game *PgnGame) GetLaTeXMovesWithCommentsTabular(width1, width2 string, nbplies int) (output string)

Produces a LaTeX string with a long table showing the moves every nbplies and the chess board

This method successively processes the moves in this PgnGame until a comment is found.

It is intended to be used in LaTeX templates

func (*PgnGame) GetPGN

func (game *PgnGame) GetPGN() (output string)

Return the contents of this game in PGN format

func (*PgnGame) GetTagValue

func (game *PgnGame) GetTagValue(name string) (value string, err error)

Return the value of a specific tag. In case the tag is not found in this game, an error is return along with any data.

It is intended to be used in LaTeX templates

func (*PgnGame) Moves

func (game *PgnGame) Moves() []PgnMove

Return a list of the moves of this game as a slice of PgnMove

func (*PgnGame) Outcome

func (game *PgnGame) Outcome() PgnOutcome

Return an instance of PgnOutcome with the result of this game

func (*PgnGame) SetLabel

func (game *PgnGame) SetLabel() string

Return the LaTeX command for setting a label

It is intended to be used in LaTeX templates

func (*PgnGame) Tags

func (game *PgnGame) Tags() (tags map[string]any)

Return the tags of this game

type PgnHistogram

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

Given a sequence of criteria (either variables or boolean expressions), which might be named, a histogram is implemented as a decision tree where internal nodes store the result of the criteria and lead to other histograms or decision trees until the leaves are reached which simply store the number of occurrences of all variables/boolean expressions from the root to it.

In addition, a histogram contains the total number of observations stored in it so that percentages can be computed for every inner/leaf node

func NewPgnHistogram

func NewPgnHistogram(spec string) (*PgnHistogram, error)

Return a brand new PgnHistogram defined with a string, which consists of a semicolon list of variables/boolean expressions in the form: "<var/expr>+". At least one should be given, and an arbitrary number of them can be specified.

func (*PgnHistogram) Add

func (histogram *PgnHistogram) Add(game PgnGame) error

Updates this histogram with information in the given game, and nil if no error was found

func (PgnHistogram) String

func (histogram PgnHistogram) String() string

Histograms are stringers, so that they can be shown on any writer

type PgnMove

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

A PGN move consist of a single ply. For each move the move number, color (with -1 representing black and +1 representing white) and actual move value (in algebraic form) is stored. Additionally, in case that the elapsed move time was present in the PGN file, it is also stored here.

Finally, any combination of moves after the move are combined into the same field (comments). In case various comments were given they are then separated by '\n'.

func (PgnMove) Color

func (move PgnMove) Color() int

Return the color of the given PgnMove

func (PgnMove) Comments

func (move PgnMove) Comments() string

Return comments of the given PgnMove

func (PgnMove) Move

func (move PgnMove) Move() string

Return the actual move of the given PgnMove

func (PgnMove) Number

func (move PgnMove) Number() int

Return the number of the given PgnMove

func (PgnMove) String

func (move PgnMove) String() string

Produces a string with the actual content of this move

type PgnOutcome

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

The outcome of a chess game consists of the score obtained by every player as two float32 numbers such that their sum equals 1. Plausible outcomes are (0, 1), (1, 0) and (0.5, 0.5). In addition, the pair (-1, -1) is considered for those games which are not properly ended

func (PgnOutcome) String

func (outcome PgnOutcome) String() string

Produces a string with information of this outcome as a pair of floating-point numbers

Jump to

Keyboard shortcuts

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