game

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 2 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const WinThreshold = 6

WinThreshold is the number of captures needed to win

Variables

View Source
var (
	DirE  = Hex{1, 0}
	DirW  = Hex{-1, 0}
	DirNE = Hex{1, -1}
	DirSW = Hex{-1, 1}
	DirNW = Hex{0, -1}
	DirSE = Hex{0, 1}

	AllDirections = []Hex{DirE, DirW, DirNE, DirSW, DirNW, DirSE}
)

Direction constants for the 6 hex directions

View Source
var (
	ErrInvalidMarbleCount = errors.New("must move 1, 2, or 3 marbles")
	ErrNotYourMarble      = errors.New("can only move your own marbles")
	ErrNotCollinear       = errors.New("marbles must be adjacent and collinear")
	ErrInvalidDirection   = errors.New("invalid direction")
	ErrMoveBlocked        = errors.New("move is blocked")
	ErrCannotPush         = errors.New("cannot push: no numerical superiority")
	ErrGameOver           = errors.New("game is over")
	ErrPushBlocked        = errors.New("push blocked: no space behind opponent marbles")
)

Functions

func ApplyMove

func ApplyMove(g *Game, marbles []Hex, dir Hex) error

ApplyMove validates and applies a move to the game.

func IsValid

func IsValid(h Hex) bool

IsValid returns true if the hex is a valid board position

Types

type Board

type Board struct {
	Cells map[Hex]Cell
}

Board represents the Abalone hex board Valid cells satisfy |q| <= 4, |r| <= 4, |q+r| <= 4 (61 cells total)

func NewBoard

func NewBoard() *Board

NewBoard creates a board with the standard starting position

func (*Board) Clone

func (b *Board) Clone() *Board

Clone returns a deep copy of the board

func (*Board) Get

func (b *Board) Get(h Hex) Cell

Get returns the cell state at the given position, or Empty if off-board

func (*Board) Set

func (b *Board) Set(h Hex, c Cell)

Set sets the cell state (only for valid positions)

type Cell

type Cell int

Cell represents the state of a board cell

const (
	Empty Cell = iota
	Black
	White
)

func Opponent

func Opponent(c Cell) Cell

Opponent returns the opposite color

type Game

type Game struct {
	Board         *Board
	Turn          Cell // Black or White
	CapturedBlack int  // black marbles pushed off (white's score)
	CapturedWhite int  // white marbles pushed off (black's score)
	Winner        Cell // Empty if game ongoing
}

Game holds the full game state

func NewGame

func NewGame() *Game

NewGame creates a new game with standard starting position

func (*Game) CheckWinner

func (g *Game) CheckWinner()

CheckWinner updates the Winner field if someone has won

func (*Game) IsOver

func (g *Game) IsOver() bool

IsOver returns true if the game has ended

func (*Game) SwitchTurn

func (g *Game) SwitchTurn()

SwitchTurn switches to the other player

type Hex

type Hex struct {
	Q, R int
}

Hex represents axial coordinates on the hex board

func (Hex) Neighbor

func (h Hex) Neighbor(dir Hex) Hex

Neighbor returns the adjacent hex in the given direction

type Move

type Move struct {
	Marbles   []Hex
	Direction Hex
}

Move represents a player's move

Jump to

Keyboard shortcuts

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