bgammon

package module
v0.0.0-...-5113d37 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: AGPL-3.0 Imports: 10 Imported by: 4

README

bgammon - Backgammon server powering bgammon.org

GoDoc Donate via LiberaPay

Play

Browser and Desktop

Click here to play using your browser.

Download Boxcars at https://bgammon.org/download

Source code is available here.

Android

Google Play   F-Droid

Download Boxcars via Google Play or F-Droid

Source code is available here.

Terminal

ssh bgammon.org -p 5000

Source code is available here.

Translate

Translation is handled online.

Support

Please share issues and suggestions here.

The bgammon protocol is specified in PROTOCOL.md.

Information on how to play backgammon is available here.

Documentation

Overview

Package bgammon provides an interface for connecting to the bgammon.org online backgammon server.

Index

Constants

View Source
const (
	SpaceHomePlayer   int8 = 0  // Current player's home.
	SpaceHomeOpponent int8 = 25 // Opponent player's home.
	SpaceBarPlayer    int8 = 26 // Current player's bar.
	SpaceBarOpponent  int8 = 27 // Opponent player's bar.
)
View Source
const (
	CommandLogin         = "login"         // Log in with username and password, or as a guest.
	CommandLoginJSON     = "loginjson"     // Log in with username and password, or as a guest, and enable JSON messages.
	CommandRegister      = "register"      // Register an account.
	CommandRegisterJSON  = "registerjson"  // Register an account and enable JSON messages.
	CommandResetPassword = "resetpassword" // Request password reset link via email.
	CommandPassword      = "password"      // Change password.
	CommandSet           = "set"           // Change account setting.
	CommandReplay        = "replay"        // Retrieve replay.
	CommandHistory       = "history"       // Retrieve match history.
	CommandHelp          = "help"          // Print help information.
	CommandJSON          = "json"          // Enable or disable JSON formatted messages.
	CommandSay           = "say"           // Send chat message.
	CommandList          = "list"          // List available matches.
	CommandCreate        = "create"        // Create match.
	CommandJoin          = "join"          // Join match.
	CommandLeave         = "leave"         // Leave match.
	CommandDouble        = "double"        // Offer double to opponent.
	CommandResign        = "resign"        // Decline double offer and resign game.
	CommandRoll          = "roll"          // Roll dice.
	CommandMove          = "move"          // Move checkers.
	CommandReset         = "reset"         // Reset checker movement.
	CommandOk            = "ok"            // Confirm checker movement and pass turn to next player.
	CommandRematch       = "rematch"       // Confirm checker movement and pass turn to next player.
	CommandBoard         = "board"         // Print current board state in human-readable form.
	CommandPong          = "pong"          // Response to server ping.
	CommandDisconnect    = "disconnect"    // Disconnect from server.
)
View Source
const (
	EventTypeWelcome     = "welcome"
	EventTypeHelp        = "help"
	EventTypePing        = "ping"
	EventTypeNotice      = "notice"
	EventTypeSay         = "say"
	EventTypeList        = "list"
	EventTypeJoined      = "joined"
	EventTypeFailedJoin  = "failedjoin"
	EventTypeLeft        = "left"
	EventTypeFailedLeave = "failedleave"
	EventTypeBoard       = "board"
	EventTypeRolled      = "rolled"
	EventTypeFailedRoll  = "failedroll"
	EventTypeMoved       = "moved"
	EventTypeFailedMove  = "failedmove"
	EventTypeFailedOk    = "failedok"
	EventTypeWin         = "win"
	EventTypeSettings    = "settings"
	EventTypeReplay      = "replay"
	EventTypeHistory     = "history"
)
View Source
const (
	SpeedSlow    int8 = 0
	SpeedMedium  int8 = 1
	SpeedFast    int8 = 2
	SpeedInstant int8 = 3
)
View Source
const (
	VariantBackgammon int8 = 0
	VariantAceyDeucey int8 = 1
	VariantTabula     int8 = 2
)
View Source
const BoardSpaces = 28

BoardSpaces is the total number of spaces needed to represent a backgammon board.

Variables

This section is empty.

Functions

func DecodeEvent

func DecodeEvent(message []byte) (interface{}, error)

func FlipMoves

func FlipMoves(moves [][]int8, player int8, variant int8) [][]int8

func FlipSpace

func FlipSpace(space int8, player int8, variant int8) int8

func FormatAndFlipMoves

func FormatAndFlipMoves(moves [][]int8, player int8, variant int8) []byte

func FormatMoves

func FormatMoves(moves [][]int8) []byte

func FormatSpace

func FormatSpace(space int8) []byte

func HomeRange

func HomeRange(player int8, variant int8) (from int8, to int8)

HomeRange returns the start and end space of the provided player's home board.

func IterateSpaces

func IterateSpaces(from int8, to int8, variant int8, f func(space int8, spaceCount int8))

func NewBoard

func NewBoard(variant int8) []int8

NewBoard returns a new backgammon board represented as integers. Positive integers represent player 1's checkers and negative integers represent player 2's checkers. The board's space numbering is always from the perspective of the current player (i.e. the 1 space will always be in the current player's home board).

func OpponentCheckers

func OpponentCheckers(checkers int8, player int8) int8

func ParseSpace

func ParseSpace(space string) int8

func PlayerCheckers

func PlayerCheckers(checkers int8, player int8) int8

func RollForMove

func RollForMove(from int8, to int8, player int8, variant int8) int8

RollForMove returns the roll needed to move a checker from the provided spaces.

func SortMoves

func SortMoves(moves [][]int8)

SortMoves sorts moves from highest to lowest.

func SpaceDiff

func SpaceDiff(from int8, to int8, variant int8) int8

func ValidSpace

func ValidSpace(space int8) bool

Types

type Client

type Client interface {
	HandleReadWrite()
	Write(message []byte)
	Terminate(reason string)
	Terminated() bool
}

type Command

type Command string

type Event

type Event struct {
	Type   string
	Player string
}

type EventBoard

type EventBoard struct {
	Event
	GameState
}

type EventFailedJoin

type EventFailedJoin struct {
	Event
	Reason string
}

type EventFailedLeave

type EventFailedLeave struct {
	Event
	Reason string
}

type EventFailedMove

type EventFailedMove struct {
	Event
	From   int8
	To     int8
	Reason string
}

type EventFailedOk

type EventFailedOk struct {
	Event
	Reason string
}

type EventFailedRoll

type EventFailedRoll struct {
	Event
	Reason string
}

type EventHelp

type EventHelp struct {
	Event
	Topic   string
	Message string
}

type EventHistory

type EventHistory struct {
	Event
	Page                   int
	Pages                  int
	Matches                []*HistoryMatch
	CasualBackgammonSingle int
	CasualBackgammonMulti  int
	CasualAceyDeuceySingle int
	CasualAceyDeuceyMulti  int
	CasualTabulaSingle     int
	CasualTabulaMulti      int
}

type EventJoined

type EventJoined struct {
	Event
	GameID       int
	PlayerNumber int8
}

type EventLeft

type EventLeft struct {
	Event
}

type EventList

type EventList struct {
	Event
	Games []GameListing
}

type EventMoved

type EventMoved struct {
	Event
	Moves [][]int8
}

type EventNotice

type EventNotice struct {
	Event
	Message string
}

type EventPing

type EventPing struct {
	Event
	Message string
}

type EventReplay

type EventReplay struct {
	Event
	ID      int
	Content []byte
}

type EventRolled

type EventRolled struct {
	Event
	Roll1    int8
	Roll2    int8
	Roll3    int8 // Used in tabula games.
	Selected bool // Whether the roll is selected by the player (used in acey-deucey games).
}

type EventSay

type EventSay struct {
	Event
	Message string
}

type EventSettings

type EventSettings struct {
	Event
	AutoPlay    bool
	Highlight   bool
	Pips        bool
	Moves       bool
	Flip        bool
	Traditional bool
	Advanced    bool
	Speed       int8
}

type EventType

type EventType string

type EventWelcome

type EventWelcome struct {
	Event
	PlayerName string
	Clients    int
	Games      int
}

type EventWin

type EventWin struct {
	Event
	Points int8
}

type Game

type Game struct {
	Started time.Time
	Ended   time.Time

	Player1 Player
	Player2 Player

	Variant int8 // 0 - Backgammon, 1 - Acey-deucey, 2 - Tabula.
	Board   []int8
	Turn    int8

	Roll1 int8
	Roll2 int8
	Roll3 int8 // Used in tabula games.

	Moves  [][]int8 // Pending moves.
	Winner int8

	Points        int8 // Points required to win the match.
	DoubleValue   int8 // Doubling cube value.
	DoublePlayer  int8 // Player that currently posesses the doubling cube.
	DoubleOffered bool // Whether the current player is offering a double.

	Reroll bool // Used in acey-deucey.

	// Fields after this point are provided for backwards-compatibility only and will eventually be removed.
	Acey bool // For Boxcars v1.2.1 and earlier.
	// contains filtered or unexported fields
}

func NewGame

func NewGame(variant int8) *Game

func (*Game) AddLocalMove

func (g *Game) AddLocalMove(move []int8) bool

AddLocalMove adds a move without performing any validation. This is useful when adding a move locally while waiting for an EventBoard response from the server.

func (*Game) AddMoves

func (g *Game) AddMoves(moves [][]int8, local bool) (bool, [][]int8)

AddMoves adds moves to the game state. Adding a backwards move will remove the equivalent existing move.

func (*Game) BoardState

func (g *Game) BoardState(player int8, local bool) []byte

func (*Game) Copy

func (g *Game) Copy(shallow bool) *Game

func (*Game) DiceRolls

func (g *Game) DiceRolls() []int8

func (*Game) ExpandMove

func (g *Game) ExpandMove(move []int8, currentSpace int8, moves [][]int8, local bool) ([][]int8, bool)

func (*Game) HaveBearOffDiceRoll

func (g *Game) HaveBearOffDiceRoll(diff int8) int8

func (*Game) HaveDiceRoll

func (g *Game) HaveDiceRoll(from int8, to int8) int8

func (*Game) LegalMoves

func (g *Game) LegalMoves(local bool) [][]int8

func (*Game) MayBearOff

func (g *Game) MayBearOff(player int8, local bool) bool

MayBearOff returns whether the provided player may bear checkers off of the board.

func (*Game) NextPartialTurn

func (g *Game) NextPartialTurn(player int8)

func (*Game) NextTurn

func (g *Game) NextTurn(reroll bool)

func (*Game) PartialHandled

func (g *Game) PartialHandled() bool

func (*Game) PartialTime

func (g *Game) PartialTime() int

func (*Game) PartialTurn

func (g *Game) PartialTurn() int8

func (*Game) RenderSpace

func (g *Game) RenderSpace(player int8, space int8, spaceValue int8, legalMoves [][]int8) []byte

func (*Game) Reset

func (g *Game) Reset()

func (*Game) SecondHalf

func (g *Game) SecondHalf(player int8, local bool) bool

func (*Game) SetPartialHandled

func (g *Game) SetPartialHandled(handled bool)

func (*Game) TabulaBoard

func (g *Game) TabulaBoard() (tabula.Board, bool)

type GameListing

type GameListing struct {
	ID       int
	Password bool
	Points   int8
	Players  int8
	Rating   int
	Name     string
}

type GameState

type GameState struct {
	*Game
	PlayerNumber int8
	Available    [][]int8 // Legal moves.
	Forced       bool     // A forced move is being played automatically.
	Spectating   bool
}

func (*GameState) LocalPlayer

func (g *GameState) LocalPlayer() Player

func (*GameState) MayChooseRoll

func (g *GameState) MayChooseRoll() bool

MayChooseRoll returns whether the player may send the 'ok' command, supplying the chosen roll. This command only applies to acey-deucey games.

func (*GameState) MayDouble

func (g *GameState) MayDouble() bool

MayDouble returns whether the player may send the 'double' command.

func (*GameState) MayOK

func (g *GameState) MayOK() bool

MayOK returns whether the player may send the 'ok' command.

func (*GameState) MayReset

func (g *GameState) MayReset() bool

MayReset returns whether the player may send the 'reset' command.

func (*GameState) MayResign

func (g *GameState) MayResign() bool

MayResign returns whether the player may send the 'resign' command.

func (*GameState) MayRoll

func (g *GameState) MayRoll() bool

MayRoll returns whether the player may send the 'roll' command.

func (*GameState) OpponentPlayer

func (g *GameState) OpponentPlayer() Player

func (*GameState) Pips

func (g *GameState) Pips(player int8) int

Pips returns the pip count for the specified player.

func (*GameState) SpaceAt

func (g *GameState) SpaceAt(x int8, y int8) int8

type HistoryMatch

type HistoryMatch struct {
	ID        int
	Timestamp int64
	Points    int8
	Opponent  string
	Winner    int8
}

type Player

type Player struct {
	Number   int8 // 1 black, 2 white
	Name     string
	Rating   int
	Points   int8
	Entered  bool // Whether all checkers have entered the board. (Acey-deucey)
	Inactive int  // Inactive time. (Seconds)
}

func NewPlayer

func NewPlayer(number int8) Player

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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