chessapi

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

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

Go to latest
Published: Feb 18, 2019 License: MIT Imports: 2 Imported by: 1 module, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Chess API

This is a Chess game API written in Go. It is the engine behind https://github.com/miketmoore/chess.

Test

go test

API

View documentation by using godoc locally: godoc -http=:6060, then open http://localhost:6060.

Resources

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintPlayerPiece

func PrintPlayerPiece(p *PlayerPiece)

Types

type Board

type Board struct {
	Pieces []*PlayerPiece
}

Board is a data structure used to track pieces currently on the board

func NewBoard

func NewBoard() Board

NewBoard returns a new chess board model

func (*Board) GetOccupant

func (b *Board) GetOccupant(coord Coord) (*PlayerPiece, bool)

GetOccupant returns a pointer to the piece at the specified coordinate, if it exists there

func (*Board) IsOccupied

func (b *Board) IsOccupied(coord Coord) bool

IsOccupied returns a boolean indicating if the specified board coordinate is occupied

func (*Board) IsOccupiedByColor

func (b *Board) IsOccupiedByColor(coord Coord, color Player) bool

IsOccupiedByColor returns a boolean indicating if the specified board coordinate is occupied by a piece with the specified color

func (*Board) MovePieceTo

func (b *Board) MovePieceTo(piece *PlayerPiece, coord Coord)

MovePieceTo updates the specified piece's coordinate and removes the captured piece from the board (if applicable)

func (*Board) Remove

func (b *Board) Remove(coord Coord)

Remove removes the piece at the specified coordinate (if it exists)

type Coord

type Coord struct {
	Rank Rank
	File File
}

Coord represents a board square by rank and file

func (*Coord) Equals

func (c *Coord) Equals(coord Coord) bool

type Direction

type Direction int
const (
	North Direction = iota
	Northeast
	East
	Southeast
	South
	Southwest
	West
	Northwest
)

type File

type File uint8

File is a custom type that represents a vertical column (file) on the chess board

const (
	FileNone File = iota
	FileA
	FileB
	FileC
	FileD
	FileE
	FileF
	FileG
	FileH
)

type Game

type Game struct {
	Board Board

	ValidDestinations ValidDestinations
	// contains filtered or unexported fields
}

Model contains data used for the game

func NewGame

func NewGame() Game

NewGame creates a new chess game

func (*Game) EndTurn

func (m *Game) EndTurn()

func (*Game) PlyEnd

func (m *Game) PlyEnd(coord Coord) (err error, success bool, capture bool, capturedPiece *PlayerPiece)

PlyEnd checks if the square contains a valid destination for the current player's selected piece

Reference: https://en.wikipedia.org/wiki/Ply_(game_theory)

func (*Game) PlyStart

func (m *Game) PlyStart(coord Coord) bool

PlyStart checks if the square contains a piece that can be legally moved by the current player and updates state if true

Reference: https://en.wikipedia.org/wiki/Ply_(game_theory)

type Piece

type Piece uint8

Piece represents the piece type

const (
	Pawn Piece = iota
	Rook
	Knight
	Bishop
	Queen
	King
)

type Player

type Player bool

Player is a type for player colors

const (
	// White represents the white player
	White Player = true

	// Black represents the black player
	Black Player = false
)

type PlayerPiece

type PlayerPiece struct {
	Color   Player
	Piece   Piece
	Coord   Coord
	History []Coord
}

PlayerPiece represents one player's piece

func NewPiece

func NewPiece(color Player, piece Piece, file File, rank Rank) *PlayerPiece

NewPiece returns an instance of PlayerPiece

func NewPlayerPiece

func NewPlayerPiece(color Player, piece Piece) PlayerPiece

NewPlayerPiece returns an instance of PlayerPiece

func (*PlayerPiece) HasMoved

func (p *PlayerPiece) HasMoved() bool

HasMoved returns a boolean indicating if the piece has moved or not

type Rank

type Rank uint8

Rank is a custom type that represents a horizontal row (rank) on the chess board

const (
	RankNone Rank = iota
	Rank1
	Rank2
	Rank3
	Rank4
	Rank5
	Rank6
	Rank7
	Rank8
)

type ValidDestinations

type ValidDestinations map[Coord]bool

ValidDestinations contains a set of board coordinates that are valid destinations for a piece

type ValidationResponse

type ValidationResponse struct {
	Destinations ValidDestinations
	Check        bool
	CheckedBy    map[Piece]Coord
}

func NewValidationResponse

func NewValidationResponse() ValidationResponse

func (ValidationResponse) Checkmate

func (v ValidationResponse) Checkmate() bool

Jump to

Keyboard shortcuts

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