bot

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package bot plays TwixT. One alpha-beta search backs three effort tiers and the hint feature; the tiers differ in budget, depth, candidate width and how much of the evaluation they are allowed to see.

Index

Constants

View Source
const NoChain = -1

NoChain is the Dist of a side that can no longer join its two borders at all, because the opponent's links seal every route. It is a distinct value rather than a large number so that a caller cannot mistake it for a peg count.

Variables

View Source
var ErrNoMove = errors.New("bot: no legal placement available")

ErrNoMove reports a position where the bot has nowhere to play.

Functions

func TierNames

func TierNames() []string

TierNames returns the tier names weakest first.

func TierSummary

func TierSummary(name string) string

TierSummary returns the one-line description of a named tier, for help text and shell completion. An unknown name returns the empty string.

Types

type Bot

type Bot interface {
	Tier() Tier
	// Move returns the hole to play. It never returns an illegal move.
	//
	// A deadline or a cancellation on ctx cuts the search short and Move
	// returns the best move it had reached, which is how a per-move budget is
	// enforced; it is not an error, and even an already-cancelled context
	// yields a legal move chosen by the ordering heuristic. An error means the
	// position genuinely has no move: the game is over, or there is nowhere
	// left to play.
	Move(ctx context.Context, g *game.Game) (game.Point, error)
	// Hint explains the move the bot would consider best for the side to move.
	// A hint is always computed with the strongest settings the package has,
	// whatever tier is playing, so that a beginner-tier game does not also give
	// beginner-tier advice.
	Hint(ctx context.Context, g *game.Game) (Hint, error)
}

Bot plays one side of a game.

A Bot carries the working state of its search and is not safe for concurrent use: give each game its own Bot.

func New

func New(t Tier, seed int64) Bot

New returns a bot of the given tier. The seed fixes its choices: the same seed in the same position always produces the same move, which is what makes a game reproducible and a strength measurement repeatable.

type Hint

type Hint struct {
	Move game.Point
	// Headline is one short line: what to play.
	Headline string
	// Detail is one to three short sentences saying why, in terms of the
	// numbers the search actually measured.
	Detail string
	// Highlight lists the holes the explanation refers to, for the board to
	// mark.
	Highlight []game.Point
}

Hint is a recommended move together with an explanation derived from the search that found it.

type Terms

type Terms struct {
	// Dist is the number of pegs the side still needs to finish its chain.
	// Zero means the chain is complete; NoChain means there is no route left.
	Dist int
	// OppDist is the same count for the opponent.
	OppDist int
	// Bottlenecks counts the holes every one of the side's cheapest chains has
	// to run through, so a single opposing peg would set the plan back. Zero
	// means every step of the plan has an alternative.
	Bottlenecks int
	// OppBottlenecks is the same count for the opponent.
	OppBottlenecks int
	// Ground is how much more of the board the side can reach cheaply than the
	// opponent, in parts per thousand of the usable holes. It is negative when
	// the opponent has the wider reach.
	Ground int
}

Terms is the decomposition of a static evaluation, in the units the hint feature talks about. Every score the search produces at a leaf is a function of these numbers and nothing else, which is what makes a derived explanation checkable.

func (Terms) Score

func (t Terms) Score() int

Score is the static value of the position for the side the terms describe.

type Tier

type Tier int

Tier names how much effort the bot spends on a move.

const (
	Beginner Tier = iota
	Intermediate
	Pro
)

The available tiers, weakest first.

func ParseTier

func ParseTier(s string) (Tier, error)

ParseTier reads a tier name, ignoring case and surrounding space.

func (Tier) String

func (t Tier) String() string

String returns the tier's name.

Jump to

Keyboard shortcuts

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