movegengo

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

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

Go to latest
Published: Oct 16, 2018 License: MIT Imports: 2 Imported by: 0

README

MovegenGo Documentation

forthebadgeforthebadge

Health

Branch Build status Code climate Go Report Card Code Coverage
master CircleCI Maintainability Go Report Card Test Coverage

About

This package is a chess move generator that encodes moves. It uses a hybrid of psuedo and legal moves. It has implemented state pattern (using the game state) and iterator pattern (iterating over the move list). I will be implementing a array pointer with an range as argument to directly store the moves into a stack/game tree for a speed improvement.

This package also includes a Move "class" for encoding and decoding moves and move tables for certain pieces like knights.

Bitboard layout


# Chess board layout

63 62 61 60 59 58 57 56  | 8
55 54 53 52 51 50 49 48  | 7
47 46 45 44 43 42 41 40  | 6
39 38 37 36 35 34 33 32  | 5
31 30 29 28 27 26 25 24  | 4
23 22 21 20 19 18 17 16  | 3
15 14 13 12 11 10 09 08  | 2
07 06 05 04 03 02 01 00  | 1
_________________________|
 A  B  C  D  E  F  G  H

# uint64 layout by index
A8 B8 C8 D8 ... E1 F1 G1 H1
63 62 61 60 ... 03 02 01 00

The position H1 is found at bitboard & 0x1, while A8 is found at bitboard & (0x1<<63).

Quick start using the iterator

package main

import mg "github.com/chessmodule/movegengo"
import "fmt"

func main() {
  // For a move generator with a default game state use:
  //   movegen := mg.NewMoveGen()
  // If you have a populated game state (mg.GameState) use:
  //   movegen := mg.NewMoveGenByState(gs)
  movegen := mg.NewMoveGen()

  movegen.GenerateMoves() // generates all the moves
  for it := movegen.CreateIterator(); it.Good(); it.Next() {
    mover := mg.NewMove(it.GetMove()) //GetMove returns a uint16 encoded move
    fmt.Println("move: " + mover.ToStr()) // shows from and to values
  }
}

Documentation

Index

Constants

View Source
const BBishop uint64 = 2594073385365405696

BBishop black bishops

View Source
const BKing uint64 = 576460752303423488

BKing black king

View Source
const BKnight uint64 = 4755801206503243776

BKnight black knight

View Source
const BPawn uint64 = 71776119061217280

BPawn black pawn

View Source
const BPieces uint64 = BBishop | BKnight | BPawn | BQueen | BKing | BRook

BPieces all the black pieces concated

View Source
const BQueen uint64 = 1152921504606846976

BQueen black queen

View Source
const BRook uint64 = 9295429630892703744

BRook black rooks

View Source
const FlagCapture uint16 = 16384 // 0b0100000000000000

FlagCapture capture bit

View Source
const FlagPromotion uint16 = 32768 // 0b1000000000000000

FlagPromotion promotion bit

View Source
const FlagSpecial0 uint16 = 4096 // 0b0001000000000000

FlagSpecial0 special2 bit

View Source
const FlagSpecial1 uint16 = 8192 // 0b0010000000000000

FlagSpecial1 special1 bit

View Source
const MaxMoves = 256

MaxMoves per round

View Source
const RangeFlag uint16 = 61440 // 0b1111000000000000

RangeFlag bit range of all bit positions used for flags

View Source
const RangeFrom uint16 = 4032 // 0b0000111111000000

RangeFrom bit range which holds the From position as a uint8 position

View Source
const RangeTo uint16 = 63 // 0b0000000000111111

RangeTo bit range which holds the To position as a uint8 position

View Source
const WBishop uint64 = 36

WBishop white bishops

View Source
const WKing uint64 = 8

WKing white king

View Source
const WKnight uint64 = 66

WKnight white knights

View Source
const WPawn uint64 = 65280

WPawn white pawns

View Source
const WPieces uint64 = WBishop | WKnight | WPawn | WQueen | WKing | WRook

WPieces all the white pieces concated

View Source
const WQueen uint64 = 16

WQueen white queen

View Source
const WRook uint64 = 129

WRook white rooks

Variables

View Source
var KnightMoves = [64]uint64{
	0x20400, 0x50800, 0xa1100, 0x142200, 0x284400, 0x508800, 0xa01000, 0x402000,
	0x2040004, 0x5080008, 0xa110011, 0x14220022, 0x28440044, 0x50880088, 0xa0100010, 0x40200020,
	0x204000402, 0x508000805, 0xa1100110a, 0x1422002214, 0x2844004428, 0x5088008850, 0xa0100010a0, 0x4020002040,
	0x20400040200, 0x50800080500, 0xa1100110a00, 0x142200221400, 0x284400442800, 0x508800885000, 0xa0100010a000, 0x402000204000,
	0x2040004020000, 0x5080008050000, 0xa1100110a0000, 0x14220022140000, 0x28440044280000, 0x50880088500000, 0xa0100010a00000, 0x40200020400000,
	0x204000402000000, 0x508000805000000, 0xa1100110a000000, 0x1422002214000000, 0x2844004428000000, 0x5088008850000000, 0xa0100010a0000000, 0x4020002040000000,
	0x400040200000000, 0x800080500000000, 0x1100110a00000000, 0x2200221400000000, 0x4400442800000000, 0x8800885000000000, 0x100010a000000000, 0x2000204000000000,
	0x4020000000000, 0x8050000000000, 0x110a0000000000, 0x22140000000000, 0x44280000000000, 0x88500000000000, 0x10a00000000000, 0x20400000000000,
}

Functions

func DefaultGameStateColour

func DefaultGameStateColour() uint8

func LSB

func LSB(x uint64) int

LSB Least Significant Bit

func NLSB

func NLSB(x *uint64, i int) int

NLSB Next Least Significant Bit

func Perft

func Perft()

Types

type GameState

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

func NewGameState

func NewGameState() *GameState

func (*GameState) BasicOutput

func (state *GameState) BasicOutput() string

BasicOutput uses letters to indicate pieces

func (*GameState) PrettyOutput

func (state *GameState) PrettyOutput() string

PrettyOutput uses unicodes to indicate pieces

func (*GameState) String

func (state *GameState) String() string

String returns the FEN string for the board

type Iterator

type Iterator interface {
	Begin() uint
	End() uint
	Next() uint
	Good() bool
}

Iterator has the bare minimum to implement an iterator pattern

type Move

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

Move is used to hold a move and manipulate or extract data from it

func NewMove

func NewMove(move uint16) *Move

NewMove Creates a new move from a encoded move

func NewMoveDetail

func NewMoveDetail(from, to, flags uint16) *Move

NewMoveDetail creates a new encoded move based on primitive values

func (*Move) ButterflyIndex

func (m *Move) ButterflyIndex() uint16

ButterflyIndex ...

func (*Move) Equal

func (m *Move) Equal(a *Move) bool

Equal ...

func (*Move) Flags

func (m *Move) Flags() uint16

Flags ...

func (*Move) From

func (m *Move) From() uint16

From ...

func (*Move) GetMove

func (m *Move) GetMove() uint16

GetMove ...

func (*Move) HasCapture

func (m *Move) HasCapture() bool

HasCapture ...

func (*Move) HasPromotion

func (m *Move) HasPromotion() bool

HasPromotion ...

func (*Move) HasSpecial1

func (m *Move) HasSpecial1() bool

HasSpecial1 ...

func (*Move) IsBishopPromoCapture

func (m *Move) IsBishopPromoCapture() bool

IsBishopPromoCapture ...

func (*Move) IsBishopPromotion

func (m *Move) IsBishopPromotion() bool

IsBishopPromotion ...

func (*Move) IsCheck

func (m *Move) IsCheck() bool

IsCheck When the moving piece has caused check. Returns true on check.

func (*Move) IsDoublePawnPush

func (m *Move) IsDoublePawnPush() bool

IsDoublePawnPush ...

func (*Move) IsEPCapture

func (m *Move) IsEPCapture() bool

IsEPCapture ...

func (*Move) IsKingCastle

func (m *Move) IsKingCastle() bool

IsKingCastle ...

func (*Move) IsKnightPromoCapture

func (m *Move) IsKnightPromoCapture() bool

IsKnightPromoCapture ...

func (*Move) IsKnightPromotion

func (m *Move) IsKnightPromotion() bool

IsKnightPromotion ...

func (*Move) IsQueenCastle

func (m *Move) IsQueenCastle() bool

IsQueenCastle ...

func (*Move) IsQueenPromoCapture

func (m *Move) IsQueenPromoCapture() bool

IsQueenPromoCapture ...

func (*Move) IsQueenPromotion

func (m *Move) IsQueenPromotion() bool

IsQueenPromotion ...

func (*Move) IsQuietMoves

func (m *Move) IsQuietMoves() bool

IsQuietMoves ...

func (*Move) IsRookPromoCapture

func (m *Move) IsRookPromoCapture() bool

IsRookPromoCapture ...

func (*Move) IsRookPromotion

func (m *Move) IsRookPromotion() bool

IsRookPromotion ...

func (*Move) Not

func (m *Move) Not(a *Move) bool

Not ...

func (*Move) SetFlags

func (m *Move) SetFlags(flags uint16)

SetFlags ...

func (*Move) SetFrom

func (m *Move) SetFrom(from uint16)

SetFrom ...

func (*Move) SetMove

func (m *Move) SetMove(move uint16)

SetMove ...

func (*Move) SetMoveFromInstance

func (m *Move) SetMoveFromInstance(m2 *Move)

SetMoveFromInstance ...

func (*Move) SetTo

func (m *Move) SetTo(to uint16)

SetTo ...

func (*Move) String

func (m *Move) String() string

String shows from and to. Essentially what piece was moved, from where it was moved and it's new position

func (*Move) To

func (m *Move) To() uint16

To ...

type MoveGen

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

MoveGen chess move generator

func NewMoveGen

func NewMoveGen() *MoveGen

NewMoveGen initiate a new MoveGen instance

func NewMoveGenByState

func NewMoveGenByState(st *GameState) *MoveGen

NewMoveGenByState creates a new movegen instance using the following state

func (*MoveGen) AddMove

func (mg *MoveGen) AddMove(move uint16)

AddMove to the stack

func (*MoveGen) Clear

func (mg *MoveGen) Clear()

Clear the moves list tracker & iterator

func (*MoveGen) CreateIterator

func (mg *MoveGen) CreateIterator() *MoveGenIterator

CreateIterator Iterator pattern

func (*MoveGen) GenerateKnightMoves

func (mg *MoveGen) GenerateKnightMoves() uint64

GenerateKnightMoves ...

func (*MoveGen) GenerateMoves

func (mg *MoveGen) GenerateMoves()

GenerateMoves generates all the moves for an active player

func (*MoveGen) GeneratePawnMoves

func (mg *MoveGen) GeneratePawnMoves() uint64

func (*MoveGen) GetMove

func (mg *MoveGen) GetMove(index uint) uint16

GetMove returns the move for a given index

func (*MoveGen) SetMove

func (mg *MoveGen) SetMove(move uint16, index int)

SetMove set a move at a specific location

func (*MoveGen) SetState

func (mg *MoveGen) SetState(st *GameState)

SetState update gamestate by reusing memory space

func (*MoveGen) Size

func (mg *MoveGen) Size() uint

Size get the number of stored moves so far

type MoveGenIterator

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

MoveGenIterator chess move generator

func NewMoveGenIterator

func NewMoveGenIterator(i, size uint, mg *MoveGen) *MoveGenIterator

NewMoveGenIterator Creates an iterator for MoveGen

func (*MoveGenIterator) Begin

func (it *MoveGenIterator) Begin() uint

Begin [iterator] return the first element

func (*MoveGenIterator) End

func (it *MoveGenIterator) End() uint

End [iterator] return the element after the last

func (*MoveGenIterator) GetIndex

func (it *MoveGenIterator) GetIndex() uint

GetIndex returns the current iterator index (progress)

func (*MoveGenIterator) GetMove

func (it *MoveGenIterator) GetMove() uint16

GetMove explicit to MoveGen and returns an encoded chess move

func (*MoveGenIterator) Good

func (it *MoveGenIterator) Good() bool

Good checks if the iteration is within bounds

func (*MoveGenIterator) Next

func (it *MoveGenIterator) Next() uint

Next [iterator] get the next index

Jump to

Keyboard shortcuts

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