engine

package
v0.0.0-...-3f69db4 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const MaxRolls = 3
View Source
const MaxRounds = 13
View Source
const UpperBonusThreshold = 63
View Source
const UpperBonusValue = 35

Variables

View Source
var UpperCategories = []Category{Ones, Twos, Threes, Fours, Fives, Sixes}

Functions

func CalcScore

func CalcScore(c Category, dice [5]int) int

func IsValidCategory

func IsValidCategory(c Category) bool

func Reroll

func Reroll(dice [5]int, hold []int, src rand.Source) [5]int

func RollAll

func RollAll(src rand.Source) [5]int

Types

type AIPlayer

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

func NewAIPlayer

func NewAIPlayer(game *Game, playerID string) *AIPlayer

func NewAIPlayerWithStrategy

func NewAIPlayerWithStrategy(game *Game, playerID string, strategy Strategy) *AIPlayer

func (*AIPlayer) PlayTurn

func (ai *AIPlayer) PlayTurn() (AITurnResult, error)

type AITurnResult

type AITurnResult struct {
	PlayerName   string
	Dice         [5]int
	Category     Category
	Score        int
	StrategyName string
	HoldHistory  []HoldStep
}

type BattleConfig

type BattleConfig struct {
	Players    []BattlePlayer
	Seed       int64
	OnTurnDone func(result AITurnResult)
}

BattleConfig holds the configuration for a battle.

type BattlePlayer

type BattlePlayer struct {
	Name     string
	Strategy Strategy
}

BattlePlayer represents a player in a battle.

type BattlePlayerResult

type BattlePlayerResult struct {
	Name  string
	Score int
}

BattlePlayerResult holds the final result for a single player.

type BattleResult

type BattleResult struct {
	Players []BattlePlayerResult
}

BattleResult holds the final results of a battle.

type Category

type Category string
const (
	Ones          Category = "ones"
	Twos          Category = "twos"
	Threes        Category = "threes"
	Fours         Category = "fours"
	Fives         Category = "fives"
	Sixes         Category = "sixes"
	ThreeOfAKind  Category = "three_of_a_kind"
	FourOfAKind   Category = "four_of_a_kind"
	FullHouse     Category = "full_house"
	SmallStraight Category = "small_straight"
	LargeStraight Category = "large_straight"
	Yahtzee       Category = "yahtzee"
	Chance        Category = "chance"
)

type Game

type Game struct {
	Players   []Player
	Current   int
	Round     int
	Dice      [5]int
	RollCount int
	Phase     GamePhase
	// contains filtered or unexported fields
}

func NewGame

func NewGame(playerNames []string, src rand.Source) *Game

func (*Game) GetAvailableCategories

func (g *Game) GetAvailableCategories() []Category

func (*Game) GetScorecard

func (g *Game) GetScorecard(playerID string) *Scorecard

func (*Game) GetState

func (g *Game) GetState() GameState

func (*Game) Hold

func (g *Game) Hold(indices []int) error

func (*Game) Roll

func (g *Game) Roll() error

func (*Game) Score

func (g *Game) Score(category Category) error

type GameClient

type GameClient interface {
	Roll() (*GameState, error)
	Hold(indices []int) (*GameState, error)
	Score(category Category) (*GameState, error)
	GetState() (*GameState, error)
}

type GamePhase

type GamePhase int
const (
	PhaseWaiting  GamePhase = iota // reserved, unused in v1
	PhaseRolling                   // Player can Roll() or Hold()
	PhaseChoosing                  // Player must Score() (after 3 rolls)
	PhaseFinished                  // Game over
)

type GameState

type GameState struct {
	Players             []PlayerState
	CurrentPlayer       string
	CurrentPlayerIndex  int
	Round               int
	Dice                [5]int
	RollCount           int
	Phase               GamePhase
	AvailableCategories []Category
}

func RunBattle

func RunBattle(cfg BattleConfig) (*GameState, error)

RunBattle executes a full AI-vs-AI game and returns the final state.

type GreedyStrategy

type GreedyStrategy struct{}

GreedyStrategy always scores immediately with the highest-scoring available category. It never uses Hold to reroll.

func (*GreedyStrategy) DecideAction

func (s *GreedyStrategy) DecideAction(dice [5]int, rollCount int, scorecard Scorecard, available []Category) TurnAction

func (*GreedyStrategy) Name

func (s *GreedyStrategy) Name() string

type HoldStep

type HoldStep struct {
	Dice [5]int
	Held []int
}

HoldStep records a hold decision during a turn.

type LocalClient

type LocalClient struct {
	LastAIResults []AITurnResult
	// contains filtered or unexported fields
}

func NewLocalClient

func NewLocalClient(game *Game, playerID string, ais []*AIPlayer) *LocalClient

func (*LocalClient) GetState

func (c *LocalClient) GetState() (*GameState, error)

func (*LocalClient) Hold

func (c *LocalClient) Hold(indices []int) (*GameState, error)

func (*LocalClient) Roll

func (c *LocalClient) Roll() (*GameState, error)

func (*LocalClient) Score

func (c *LocalClient) Score(category Category) (*GameState, error)

type Player

type Player struct {
	ID        string
	Name      string
	Scorecard Scorecard
}

type PlayerState

type PlayerState struct {
	ID        string
	Name      string
	Scorecard Scorecard
}

type Scorecard

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

func NewScorecard

func NewScorecard() Scorecard

func (*Scorecard) AvailableCategories

func (sc *Scorecard) AvailableCategories() []Category

func (*Scorecard) Fill

func (sc *Scorecard) Fill(c Category, score int)

func (*Scorecard) GetScore

func (sc *Scorecard) GetScore(c Category) int

func (*Scorecard) HasUpperBonus

func (sc *Scorecard) HasUpperBonus() bool

func (*Scorecard) IsFilled

func (sc *Scorecard) IsFilled(c Category) bool

func (Scorecard) MarshalJSON

func (sc Scorecard) MarshalJSON() ([]byte, error)

func (*Scorecard) Total

func (sc *Scorecard) Total() int

func (*Scorecard) UnmarshalJSON

func (sc *Scorecard) UnmarshalJSON(data []byte) error

func (*Scorecard) UpperTotal

func (sc *Scorecard) UpperTotal() int

type StatisticalStrategy

type StatisticalStrategy struct{}

StatisticalStrategy uses expected value calculation to decide whether to hold or score. On the 3rd roll it always scores the best category. Otherwise it compares immediate scoring vs expected value of each hold combination.

func (*StatisticalStrategy) DecideAction

func (s *StatisticalStrategy) DecideAction(dice [5]int, rollCount int, scorecard Scorecard, available []Category) TurnAction

func (*StatisticalStrategy) Name

func (s *StatisticalStrategy) Name() string

type Strategy

type Strategy interface {
	Name() string
	DecideAction(dice [5]int, rollCount int, scorecard Scorecard, available []Category) TurnAction
}

Strategy defines the interface for AI decision-making.

type TurnAction

type TurnAction struct {
	Type     string   // "hold" or "score"
	Indices  []int    // hold: dice indices to keep
	Category Category // score: category to score in
}

TurnAction represents a decision made by a Strategy during a turn.

Jump to

Keyboard shortcuts

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