chess

package module
v0.0.0-...-8a7b2b4 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2016 License: MIT Imports: 5 Imported by: 0

README

Chess

A chess game that just works™.

How to use it

Run the code below

package main

import "github.com/jaxi/chess"

func main() {
	b := chess.NewBoard()
	b.Looping()
}

And then run the code above and it pretty much looks like this.

➜  examples git:(master) go run main.go
     a   b   c   d   e   f   g   h
   +---+---+---+---+---+---+---+---+
 8 | ♜ | ♞ | ♝ | ♛ | ♚ | ♝ | ♞ | ♜ | 8
   +---+---+---+---+---+---+---+---+
 7 | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | 7
   +---+---+---+---+---+---+---+---+
 6 |   |   |   |   |   |   |   |   | 6
   +---+---+---+---+---+---+---+---+
 5 |   |   |   |   |   |   |   |   | 5
   +---+---+---+---+---+---+---+---+
 4 |   |   |   |   |   |   |   |   | 4
   +---+---+---+---+---+---+---+---+
 3 |   |   |   |   |   |   |   |   | 3
   +---+---+---+---+---+---+---+---+
 2 | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | 2
   +---+---+---+---+---+---+---+---+
 1 | ♖ | ♘ | ♗ | ♕ | ♔ | ♗ | ♘ | ♖ | 1
   +---+---+---+---+---+---+---+---+
     a   b   c   d   e   f   g   h

Move examples that use configurable interface in the examples folder. You can even play the game remotely!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bishop

type Bishop struct {
	Piece
}

Bishop - The bishop

func (Bishop) Move

func (b Bishop) Move(bd *Board, pos1, pos2 Position) bool

Move like a Bishop

func (Bishop) PieceKind

func (b Bishop) PieceKind() PieceKind

PieceKind of Bishop

func (Bishop) String

func (b Bishop) String() string

type Board

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

Board is where the chess is played on.

func EmptyBoard

func EmptyBoard() *Board

EmptyBoard returns an board with no pieces

func NewBoard

func NewBoard() *Board

NewBoard creates a new board that has pieces been placed

func (*Board) AdvanceLooping

func (b *Board) AdvanceLooping(players ...Player) int

AdvanceLooping loop the gamve with more options returns the failed/disconnected player index

func (*Board) Looping

func (b *Board) Looping()

Looping the game

func (*Board) Move

func (b *Board) Move(pos1, pos2 Position) bool

Move the piece on the board

func (*Board) String

func (b *Board) String() string

String - aka. turn the board into a string

func (*Board) TerminalString

func (b *Board) TerminalString() string

TerminalString is a specific string returns for terminal

func (*Board) Turn

func (b *Board) Turn() Side

Turn shows which side is playing now

type EmptySquare

type EmptySquare struct {
	Piece
}

EmptySquare - aka no piece on the board

func (EmptySquare) Move

func (es EmptySquare) Move(b *Board, pos1, pos2 Position) bool

Move always returns false if the squre is empty

func (EmptySquare) PieceKind

func (es EmptySquare) PieceKind() PieceKind

PieceKind of KING

type King

type King struct {
	Piece
	*Movable
}

King - The piece to protect

func (King) Move

func (k King) Move(b *Board, pos1, pos2 Position) bool

Move in the King's way

func (King) PieceKind

func (k King) PieceKind() PieceKind

PieceKind of KING

func (King) String

func (k King) String() string

type Knight

type Knight struct {
	Piece
}

Knight - Piece that moves in the weird way

func (Knight) Move

func (k Knight) Move(b *Board, pos1, pos2 Position) bool

Move like a Knight

func (Knight) PieceKind

func (k Knight) PieceKind() PieceKind

PieceKind of Knight

func (Knight) String

func (k Knight) String() string

type Movable

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

Movable is used to attach to the piece that need recording if it's been moved or not

type Move

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

Move - The Movement in one turn

func NewMove

func NewMove(i, j, k, l int) Move

NewMove create a new Move struct

func (Move) Null

func (m Move) Null() bool

Null tells Whether the Move doesn't make sense

type Pawn

type Pawn struct {
	Piece
	*Movable
}

Pawn - the weakest

func (Pawn) Move

func (p Pawn) Move(b *Board, pos1, pos2 Position) bool

Move like a Pawn

func (Pawn) PieceKind

func (p Pawn) PieceKind() PieceKind

PieceKind of Pawn

func (Pawn) String

func (p Pawn) String() string

type Piece

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

The Piece stand at wherever on the board

func (Piece) Side

func (p Piece) Side() Side

Side returns the side of the piece

func (Piece) String

func (p Piece) String() string

type PieceKind

type PieceKind int

PieceKind is the type representing for the pieces on the board

const (
	// EMPTY_SQUARE aka Empty
	EMPTY_SQUARE PieceKind = 0 + iota
	// PAWN (♙, ♟)
	PAWN
	// ROOK (♖, ♜)
	ROOK
	// KNIGHT (♘, ♞)
	KNIGHT
	// BISHOP (♗, ♝)
	BISHOP
	// QUEEN (♕, ♛)
	QUEEN
	// KING (♔, ♚)
	KING
)

type Player

type Player interface {
	ShowTurn(b *Board)
	RenderBoard(b *Board)
	FetchMove() (Move, error)
	ErrorMessage(b *Board)
}

Player is the standard protocol can connect to the game

type Position

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

Position is the place where a piece stand

func (Position) Equal

func (p Position) Equal(p2 Position) bool

Equal returns if two positions are in the same place

func (Position) IsValid

func (p Position) IsValid() bool

IsValid returns if a position is on the board or not

type Queen

type Queen struct {
	Piece
}

Queen - The strongest piece on the board

func (Queen) Move

func (q Queen) Move(b *Board, pos1, pos2 Position) bool

Move like the Queen

func (Queen) PieceKind

func (q Queen) PieceKind() PieceKind

PieceKind of Queen

func (Queen) String

func (q Queen) String() string

type Rook

type Rook struct {
	Piece
	*Movable
}

Rook - aka. the Tower

func (Rook) Move

func (r Rook) Move(b *Board, pos1, pos2 Position) bool

Move like a Rook

func (Rook) PieceKind

func (r Rook) PieceKind() PieceKind

PieceKind of Rook

func (Rook) String

func (r Rook) String() string

type Side

type Side int

Side can be either white or black

const (
	// EMPTY is where neither white nor black piece stand on it
	EMPTY Side = 0 + iota
	// WHITE is the side that start the game second
	WHITE
	// BLACK move after the WHITE side is moved at the beginning
	BLACK
)

type Square

type Square interface {
	String() string
	PieceKind() PieceKind
	Side() Side
	Move(b *Board, pos1, pos2 Position) bool
}

Square represents for one of the 64 squares on the board

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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