emil

package module
v0.0.0-...-6a64290 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2015 License: MIT Imports: 12 Imported by: 0

README

emil

Writing some go code to learn chess programming.

First goal:

The computer should win a game with a king and a rock against a king (KRK). So we need:

  • a chess board (done)
  • king and rock pieces (done)
  • movements for king and rock (done)
  • delete illegal moves (done)
  • create an endgame database for KRK
    • Step 1: Generating all possible positions (done)
    • Step 2: Evaluating positions using retrograde analysis (done)
    • Step 3: Verification
  • play for checkmate
retrograde analysis:
  • generating all 249.984 (64 x 63 x 62) positions for KRK take 10s
  • filter 26.040 illegal positions, where the kings are to close
  • remaining 223.944 positions for analysis
  • found 216 checkmates and 68 patt positions in < 1s, analysing only 13.144 positions where
    • the black king is on a border square and
    • the distance between the kings is 2
profiling:
db.FindMatesIn 0:     216 boards
db.FindMatesIn 1:   1.512 boards
db.FindMatesIn 2:     624 boards
db.FindMatesIn 3:   4.676 boards
db.FindMatesIn 4:   1.948 boards
db.FindMatesIn 5:   3.852 boards
db.FindMatesIn 6:     648 boards
db.FindMatesIn 7:   1.900 boards
db.FindMatesIn 8:   1.584 boards
db.FindMatesIn 9:   4.848 boards
db.FindMatesIn 10:  3.768 boards
db.FindMatesIn 11:  8.708 boards
db.FindMatesIn 12:  4.728 boards
db.FindMatesIn 13: 11.320 boards
db.FindMatesIn 14:  5.444 boards
db.FindMatesIn 15: 17.172 boards
db.FindMatesIn 16: 11.448 boards
db.FindMatesIn 17: 20.088 boards
db.FindMatesIn 18: 13.672 boards
db.FindMatesIn 19: 19.016 boards
db.FindMatesIn 20: 15.872 boards
db.FindMatesIn 21: 20.476 boards
db.FindMatesIn 22: 22.788 boards
db.FindMatesIn 23: 21.480 boards
db.FindMatesIn 24: 28.732 boards
db.FindMatesIn 25: 17.824 boards
db.FindMatesIn 26: 33.516 boards
db.FindMatesIn 27: 16.136 boards
db.FindMatesIn 28: 36.372 boards
db.FindMatesIn 29:  5.244 boards
db.FindMatesIn 30: 17.284 boards
db.FindMatesIn 31:    916 boards
db.FindMatesIn 32:  3.056 boards
db.FindMatesIn 33:      0 boards

duration 16m0.7s

Documentation

Index

Constants

View Source
const (
	WHITE = 1
	BLACK = -1
)

Players

View Source
const (
	//FILES the files of a board
	FILES = "abcdefgh"

	//SQUARES the number of quares
	SQUARES = 64
)
View Source
const (
	Empty = 0

	WhiteKing   = WHITE * kingValue
	WhiteQueen  = WHITE * queenValue
	WhiteRock   = WHITE * rockValue
	WhiteBishop = WHITE * bishopValue
	WhiteKnight = WHITE * knightValue
	WhitePawn   = WHITE * pawnValue

	BlackKing   = BLACK * kingValue
	BlackQueen  = BLACK * queenValue
	BlackRock   = BLACK * rockValue
	BlackBishop = BLACK * bishopValue
	BlackPawn   = BLACK * pawnValue
	BlackKnight = BLACK * knightValue
)

Pieces values

View Source
const (
	A1 = 0
	B1 = 1
	C1 = 2
	D1 = 3
	E1 = 4
	F1 = 5
	G1 = 6
	H1 = 7
	A2 = 8
	B2 = 9
	C2 = 10
	D2 = 11
	E2 = 12
	F2 = 13
	G2 = 14
	H2 = 15
	A3 = 16
	B3 = 17
	C3 = 18
	D3 = 19
	E3 = 20
	F3 = 21
	G3 = 22
	H3 = 23
	A4 = 24
	B4 = 25
	C4 = 26
	D4 = 27
	E4 = 28
	F4 = 29
	G4 = 30
	H4 = 31
	A5 = 32
	B5 = 33
	C5 = 34
	D5 = 35
	E5 = 36
	F5 = 37
	G5 = 38
	H5 = 39
	A6 = 40
	B6 = 41
	C6 = 42
	D6 = 43
	E6 = 44
	F6 = 45
	G6 = 46
	H6 = 47
	A7 = 48
	B7 = 49
	C7 = 50
	D7 = 51
	E7 = 52
	F7 = 53
	G7 = 54
	H7 = 55
	A8 = 56
	B8 = 57
	C8 = 58
	D8 = 59
	E8 = 60
	F8 = 61
	G8 = 62
	H8 = 63
)

Board indexes

View Source
const (
	North     = 8
	South     = -8
	West      = -1
	East      = 1
	NorthWest = 7
	NorthEast = 9
	SouthWest = -9
	SouthEast = -7
)

Directions in terms of board index

Variables

View Source
var (
	//FirstSquares of rank a to print the board
	FirstSquares = [...]int{A8, A7, A6, A5, A4, A3, A2, A1}

	//BoardSquares an array of *Square of the board
	BoardSquares [SQUARES]*Square
)
View Source
var (
	Pieces = map[int]string{
		WhiteKing:   "K",
		BlackKing:   "k",
		WhiteQueen:  "Q",
		BlackQueen:  "q",
		WhiteRock:   "R",
		BlackRock:   "r",
		WhiteBishop: "B",
		BlackBishop: "b",
		WhiteKnight: "N",
		BlackKnight: "n",
		WhitePawn:   "P",
		BlackPawn:   "p",
		Empty:       " ",
	}
	Symbols = map[string]int{
		"K": WhiteKing,
		"k": BlackKing,
		"Q": WhiteQueen,
		"q": BlackQueen,
		"R": WhiteRock,
		"r": BlackRock,
		"B": WhiteBishop,
		"b": BlackBishop,
		"N": WhiteKnight,
		"n": BlackKnight,
		"P": WhitePawn,
		"p": BlackPawn,
		" ": Empty,
	}
)

Pieces symbols

View Source
var DEBUG = false

DEBUG set to write cpu.po

View Source
var IN_TEST = false
View Source
var PROFILE = false

PROFILE set to true for log messages

Functions

func CanPlayerCaptureKing

func CanPlayerCaptureKing(p *Position) (kingInCheck bool)

func SaveEndGameDb

func SaveEndGameDb(file string, analysisStr map[string]string) error

SaveEndGameDb saves the an end game DB for KRK to file

Types

type Analysis

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

func NewAnalysis

func NewAnalysis(board *Board) *Analysis

func (*Analysis) BestMove

func (a *Analysis) BestMove(player int) (bestMove *Move)

func (*Analysis) DTMs

func (a *Analysis) DTMs(player int) []*DTM

func (*Analysis) String

func (a *Analysis) String() string

type Board

type Board struct {
	Squares []int
	// contains filtered or unexported fields
}

Board with an array of field values, representing pieces

func Fen2Board

func Fen2Board(fen string) *Board

Fen2Board creates a new board from a fen string

func NewBoard

func NewBoard() *Board

NewBoard creates a new board

func (*Board) BlackKing

func (b *Board) BlackKing() int

func (*Board) DoMove

func (b *Board) DoMove(m *Move) (newBoard *Board)

func (*Board) Empty

func (b *Board) Empty(square int)

Empty removes a piece from a square

func (*Board) Picture

func (b *Board) Picture() string

func (*Board) Setup

func (b *Board) Setup(piece, square int) (noError error)

Setup a piece on a square

func (*Board) Square

func (b *Board) Square(i int) int

func (*Board) String

func (b *Board) String() string

func (*Board) WhiteKing

func (b *Board) WhiteKing() int

type DTM

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

func DTMsFromString

func DTMsFromString(s string) (list []*DTM)

func (*DTM) String

func (d *DTM) String() string

type EndGameDb

type EndGameDb struct {
	Start       time.Time
	Duration    time.Duration
	AnalysisMap map[string]*Analysis
	AnalysisStr map[string]string
}

EndGameDb to query for mate in 1,2, etc.

func LoadEndGameDb

func LoadEndGameDb() (db *EndGameDb, err error)

func NewEndGameDb

func NewEndGameDb() *EndGameDb

NewEndGameDb generates an end game DB for KRK

func (*EndGameDb) CreateAnalysisStr

func (db *EndGameDb) CreateAnalysisStr()

func (*EndGameDb) Find

func (db *EndGameDb) Find(p *Position) (bestMove *Move)

func (*EndGameDb) FindMate

func (db *EndGameDb) FindMate(piece, square int) (boards []*Board)

func (*EndGameDb) FindMates

func (db *EndGameDb) FindMates() (as []*Analysis)

func (*EndGameDb) FindMatesIn

func (db *EndGameDb) FindMatesIn(dtm int) (as []*Analysis)

type EndGameSave

type EndGameSave struct {
	AnalysisMap map[string]string `json:"analysis"`
}

type Move

type Move struct {
	Player      int
	Piece       int
	Capture     int
	Source      int
	Destination int
	IsCapture   bool
}

Move represents a move on the board

func GenerateMoves

func GenerateMoves(p *Position) (list []*Move)

func MoveFromString

func MoveFromString(str string) *Move

MoveFromString parses a string like Ke1e2 to a *Move

func Search(p *Position) (bestMove *Move)

Search best move for player on board

func (*Move) String

func (m *Move) String() string

type Position

type Position struct {
	Board  *Board
	Player int
}

func NewPosition

func NewPosition(board *Board, player int) *Position

func PositionFromKey

func PositionFromKey(key string) *Position

func (*Position) String

func (p *Position) String() string

type PositionDb

type PositionDb struct {
	Positions map[PositionKey]*PositionEntry
}

PositionDb to query for mate in 1,2, etc.

func LoadPositionDb

func LoadPositionDb(file string) (db *PositionDb, err error)

func NewPositionDB

func NewPositionDB() *PositionDb

NewPositionDB creates a new *PositionDB

func (*PositionDb) AddPrevPositions

func (db *PositionDb) AddPrevPositions()

func (*PositionDb) FillWithKRKPositions

func (db *PositionDb) FillWithKRKPositions()

func (*PositionDb) FindWhitePosition

func (db *PositionDb) FindWhitePosition(fen string, result *PositionEntry) error

FindWhitePosition serves with rpc

func (*PositionDb) SavePositionDb

func (db *PositionDb) SavePositionDb(file string) error

SaveEndGameDb saves the an end game DB for KRK to file

type PositionEntry

type PositionEntry struct {
	Position      *Position
	Dtm           int
	PrevPositions map[PositionKey]*Move
	NextPositions map[PositionKey]*Move
}

PositionEntry is an entry in the PositionDb

func NewPositionEntry

func NewPositionEntry(p *Position) *PositionEntry

NewPositionEntry ceates a new *PositionEntry

type PositionKey

type PositionKey string

type Square

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

Square of a chess board

func (*Square) String

func (s *Square) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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