common

package
v0.0.0-...-a9017b9 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PlayerA Player = 0 // Player A moves first
	PlayerB Player = 1 // Player B has a second move
	Empty   Player = 2
	NoMove  Player = 3

	PlayerARune = 'A'
	PlayerBRune = 'B'
	EmptyCell   = "."
)
View Source
const ItReportPeriodMask = 0b11111111111111111111 // modulo 2^20 (1048576) mask
View Source
const ProgressBarResolution = 1_000_000_000
View Source
const RefreshProgressPeriod = 2 * time.Second

Variables

View Source
var (
	MetricCacheDepthSize = promauto.NewGaugeVec(prometheus.GaugeOpts{
		Name: "c4solver_cache_depth_size",
		Help: "Current size of cached endgames for each depth",
	}, []string{"depth"})

	MetricProgress = promauto.NewGauge(prometheus.GaugeOpts{
		Name: "c4solver_progress",
		Help: "Total progress of training",
	})
	MetricTotalIterations = promauto.NewGauge(prometheus.GaugeOpts{
		Name: "c4solver_iterations",
		Help: "Total number of iterations (considered boards)",
	})
)
View Source
var CacheSizeLimit int = 1_500_000_000
View Source
var InterruptError error = errors.New("Interrupt")
View Source
var PlayerDisplays = map[Player]string{
	PlayerA: "\u001b[33;1mA\u001b[0m",
	PlayerB: "\u001b[31;1mB\u001b[0m",
	Empty:   ".",
	NoMove:  "-",
}
View Source
var ShortGameEndingDisplays = map[GameEnding]string{
	Win:      "\u001b[32;1mW\u001b[0m",
	Tie:      "\u001b[33;1mT\u001b[0m",
	Lose:     "\u001b[31;1mL\u001b[0m",
	NoEnding: "-",
}
View Source
var StackSizeLookup []int

Board column state -> stack size

Functions

func AssertEqualTrimmed

func AssertEqualTrimmed(t *testing.T, actual, expected string)

func BigintSeparated

func BigintSeparated(num uint64) string

func CacheFileExists

func CacheFileExists(board *Board) bool

func CalculateMovesOrder

func CalculateMovesOrder(board *Board) []int

func HandleInterrupt

func HandleInterrupt(interruptible Interruptible) chan os.Signal

func LoadCache

func LoadCache(cache ICache, boardW, boardH int) error

func MaximumZeroIndex

func MaximumZeroIndex(nums []uint64) int

func MustLoadCache

func MustLoadCache(cache ICache, boardW, boardH int)

func MustSaveCache

func MustSaveCache(cache ICache, boardW, boardH int)

func NewProgressBar

func NewProgressBar() *progressbar.ProgressBar

func ReverseLines

func ReverseLines(s []string) []string

func SaveCache

func SaveCache(cache ICache, boardW, boardH int) error

func ServeMetrics

func ServeMetrics()

Types

type Board

type Board struct {
	W         int
	H         int
	WinStreak int
	State     BoardState
}

Board keeps state in a binary format: - first binary One signifies stack height - next bits signifies Player A (zero) or Player B (one)

func NewBoard

func NewBoard(options ...Option) *Board

func ParseBoard

func ParseBoard(txt string, options ...Option) *Board

func (*Board) ApplyMoves

func (b *Board) ApplyMoves(startWithMoves string) *Board

func (*Board) CanMakeMove

func (b *Board) CanMakeMove(x int) bool

func (*Board) Clear

func (b *Board) Clear()

func (*Board) Clone

func (b *Board) Clone() *Board

func (*Board) CountMoves

func (b *Board) CountMoves() uint

func (*Board) GetCell

func (b *Board) GetCell(x int, y int) Player

GetCell return token at given cell coordinates (axes oriented top-right)

func (*Board) NextPlayer

func (b *Board) NextPlayer() Player

func (*Board) Revert

func (b *Board) Revert(x int, y int)

func (*Board) StackSize

func (b *Board) StackSize(x int) int

func (*Board) String

func (b *Board) String() string

func (*Board) Throw

func (b *Board) Throw(x int, player Player) int

type BoardState

type BoardState []uint64

Slice of column states (a column is uint8, but keeping as uint64 improves operations later on)

type GameEnding

type GameEnding string
const (
	Win      GameEnding = "\u001b[32;1mWin\u001b[0m"
	Tie      GameEnding = "\u001b[33;1mTie\u001b[0m"
	Lose     GameEnding = "\u001b[31;1mLose\u001b[0m"
	NoEnding GameEnding = "None"
)

func EndingForPlayer

func EndingForPlayer(ending Player, player Player) GameEnding

func (GameEnding) String

func (e GameEnding) String() string

type ICache

type ICache interface {
	Get(board *Board, depth uint) (ending Player, ok bool)
	Put(board *Board, depth uint, ending Player) Player
	ClearCache(depth uint)
	Size() uint64
	DepthSize(depth uint) int
	MaxCachedDepth() uint
	DepthCaches() []map[uint64]Player
	SetEntry(depth int, key uint64, value Player)
}

type IMoveSolver

type IMoveSolver interface {
	MovesEndings(board *Board) []Player
	HasPlayerWon(board *Board, move int, y int, player Player) bool
	SummaryVars() log.Ctx
	Cache() ICache
	Retrain(board *Board, maxDepth uint)
}

type Interruptible

type Interruptible interface {
	Interrupt()
}

type Mode

type Mode string
const (
	TrainMode  Mode = "train"
	PlayMode   Mode = "play"
	BrowseMode Mode = "browse"
)

type Option

type Option func(*Board) error

func WithSize

func WithSize(width int, height int) Option

func WithWinStreak

func WithWinStreak(winStreak int) Option

type Player

type Player uint8

func OppositePlayer

func OppositePlayer(player Player) Player

func (Player) String

func (p Player) String() string

Jump to

Keyboard shortcuts

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