chessboard

package module
v0.0.0-...-baf44d9 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2017 License: MIT Imports: 22 Imported by: 0

README

chessboard

A Go library for rendering chess positions described by package chess as images.

Dependencies

This package does not currently use dependency management, so you may need to manually go get all dependencies prior to building.

Documentation

API Reference

Installation

go get -u gitlab.com/eightsquared/chessboard

License

MIT

Documentation

Overview

Package chessboard provides functions to render a chess position as an image.

To use this package, you will probably also need the chess package (https://gitlab.com/eightsquared/chess) to describe games. For example, supposing you have the following board:

// White to mate in one move.
g := chess.Game{
	Board: chess.Board{
		chess.E8: chess.BlackBishop,
		chess.F8: chess.BlackKing,
		chess.A7: chess.BlackPawn,
		chess.B7: chess.BlackPawn,
		chess.C7: chess.BlackPawn,
		chess.G7: chess.BlackPawn,
		chess.C6: chess.BlackKnight,
		chess.D6: chess.BlackPawn,
		chess.H6: chess.BlackPawn,
		chess.C5: chess.BlackBishop,
		chess.C4: chess.WhiteBishop,
		chess.F4: chess.WhiteBishop,
		chess.G4: chess.BlackKnight,
		chess.H4: chess.BlackQueen,
		chess.C3: chess.WhiteKnight,
		chess.A2: chess.WhitePawn,
		chess.B2: chess.WhitePawn,
		chess.G2: chess.WhitePawn,
		chess.H2: chess.WhitePawn,
		chess.E1: chess.WhiteRook,
		chess.F1: chess.WhiteRook,
		chess.H1: chess.WhiteKing,
	},
	ToMove: chess.White,
}

You can write an ASCII-art visualization of this position to standard output as follows:

err := chessboard.WriteSVG(os.Stdout, chessboard.Defaults)
// Output:
//
//   ╔═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗
// 8 ║   │   │   │   │ b │ k │   │   ║
//   ╟───┼───┼───┼───┼───┼───┼───┼───╢
// 7 ║ p │ p │ p │   │   │   │ p │   ║
//   ╟───┼───┼───┼───┼───┼───┼───┼───╢
// 6 ║   │   │ n │ p │   │   │   │ p ║
//   ╟───┼───┼───┼───┼───┼───┼───┼───╢
// 5 ║   │   │ b │   │   │   │   │   ║
//   ╟───┼───┼───┼───┼───┼───┼───┼───╢
// 4 ║   │   │ B │   │   │ B │ n │ q ║
//   ╟───┼───┼───┼───┼───┼───┼───┼───╢
// 3 ║   │   │ N │   │   │   │   │   ║
//   ╟───┼───┼───┼───┼───┼───┼───┼───╢
// 2 ║ P │ P │   │   │   │   │ P │ P ║
//   ╟───┼───┼───┼───┼───┼───┼───┼───╢
// 1 ║   │   │   │   │ R │ R │   │ K ║
//   ╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝
// w   a   b   c   d   e   f   g   h

In addition to ASCII output, this package supports rendering to SVG (fast) and PNG (much slower due to raster scaling operations).

Index

Constants

View Source
const (
	MinImageSize = 8 * 16
	MaxImageSize = 8 * 256
	MaxFontSize  = 20
)

These constants establish bounds on the rendered image dimensions and the maximum label size.

Variables

View Source
var Defaults = Options{
	Size:   256,
	Light:  color.RGBA{255, 206, 158, 255},
	Dark:   color.RGBA{209, 139, 71, 255},
	Rotate: None,
	Style:  DefaultStyle,
}

Defaults holds a set of reasonable default options.

Functions

func WriteASCII

func WriteASCII(w io.Writer, g chess.Game, o Options) (err error)

WriteASCII renders a visual representation of the game g to w in ASCII art format.

This function does not yet support board rotation.

func WritePNG

func WritePNG(w io.Writer, g chess.Game, o Options) (err error)

WritePNG renders a visual representation of the game g to w in PNG format.

func WriteSVG

func WriteSVG(w io.Writer, g chess.Game, o Options) (err error)

WriteSVG renders a visual representation of the game g to w in SVG format.

Types

type Options

type Options struct {
	// Size sets the image width and height in pixels.
	Size int
	// Light holds the color used to draw light-colored squares.
	Light color.RGBA
	// Dark holds the color used to draw dark-colored squares.
	Dark color.RGBA
	// Rotate describes how to transform (typically rotate) the board. (Piece
	// images and square labels are not affected by rotation.)
	Rotate Rotation
	// Style controls the look of the piece images.
	Style PieceStyle
}

Options describes the options that may be applied to a rendered image.

type PieceStyle

type PieceStyle uint8

PieceStyle enumerates the allowed piece styles.

const (
	Lasker PieceStyle = iota
	Merida
	DefaultStyle PieceStyle = Lasker
)

Lasker is an open-source style found at https://gitlab.com/eightsquared/lasker. Merida is derived from a free font of the same name.

func (PieceStyle) String

func (s PieceStyle) String() string

type Rotation

type Rotation uint8

Rotation enumerates the allowed rotational transforms that can be applied to a rendered board.

const (
	None Rotation = iota
	Clockwise
	CounterClockwise
	Half
)

By default, no rotation is applied to the board. Clockwise and CounterCllockwise indicate that the board should be rotated one quarter-turn in the clockwise and counter-clockwise directions, respectively. Half indicates that the board should be rotated one half-turn, which effectively draws the board from Black's point of view.

Jump to

Keyboard shortcuts

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