pgn

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2020 License: MIT Imports: 9 Imported by: 0

README

pgn

A pgn parser for golang. This is release v1.

Build Status Coverage Status

Normal go install... go get gopkg.in/freeeve/pgn.v1

test instructions

go get gopkg.in/check.v1
go get ./...
go test

minimum viable snippet

package main

import (
	"fmt"
	"log"
	"os"

	"gopkg.in/freeeve/pgn.v1"
)

func main() {
	f, err := os.Open("polgar.pgn")
	if err != nil {
		log.Fatal(err)
	}
	ps := pgn.NewPGNScanner(f)
	// while there's more to read in the file
	for ps.Next() {
		// scan the next game
		game, err := ps.Scan()
		if err != nil {
			log.Fatal(err)
		}

		// print out tags
		fmt.Println(game.Tags)

		// make a new board so we can get FEN positions
		b := pgn.NewBoard()
		for _, move := range game.Moves {
			// make the move on the board
			b.MakeMove(move)
			// print out FEN for each move in the game
			fmt.Println(b)
		}
	}
}

produces output like this for each game in the pgn file:

map[Event:Women's Chess Cup
    Site:Dresden GER 
    Round:7.1 
    Black:Polgar,Z 
    Result:1/2-1/2 
    Date:2006.07.08 
    White:Paehtz,E 
    WhiteElo:2438 
    BlackElo:2577 
    ECO:B35]
rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq d3 0 1
rnbqkbnr/pppp1ppp/4p3/8/3P4/8/PPP1PPPP/RNBQKBNR w KQkq - 0 2
rnbqkbnr/pppp1ppp/4p3/8/3P4/5N2/PPP1PPPP/RNBQKB1R b KQkq - 1 2
rnbqkbnr/pppp2pp/4p3/5p2/3P4/5N2/PPP1PPPP/RNBQKB1R w KQkq f6 0 3
rnbqkbnr/pppp2pp/4p3/3P1p2/8/5N2/PPP1PPPP/RNBQKB1R b KQkq - 0 3
rnbqkb1r/pppp2pp/4pn2/3P1p2/8/5N2/PPP1PPPP/RNBQKB1R w KQkq - 1 4
rnbqkb1r/pppp2pp/4Pn2/5p2/8/5N2/PPP1PPPP/RNBQKB1R b KQkq - 0 4
rnbq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 w - - 9 72
8/8/3rk1p1/6Rp/5R1P/2P1K3/r5P1/8 b - - 10 72
8/3k4/3r2p1/6Rp/5R1P/2P1K3/r5P1/8 w - - 11 73
8/3k4/3r2p1/6Rp/3R3P/2P1K3/r5P1/8 b - - 12 73
8/3k4/r2r2p1/6Rp/3R3P/2P1K3/6P1/8 w - - 13 74
8/3k4/r2r2R1/7p/3R3P/2P1K3/6P1/8 b - - 0 74
8/3k4/r5R1/7p/3r3P/2P1K3/6P1/8 w - - 0 75
8/3k2R1/r7/7p/3r3P/2P1K3/6P1/8 b - - 1 75
8/6R1/r3k3/7p/3r3P/2P1K3/6P1/8 w - - 2 76
8/6R1/r3k3/7p/3P3P/4K3/6P1/8 b - - 0 76

license

MIT License, see LICENSE file.

code of conduct

See Women Who Code DC's CoC

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAmbiguousMove       = errors.New("pgn: ambiguous algebraic move")
	ErrUnknownMove         = errors.New("pgn: unknown move")
	ErrAttackerNotFound    = errors.New("pgn: attacker not found")
	ErrMoveFromEmptySquare = errors.New("pgn: move from empty square")
	ErrMoveWrongColor      = errors.New("pgn: move from wrong color")
	ErrMoveThroughPiece    = errors.New("pgn: move through piece")
	ErrMoveThroughCheck    = errors.New("pgn: move through check")
	ErrMoveIntoCheck       = errors.New("pgn: move into check")
	ErrMoveInvalidCastle   = errors.New("pgn: move invalid castle")
)

Functions

func FORFromBoard

func FORFromBoard(b *Board) string

func ParseMoveOrResult added in v1.1.1

func ParseMoveOrResult(s *scanner.Scanner) string

func ParseMoves

func ParseMoves(s *scanner.Scanner, g *Game) error

func ParseTags

func ParseTags(s *scanner.Scanner, g *Game) error

Types

type Board

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

func NewBoard

func NewBoard() *Board

func NewBoardFEN

func NewBoardFEN(fen string) (*Board, error)

func (Board) FindKing

func (b Board) FindKing(color Color) Position

func (Board) GetPiece

func (b Board) GetPiece(p Position) Piece

func (*Board) MakeAlgebraicMove

func (b *Board) MakeAlgebraicMove(str string, color Color) error

func (*Board) MakeCoordMove

func (b *Board) MakeCoordMove(str string) error

func (*Board) MakeMove

func (b *Board) MakeMove(m Move) error

func (*Board) MoveFromAlgebraic

func (b *Board) MoveFromAlgebraic(str string, color Color) (Move, error)

func (*Board) RemovePiece

func (b *Board) RemovePiece(pos Position, p Piece)

func (*Board) Save added in v1.2.0

func (b *Board) Save(path string) error

func (*Board) SetPiece

func (b *Board) SetPiece(pos Position, p Piece)

func (*Board) String

func (b *Board) String() string

type CastleStatus

type CastleStatus int8
const (
	Both CastleStatus = iota
	None
	Kingside
	Queenside
)

func (CastleStatus) String

func (cs CastleStatus) String(c Color) string

type Color

type Color int8
const (
	NoColor Color = iota
	Black
	White
)

func (Color) String

func (c Color) String() string

type FEN

type FEN struct {
	FOR                 string
	ToMove              Color
	WhiteCastleStatus   CastleStatus
	BlackCastleStatus   CastleStatus
	EnPassantVulnerable Position
	HalfmoveClock       int
	Fullmove            int
}

func FENFromBoard

func FENFromBoard(b *Board) FEN

func ParseFEN

func ParseFEN(fenstr string) (*FEN, error)

func (FEN) String

func (fen FEN) String() string

type File

type File byte
const (
	FileA File = 'a' + iota
	FileB
	FileC
	FileD
	FileE
	FileF
	FileG
	FileH
	NoFile File = ' '
)

type Game

type Game struct {
	Moves []Move
	Tags  map[string]string
}

func ParseGame

func ParseGame(s *scanner.Scanner) (*Game, error)

type Move

type Move struct {
	From    Position
	To      Position
	Promote Piece
	San     string
}
var (
	NilMove Move = Move{From: NoPosition, To: NoPosition}
)

func MoveFromCoord

func MoveFromCoord(str string) (Move, error)

func (Move) String

func (m Move) String() string

type PGNScanner

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

func NewPGNScanner

func NewPGNScanner(r io.Reader) *PGNScanner

func (*PGNScanner) Next

func (ps *PGNScanner) Next() bool

func (*PGNScanner) Scan

func (ps *PGNScanner) Scan() (*Game, error)

type Piece

type Piece byte
const (
	NoPiece     Piece = ' '
	BlackPawn   Piece = 'p'
	BlackKnight Piece = 'n'
	BlackBishop Piece = 'b'
	BlackRook   Piece = 'r'
	BlackQueen  Piece = 'q'
	BlackKing   Piece = 'k'
	WhitePawn   Piece = 'P'
	WhiteKnight Piece = 'N'
	WhiteBishop Piece = 'B'
	WhiteRook   Piece = 'R'
	WhiteQueen  Piece = 'Q'
	WhiteKing   Piece = 'K'
)

func (Piece) Color

func (p Piece) Color() Color

func (*Piece) Normalize

func (p *Piece) Normalize()

type Position

type Position uint64
const (
	A1 Position = 1 << iota
	B1
	C1
	D1
	E1
	F1
	G1
	H1
	A2
	B2
	C2
	D2
	E2
	F2
	G2
	H2
	A3
	B3
	C3
	D3
	E3
	F3
	G3
	H3
	A4
	B4
	C4
	D4
	E4
	F4
	G4
	H4
	A5
	B5
	C5
	D5
	E5
	F5
	G5
	H5
	A6
	B6
	C6
	D6
	E6
	F6
	G6
	H6
	A7
	B7
	C7
	D7
	E7
	F7
	G7
	H7
	A8
	B8
	C8
	D8
	E8
	F8
	G8
	H8

	NoPosition Position = 0
)

func ParsePosition

func ParsePosition(pstr string) (Position, error)

func PositionFromFileRank

func PositionFromFileRank(f File, r Rank) Position

func (Position) GetFile

func (p Position) GetFile() File

func (Position) GetRank

func (p Position) GetRank() Rank

func (Position) String

func (p Position) String() string

type Rank

type Rank byte
const (
	NoRank Rank = '0' + iota
	Rank1
	Rank2
	Rank3
	Rank4
	Rank5
	Rank6
	Rank7
	Rank8
)

Jump to

Keyboard shortcuts

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