uchess

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2021 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const MaxLength = 10

MaxLength defines the MaxWidth of the input buffer

Variables

View Source
var DefStyle = tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)

DefStyle is the default style for tcell rendering

View Source
var DefaultOptions = []Option{
	{"skill level", "3"},
}

DefaultOptions defines any default UCI options

View Source
var ThemeBasic = Theme{
	"basic",
	tcell.Color252,
	tcell.ColorBlack,
	tcell.Color188,
	tcell.Color230,
	tcell.Color226,
	tcell.Color223,
	tcell.Color218,
	tcell.Color232,
	tcell.Color232,
	tcell.Color160,
	tcell.Color247,
	tcell.Color247,
	tcell.Color160,
	tcell.Color240,
	tcell.ColorDefault,
	tcell.Color45,
	tcell.Color122,
	tcell.Color167,
	tcell.ColorDefault,
	tcell.Color247,
	tcell.ColorDefault,
	tcell.ColorDefault,
	tcell.ColorDefault,
	tcell.Color247,
}

ThemeBasic is the default theme

Functions

func Advantages

func Advantages(positions string) (string, string)

Advantages returns the white/black advantage strings

func AppDir

func AppDir() string

AppDir returns the uchess directory location

func AtScale

func AtScale(idx, tot int, val float64) bool

AtScale returns a boolean value indicating whether the given value (val) has crossed the threshold (idx) established by dividing total (tot) into equal increments

func ConfigJSON

func ConfigJSON(config Config) string

ConfigJSON returns the JSON encoded representation of the config

func DownloadFile

func DownloadFile(url string, filepath string) error

DownloadFile will download a url and store it in local filepath. It writes to the destination file as it downloads it, without loading the entire file into memory. We pass an io.TeeReader into Copy() to report progress on the download.

func DrawMsgLabel

func DrawMsgLabel(s tcell.Screen, msg string, t Theme)

DrawMsgLabel displays the current message from the command

func EmojiForPlayer

func EmojiForPlayer(playerType string) string

EmojiForPlayer returns an emoji corresponding to the player type

func EngMove

func EngMove(game *chess.Game, us UCIState, config Config) string

EngMove executes a move using the UCI engine

func EngScore

func EngScore(game *chess.Game, us UCIState, config Config) int

EngScore provides the current board score in centipawns for whomever the current game position identifies as active

func FetchStockfish

func FetchStockfish() string

FetchStockfish attempts to load Stockfish via the path and attempts to download the binary for a limited number of platforms

func FileExists

func FileExists(path string) bool

FileExists returns a bool indicating whether the specified file exists

func FindStockfish

func FindStockfish() string

FindStockfish attempts to find Stockfish in the PATH and config dir A string with the path is returned when found, an empty string is returned otherwise

func HasTheme

func HasTheme(name string, themes []ThemeHex) bool

HasTheme returns a bool indicating whether the config contains a theme of the name provided

func ImportEngines

func ImportEngines(uciWhite string, uciBlack string, uciHint string, engines []UCIEngine) (*UCIEngine, *UCIEngine, *UCIEngine)

ImportEngines returns a UCIEngine config for white and black

func InCheck

func InCheck(game *chess.Game) (bool, bool)

InCheck returns two booleans indicating if either player is in check. The first boolean represents white and the second boolean represents black

func InitEngines

func InitEngines(config Config) (*uci.Engine, *uci.Engine, *uci.Engine)

InitEngines configures UCI engines if the config dictates that they are required. The white and black engines are returned respectively

func IsCPU

func IsCPU(player chess.Color, config Config) bool

IsCPU returns a bool indicating whether the specified player is the CPU

func IsFile

func IsFile(path string) bool

IsFile returns a bool indicating whether the specified file is a regular file

func IsInteractive

func IsInteractive(config Config) bool

IsInteractive returns a bool indicating whether the game is interactive with interactive meaning a human is involved

func IsWindows

func IsWindows() bool

IsWindows returns a boolean indicating whether windows is the OS

func MatchStockfishWin

func MatchStockfishWin(file string) bool

MatchStockfishWin returns a boolean indicating whether a given file looks like the stockfish executable (Windows). Fuzzy matching is applied

func ProcessCmd

func ProcessCmd(cmd string, gs *GameState) (string, *chess.Game)

ProcessCmd processes a move request or command

func Render

func Render(gs *GameState)

Render draws the screen

func RoundNearest

func RoundNearest(x, unit float64) float64

RoundNearest rounds to the nearest unit

func ScoreStr

func ScoreStr(positions string) (string, string)

ScoreStr returns a formatted score string

func SearchDirForStockfish

func SearchDirForStockfish(dir string) string

SearchDirForStockfish scans a directory and looks for a stockfish binary. Upon matching, a string with the binary name is returned. If no match is found, an empty string is returned

func StockfishFilename

func StockfishFilename() string

StockfishFilename returns a platform specific filename for the Stockfish binary

func Timestamp

func Timestamp() string

Timestamp returns a timestamp string

func WinProb

func WinProb(cp int) float64

WinProb calculates the percentage chance that white wins

Types

type Config

type Config struct {
	UCIWhite    string      `json:"uciWhite"`
	UCIBlack    string      `json:"uciBlack"`
	UCIHint     string      `json:"uciHint"`
	UCIEngines  []UCIEngine `json:"uciEngines"`
	FEN         string      `json:"fen"`
	ActiveTheme string      `json:"activeTheme"`
	Themes      []ThemeHex  `json:"theme"`
	WhitePiece  string      `json:"whitePiece"`
	BlackPiece  string      `json:"blackPiece"`
	WhiteName   string      `json:"whiteName"`
	BlackName   string      `json:"blackName"`
}

Config defines the configurable parameters for UCI

func Init

func Init() Config

Init sets up the app config

func MakeDefault

func MakeDefault() Config

MakeDefault creates the default config

func ReadConfig

func ReadConfig(file string) Config

ReadConfig reads the specified JSON file into a config struct

type GameState

type GameState struct {
	S          tcell.Screen // Screen
	Input      *Input       // Input
	Game       *chess.Game  // Chess Board
	UCI        UCIState     // UCI State
	Config     Config       // Global Config
	Theme      Theme        // Theme
	Score      int          // Score in centipawns
	CheckWhite bool         // White is in check
	CheckBlack bool         // Black is in check
	Hint       *chess.Move  // Hint when available
}

GameState encapsulates everything needed to run the game

type Input

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

Input stores the input buffer

func NewInput

func NewInput() *Input

NewInput creates a new input buffer

func (*Input) Append

func (i *Input) Append(c rune) string

Append appends to the input buffer

func (*Input) Backspace

func (i *Input) Backspace() string

Backspace removes the last entry in the input buffer

func (*Input) Clear

func (i *Input) Clear() string

Clear clears the current buffer

func (*Input) Current

func (i *Input) Current() string

Current returns the current input buffer

func (*Input) Length

func (i *Input) Length() int

Length returns the input buffer length

type Option

type Option struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

Option allows arbitrary UCI options to be sent

type Theme

type Theme struct {
	Name         string      `json:"name"`
	MoveLabelBg  tcell.Color `json:"moveLabelBg"`
	MoveLabelFg  tcell.Color `json:"moveLabelFg"`
	SquareDark   tcell.Color `json:"squareDark"`
	SquareLight  tcell.Color `json:"squareLight"`
	SquareHigh   tcell.Color `json:"squareHigh"`
	SquareHint   tcell.Color `json:"squareHint"`
	SquareCheck  tcell.Color `json:"squareCheck"`
	White        tcell.Color `json:"white"`
	Black        tcell.Color `json:"black"`
	Msg          tcell.Color `json:"msg"`
	Rank         tcell.Color `json:"rank"`
	File         tcell.Color `json:"file"`
	Prompt       tcell.Color `json:"prompt"`
	MeterBase    tcell.Color `json:"meterBase"`
	MeterMid     tcell.Color `json:"meterMid"`
	MeterNeutral tcell.Color `json:"meterNeutral"`
	MeterWin     tcell.Color `json:"meterWin"`
	MeterLose    tcell.Color `json:"meterLose"`
	PlayerNames  tcell.Color `json:"playerNames"`
	Score        tcell.Color `json:"score"`
	MoveBox      tcell.Color `json:"moveBox"`
	Emoji        tcell.Color `json:"emoji"`
	Input        tcell.Color `json:"input"`
	Advantage    tcell.Color `json:"advantage"`
}

Theme is used for dynamically coloring the UI

func ImportThemes

func ImportThemes(want string, themes []ThemeHex) (Theme, error)

ImportThemes returns a converted Theme from a slice of ThemeHex entities if its name matches the want argument

func (Theme) Hex

func (t Theme) Hex() ThemeHex

Hex converts a Theme to a ThemeHex

type ThemeHex

type ThemeHex struct {
	Name         string `json:"name"`
	MoveLabelBg  string `json:"moveLabelBg"`
	MoveLabelFg  string `json:"moveLabelFg"`
	SquareDark   string `json:"squareDark"`
	SquareLight  string `json:"squareLight"`
	SquareHigh   string `json:"squareHigh"`
	SquareHint   string `json:"squareHint"`
	SquareCheck  string `json:"squareCheck"`
	White        string `json:"white"`
	Black        string `json:"black"`
	Msg          string `json:"msg"`
	Rank         string `json:"rank"`
	File         string `json:"file"`
	Prompt       string `json:"prompt"`
	MeterBase    string `json:"meterBase"`
	MeterMid     string `json:"meterMid"`
	MeterNeutral string `json:"meterNeutral"`
	MeterWin     string `json:"meterWin"`
	MeterLose    string `json:"meterLose"`
	PlayerNames  string `json:"playerNames"`
	Score        string `json:"score"`
	MoveBox      string `json:"moveBox"`
	Emoji        string `json:"emoji"`
	Input        string `json:"input"`
	Advantage    string `json:"advantage"`
}

ThemeHex is used for dynamically coloring the UI

func ReadThemes

func ReadThemes() []ThemeHex

ReadThemes reads packaged theme data into a ThemeHex data slice

func (ThemeHex) Theme

func (t ThemeHex) Theme() Theme

Theme converts a ThemeHex to a Theme

type UCIEngine

type UCIEngine struct {
	Name        string        `json:"name"`
	Path        string        `json:"engine"`
	Hash        int           `json:"hash"`
	Ponder      bool          `json:"ponder"`
	OwnBook     bool          `json:"ownBook"`
	MultiPV     int           `json:"multiPV"`
	Depth       int           `json:"depth"`
	SearchMoves string        `json:"searchMoves"`
	MoveTime    time.Duration `json:"moveTime"`
	Options     []Option      `json:"options"`
}

UCIEngine defines a UCIEngine configuration

type UCIState

type UCIState struct {
	UciWhite *uci.Engine // UCI Engine
	UciBlack *uci.Engine // UCI Engine
	UciHint  *uci.Engine // UCI Engine
	CfgWhite *UCIEngine  // UCI Engine Config
	CfgBlack *UCIEngine  // UCI Engine Config
	CfgHint  *UCIEngine  // UCI Engine Config
}

UCIState holds the UCI engine state

type WriteCounter

type WriteCounter struct {
	Total uint64
}

WriteCounter counts the number of bytes written to it. By implementing the Write method, it is of the io.Writer interface and we can pass this into io.TeeReader() Every write to this writer, will print the progress of the file write

func (WriteCounter) PrintProgress

func (wc WriteCounter) PrintProgress()

PrintProgress prints the progress of a file write

func (*WriteCounter) Write

func (wc *WriteCounter) Write(p []byte) (int, error)

Write updates the WriteCounter total and prints progress

Jump to

Keyboard shortcuts

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