pgn

package module
v0.0.0-...-0aa06c3 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2014 License: MIT Imports: 7 Imported by: 0

README

pgnreader

a chess PGN (Portable Game Notation) file reader (written in golang) (originally forked from github.com:wfreeman/pgn)

Build Status Coverage Status

Normal go install... go get github.com/handcraftsman/pgnreader

Test framework is https://github.com/go-check/check

go get gopkg.in/check.v1

minimum viable snippet

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/handcraftsman/pgnreader"
)

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]

license

MIT License, see LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func FORFromBoard

func FORFromBoard(b *Board) 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) 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) 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 (
	NoFile File = ' '
	FileA  File = 'a'
	FileB  File = 'b'
	FileC  File = 'c'
	FileD  File = 'd'
	FileE  File = 'e'
	FileF  File = 'f'
	FileG  File = 'g'
	FileH  File = 'h'
)

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
}
var (
	NilMove Move = Move{From: NoPosition, To: NoPosition}
)

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

func ParsePosition

func ParsePosition(pstr string) (Position, error)

func PositionFromFileRank

func PositionFromFileRank(f File, r Rank) (p 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'
	Rank1  Rank = '1'
	Rank2  Rank = '2'
	Rank3  Rank = '3'
	Rank4  Rank = '4'
	Rank5  Rank = '5'
	Rank6  Rank = '6'
	Rank7  Rank = '7'
	Rank8  Rank = '8'
)

Jump to

Keyboard shortcuts

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