chess

package module
v0.0.0-...-906a36e Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2019 License: MIT Imports: 6 Imported by: 1

README

Chess is a set of Go packages containing chess and chess960 related
functionality.

Documentation: https://godoc.org/github.com/jonpchin/chess

Installation:

	$ go get github.com/jonpchin/chess

Documentation

Overview

Package chess provides functionality to handle chess and chess960 board positions.

Index

Constants

View Source
const (
	White = iota
	Black
)
View Source
const (
	NoPiece = iota << 1
	Pawn
	Knight
	Bishop
	Rook
	Queen
	King
)

Piece types

View Source
const (
	WP = White | Pawn
	WN = White | Knight
	WB = White | Bishop
	WR = White | Rook
	WQ = White | Queen
	WK = White | King
	BP = Black | Pawn
	BN = Black | Knight
	BB = Black | Bishop
	BR = Black | Rook
	BQ = Black | Queen
	BK = Black | King
)

Pieces

View Source
const (
	FileA = iota
	FileB
	FileC
	FileD
	FileE
	FileF
	FileG
	FileH
)

Files

View Source
const (
	Rank1 = iota
	Rank2
	Rank3
	Rank4
	Rank5
	Rank6
	Rank7
	Rank8
)

Ranks

View Source
const (
	WhiteOO  = White | kingSide
	BlackOO  = Black | kingSide
	WhiteOOO = White | queenSide
	BlackOOO = Black | queenSide
)

Castling

Variables

View Source
var Figurines = []rune{
	'.', ',',
	0x2659, 0x265F,
	0x2658, 0x265E,
	0x2657, 0x265D,
	0x2656, 0x265C,
	0x2655, 0x265B,
	0x2654, 0x265A,
}
View Source
var NullMove = Move{}
View Source
var PieceLetters = []rune{
	'.', ',',
	'P', 'p',
	'N', 'n',
	'B', 'b',
	'R', 'r',
	'Q', 'q',
	'K', 'k',
}

Functions

This section is empty.

Types

type Board

type Board struct {
	Piece      [64]Piece // piece placement (NoPiece, WP, BP, WN, BN, ...)
	SideToMove int       // White or Black
	MoveNr     int       // fullmove counter (1-based)
	Rule50     int       // halfmove counter for the 50-move rule (counts from 0-100)
	EpSquare   Sq        // en-passant square (behind capturable pawn)
	CastleSq   [4]Sq     // rooks that can castle; e.g. CastleSq[WhiteOO]
	// contains filtered or unexported fields
}

Board represents a regular chess or chess960 position.

func MustParseFen

func MustParseFen(fen string) *Board

MustParseFen is like ParseFen, but panics if fen cannot be parsed.

func ParseFen

func ParseFen(fen string) (b *Board, err error)

ParseFen initializes a board with the given FEN string. Fields omitted from fen will default to the value in the starting position of a regular chess game (e.g. 'w' for the side-to-move), so that ParseFen("") returns the starting position.

For castling rights both the conventional KkQq can be used as well as file letters, for example 'C' for a white rook on the c-file that can castle. The latter is sometimes needed for chess960 positions.

func (*Board) Fen

func (b *Board) Fen() string

Fen returns the FEN string (Forsyth-Edwards Notation) of the position.

func (*Board) Hash

func (b *Board) Hash() (hash uint64)

Hash returns a 64-bit hash of the position. This is the Polyglot hash, that can be used to lookup positions in a Polyglot opening book. Relevant documentation: http://alpha.uhasselt.be/Research/Algebra/Toga/book_format.html

func (*Board) IsCheckOrMate

func (b *Board) IsCheckOrMate() (check, mate bool)

IsCheckOrMate returns whether the side to move is in check and/or has been mated. Mate without check means stalemate.

func (*Board) LegalMoves

func (b *Board) LegalMoves() []Move

LegalMoves returns the list of moves that can be played in this position.

func (Board) MakeMove

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

MakeMove returns a copy of the Board with move m applied.

func (*Board) ParseMove

func (b *Board) ParseMove(s string) (Move, error)

ParseMove parses a move in algebraic notation. The parser is forgiving and will accept varying forms of algebraic notation, including slightly incorrect notations (for instance with uncapitalized piece characters). Examples: e4, Bb5, cxd3, O-O, 0-0-0, Rae1+, f8=Q, f8/Q, e2-e4, Bf1-b5, e2e4, f1b5, e1g1 (castling), f7f8q.

type Move

type Move struct {
	From      Sq
	To        Sq
	Promotion Piece
}

func (Move) Fan

func (m Move) Fan(b *Board) string

Fan is like San but uses figurines.

func (Move) San

func (m Move) San(b *Board) string

San returns the move in Standard Algebraic Notation.

type Piece

type Piece uint8

func (Piece) Color

func (p Piece) Color() int

func (Piece) Type

func (p Piece) Type() int

type Sq

type Sq int8
const (
	A1, B1, C1, D1, E1, F1, G1, H1 Sq = 8*iota + 0, 8*iota + 1, 8*iota + 2,
		8*iota + 3, 8*iota + 4, 8*iota + 5, 8*iota + 6, 8*iota + 7
	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
	NoSquare Sq = -1
)

func Square

func Square(file, rank int) Sq

Square returns a square with the given file (0-7) and rank (0-7).

func (Sq) File

func (sq Sq) File() int

File returns the square's file (0-7).

func (Sq) Rank

func (sq Sq) Rank() int

Rank returns the square's rank (0-7).

func (Sq) RelativeRank

func (sq Sq) RelativeRank(color int) int

RelativeRank returns the square's rank relative to the given player (0-7).

func (Sq) String

func (sq Sq) String() string

String returns the algebraic notation of the square (a1, e5, etc.).

Directories

Path Synopsis
package engine defines a generic interface for communicating with chess engines.
package engine defines a generic interface for communicating with chess engines.
uci
Package uci (partly) implements the UCI protocol for communicating with chess engines.
Package uci (partly) implements the UCI protocol for communicating with chess engines.
Package pgn reads chess games from Portable Game Notation (PGN) files.
Package pgn reads chess games from Portable Game Notation (PGN) files.

Jump to

Keyboard shortcuts

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