go_boardgame

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2023 License: MIT Imports: 4 Imported by: 9

README

Go-boardgame

GoDoc

Go-boardgame is a simple Go package that can be used as the scaffolding to write the game logic for any board or turn based game.

Status

This package is now stable with new features in the works.

Installation

go get github.com/quibbble/go-boardgame

Packages

Go-boardgame also contains a number of helpful packages that can be optionally used to speed up development:

  • bgerr adds helpful error statuses and messages to board games.
  • bgn adds a standardized machine-readable notation to board games for easy storage and game recreation.
  • collection adds logic that can be used for decks, hands, etc. This package makes use of Go generics so that it can be used with any of your custom types.

Examples

Tic-Tac-Toe provides a simple example implementation.

You can also view all currently implemented games here.

Future Plans

Additional resources common to many games will be added to the above packages list as time goes on to make writing the logic for these games a far faster process. Any ideas or PRs to improve or add additional features are always welcome.

Documentation

Index

Constants

View Source
const (
	ActionSetWinners = "SetWinners" // ActionSetWinners sets the winner(s) of a game
	ActionEndTurn    = "EndTurn"    // ActionEndTurn ends the turn for the given team
)

Common actions most game may utilize if desired

Variables

This section is empty.

Functions

This section is empty.

Types

type BoardGame

type BoardGame interface {
	// Do performs an action on the game
	Do(action *BoardGameAction) error

	// GetSnapshot retrieves the current game state from 'team' view
	// Entering nothing returns a complete snapshot with no data hidden i.e. all hands, resources, etc.
	// Entering more than one team will error
	GetSnapshot(team ...string) (*BoardGameSnapshot, error)
}

BoardGame is a representation of a board game allowing one to perform actions on the game as well as to retrieve game data

type BoardGameAction

type BoardGameAction struct {
	// Team is the team performing the action - required
	Team string

	// ActionType is the key that determines what action to perform - required
	ActionType string

	// MoreDetails allows for additional action details to be passed that are unique to the action type
	// Additional details are not required for every action so this field can be ignored if desired
	MoreDetails interface{} `json:",omitempty"`
}

BoardGameAction represents an action that is performed on the game state

type BoardGameBuilder

type BoardGameBuilder interface {
	// Create creates a game with desired options
	Create(options *BoardGameOptions) (BoardGame, error)

	// Info provides additional details about the game
	Info() *BoardGameInfo

	// Key gets the game's unique key, typically the name of the game
	Key() string
}

BoardGameBuilder builds a game given a set of options

type BoardGameInfo added in v1.1.3

type BoardGameInfo struct {
	// GameKey is the unique key that differentiates this game from others
	GameKey string

	// MinTeams and MaxTeams represents the min and max teams allowed when creating a new game
	MinTeams, MaxTeams int

	// MoreInfo allows for additional game specific info
	// Additional info is not required for every game so this field can be ignored if desired
	MoreInfo interface{} `json:",omitempty"`
}

BoardGameInfo provides additional details about the game

type BoardGameOptions

type BoardGameOptions struct {
	// Teams is the list of teams that will be playing the game - required
	Teams []string

	// MoreOptions allows for additional game options to be passed that are unique to each game
	// Additional options are not required for every game so this field can be ignored if desired
	MoreOptions interface{} `json:",omitempty"`
}

BoardGameOptions are the options used to create a new game

type BoardGameSnapshot

type BoardGameSnapshot struct {
	// Turn is the turn of the current team - required
	Turn string

	// Teams is a list of all teams playing the game - required
	Teams []string

	// Winners is a list of teams that have won the game - required
	Winners []string

	// MoreData allows for additional game data to be returned that is unique to each game
	// Typically more data such as boards, decks, etc. are needed for a game but this field can be ignored if desired
	MoreData interface{} `json:",omitempty"`

	// Targets are typically a list of BoardGameAction that can be performed on the game state
	// This can allow players to view all valid actions through a GUI
	// This field is left as an interface to allow for different targeting approaches as well
	// Optional feature not required to play a game and can be ignored if desired
	Targets interface{} `json:",omitempty"`

	// Actions is a list of past game actions that have lead to the current game state
	// This can allow players to view game logs of past actions
	// Optional feature not required to play a game and can be ignored if desired
	Actions []*BoardGameAction `json:",omitempty"`

	// Message provides players with information about what to do next
	// Optional feature not required to play a game and can be ignored if desired
	Message string `json:",omitempty"`
}

BoardGameSnapshot represents the current state of the game that will be viewed by a player

type BoardGameWithBGN

type BoardGameWithBGN interface {
	BoardGame

	// GetBGN returns the game in board game notation
	GetBGN() *bgn.Game
}

BoardGameWithBGN provides extra bgn functionality that does not necessarily need to be implemented to play a game

type BoardGameWithBGNBuilder

type BoardGameWithBGNBuilder interface {
	BoardGameBuilder

	// CreateWithBGN creates a game with desired options
	CreateWithBGN(options *BoardGameOptions) (BoardGameWithBGN, error)

	// Load loads a game in board game notation into a normal game
	Load(bgn *bgn.Game) (BoardGameWithBGN, error)
}

BoardGameWithBGNBuilder builds a game with additional bgn functionality

type EndTurnActionDetails added in v1.0.5

type EndTurnActionDetails struct {
}

EndTurnActionDetails are the action details for ending a team's turn

func DecodeEndTurnActionDetailsBGN added in v1.0.5

func DecodeEndTurnActionDetailsBGN(details, teams []string) (*EndTurnActionDetails, error)

func (*EndTurnActionDetails) EncodeBGN added in v1.0.5

func (o *EndTurnActionDetails) EncodeBGN(teams []string) ([]string, error)

type SetWinnersActionDetails

type SetWinnersActionDetails struct {
	Winners []string
}

SetWinnersActionDetails sets the current winner(s) of the game with Winners

func DecodeSetWinnersActionDetailsBGN

func DecodeSetWinnersActionDetailsBGN(details, teams []string) (*SetWinnersActionDetails, error)

DecodeSetWinnersActionDetailsBGN converts a bgn.Action Details object into the SetWinnersActionDetails object

func (*SetWinnersActionDetails) EncodeBGN

func (o *SetWinnersActionDetails) EncodeBGN(teams []string) ([]string, error)

EncodeBGN converts SetWinnersActionDetails into bgn.Action Details object given the list of teams in game

Directories

Path Synopsis
pkg
bgn

Jump to

Keyboard shortcuts

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