game

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2022 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package game encapsulates the main mechanics for a Crossword Game. It interacts heavily with the protobuf data structures.

Index

Constants

View Source
const (
	//IdentificationAuthority is the authority that gives out game IDs
	IdentificationAuthority = "io.woogles"

	MacondoCreation = "Created with Macondo"

	ExchangeLimit = 8
	RackTileLimit = 8

	DefaultMaxScorelessTurns = 6
)
View Source
const (
	VarClassic      Variant = "classic"
	VarWordSmog             = "wordsmog"
	VarExtendedRack         = "extrack"
	// Redundant information, but we are deciding to treat different board
	// layouts as different variants.
	VarClassicSuper  = "classic_super"
	VarWordSmogSuper = "wordsmog_super"
)
View Source
const (
	CrossScoreOnly   = "cs"
	CrossScoreAndSet = "css"
)

Variables

This section is empty.

Functions

func CalculateCoordsFromStringPosition

func CalculateCoordsFromStringPosition(evt *pb.GameEvent)

CalculateCoordsFromStringPosition turns a "position" on the board such as H7 and turns it into a numeric row, col, and direction.

func Leave

Leave calculates the leave from the rack and the made play.

func MoveFromEvent

func MoveFromEvent(evt *pb.GameEvent, alph *alphabet.Alphabet, board *board.GameBoard) (*move.Move, error)

MoveFromEvent generates a move from an event

Types

type BackupMode

type BackupMode int
const (
	// NoBackup never performs game backups. It can be used for autoplay
	// that has absolutely no input.
	NoBackup BackupMode = iota
	// SimulationMode keeps a stack of game copies, using these for doing
	// endgame and other types of simulations.
	SimulationMode
	// InteractiveGameplayMode keeps just one backup after every turn. This is needed
	// in order to get challenges working, which would roll back the game to
	// an earlier state if a word is challenged off.
	InteractiveGameplayMode
)

type Game

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

Game is the actual internal game structure that controls the entire business logic of the game; drawing, making moves, etc. The two structures above are basically data entities. Note: a Game doesn't care how it is played. It is just rules for gameplay. AI players, human players, etc will play a game outside of the scope of this module.

func NewFromHistory

func NewFromHistory(history *pb.GameHistory, rules *GameRules, turnnum int) (*Game, error)

NewFromHistory instantiates a Game from a history, and sets the current turn to the passed in turnnum. It assumes the rules contains the current lexicon in history, if any!

func NewGame

func NewGame(rules *GameRules, playerinfo []*pb.PlayerInfo) (*Game, error)

NewGame is how one instantiates a brand new game.

func (*Game) AddFinalScoresToHistory

func (g *Game) AddFinalScoresToHistory()

AddFinalScoresToHistory adds the final scores and winner to the history.

func (*Game) Alphabet

func (g *Game) Alphabet() *alphabet.Alphabet

func (*Game) Bag

func (g *Game) Bag() *alphabet.Bag

Bag returns the current bag

func (*Game) BingosForNick

func (g *Game) BingosForNick(nick string) int

func (*Game) Board

func (g *Game) Board() *board.GameBoard

Board returns the current board state.

func (*Game) ChallengeEvent

func (g *Game) ChallengeEvent(addlBonus int, millis int) (bool, error)

ChallengeEvent should only be called if there is a history of events. It has the logic for appending challenge events and calculating scores properly. Note that this event can change the history of the game, including things like reset gameEnded back to false (for example if someone plays out with a phony). Return playLegal, error

func (*Game) Config

func (g *Game) Config() *config.Config

func (*Game) Copy

func (g *Game) Copy() *Game

Copy creates a deep copy of Game for the most part. The lexicon and alphabet are not deep-copied because these are not expected to change. The history is not copied because this only changes with the main Game, and not these copies.

func (*Game) CreateAndScorePlacementMove

func (g *Game) CreateAndScorePlacementMove(coords string, tiles string, rack string) (*move.Move, error)

CreateAndScorePlacementMove creates a *move.Move from the coords and given tiles. It scores the move, calculates the leave, etc. This should be used when a person is interacting with the interface.

func (*Game) CurrentSpread

func (g *Game) CurrentSpread() int

func (*Game) EventFromMove

func (g *Game) EventFromMove(m *move.Move) *pb.GameEvent

func (*Game) FirstPlayer

func (g *Game) FirstPlayer() *pb.PlayerInfo

func (*Game) History

func (g *Game) History() *pb.GameHistory

func (*Game) LastEvent

func (g *Game) LastEvent() *pb.GameEvent

func (*Game) LastWordsFormed

func (g *Game) LastWordsFormed() []alphabet.MachineWord

func (*Game) Lexicon

func (g *Game) Lexicon() lexicon.Lexicon

func (*Game) LexiconName

func (g *Game) LexiconName() string

func (*Game) NextPlayer

func (g *Game) NextPlayer() int

func (*Game) NickOnTurn

func (g *Game) NickOnTurn() string

func (*Game) NumPlayers

func (g *Game) NumPlayers() int

NumPlayers is always 2.

func (*Game) PlayLatestEvent

func (g *Game) PlayLatestEvent() error

PlayLatestEvent "plays" the latest event on the board. This is used for replaying a game from a GCG.

func (*Game) PlayMove

func (g *Game) PlayMove(m *move.Move, addToHistory bool, millis int) error

PlayMove plays a move on the board. This function is meant to be used by simulators as it implements a subset of possible moves, and by remote gameplay engines as much as possible. If the millis argument is passed in, it adds this value to the history as the time remaining for the user (when they played the move).

func (*Game) PlayScoringMove

func (g *Game) PlayScoringMove(coords, word string, addToHistory bool) (*move.Move, error)

PlayScoringMove plays a move on a board that is described by the coordinates and word only. It returns the move.

func (*Game) PlayToTurn

func (g *Game) PlayToTurn(turnnum int) error

func (*Game) PlayerIDOnTurn

func (g *Game) PlayerIDOnTurn() string

func (*Game) PlayerOnTurn

func (g *Game) PlayerOnTurn() int

func (*Game) Playing

func (g *Game) Playing() pb.PlayState

func (*Game) PointsFor

func (g *Game) PointsFor(playerIdx int) int

PointsFor returns the number of points for the given player

func (*Game) PointsForNick

func (g *Game) PointsForNick(nick string) int

func (*Game) RackFor

func (g *Game) RackFor(playerIdx int) *alphabet.Rack

RackFor returns the rack for the player with the passed-in index

func (*Game) RackLettersFor

func (g *Game) RackLettersFor(playerIdx int) string

RackLettersFor returns a user-visible representation of the player's rack letters

func (*Game) RecalculateBoard

func (g *Game) RecalculateBoard()

func (*Game) ResetToFirstState

func (g *Game) ResetToFirstState()

ResetToFirstState unplays all moves on the stack.

func (*Game) Rules

func (g *Game) Rules() *GameRules

func (*Game) ScorelessTurns

func (g *Game) ScorelessTurns() int

func (*Game) SetBackupMode

func (g *Game) SetBackupMode(m BackupMode)

func (*Game) SetChallengeRule

func (g *Game) SetChallengeRule(rule pb.ChallengeRule)

SetChallengeRule sets the challenge rule for a game. The game must already be started with StartGame above (call immediately afterwards). It would default to the 0 state (VOID) otherwise.

func (*Game) SetHistory

func (g *Game) SetHistory(h *pb.GameHistory)

func (*Game) SetMaxScorelessTurns

func (g *Game) SetMaxScorelessTurns(m int)

func (*Game) SetNextFirst

func (g *Game) SetNextFirst(first int)

SetNextFirst sets the player going first to the passed-in value. This will take effect the next time StartGame is called.

func (*Game) SetPlayerOnTurn

func (g *Game) SetPlayerOnTurn(onTurn int)

func (*Game) SetPlaying

func (g *Game) SetPlaying(s pb.PlayState)

func (*Game) SetPointsFor

func (g *Game) SetPointsFor(player, pts int)

func (*Game) SetRackFor

func (g *Game) SetRackFor(playerIdx int, rack *alphabet.Rack) error

SetRackFor sets the player's current rack. It throws an error if the rack is impossible to set from the current unseen tiles. It puts tiles back from opponent racks and our own racks, then sets the rack, and finally redraws for opponent.

func (*Game) SetRacksForBoth

func (g *Game) SetRacksForBoth(racks []*alphabet.Rack) error

SetRacksForBoth sets both racks at the same time.

func (*Game) SetRandomRack

func (g *Game) SetRandomRack(playerIdx int)

SetRandomRack sets the player's rack to a random rack drawn from the bag. It tosses the current rack back in first. This is used for simulations.

func (*Game) SetStateStackLength

func (g *Game) SetStateStackLength(length int)

func (*Game) SpreadFor

func (g *Game) SpreadFor(playerIdx int) int

func (*Game) StartGame

func (g *Game) StartGame()

StartGame starts a game anew, dealing out tiles to both players.

func (*Game) ThrowRacksIn

func (g *Game) ThrowRacksIn()

ThrowRacksIn throws both players' racks back in the bag.

func (*Game) ToDisplayText

func (g *Game) ToDisplayText() string

ToDisplayText turns the current state of the game into a displayable string.

func (*Game) Turn

func (g *Game) Turn() int

func (*Game) TurnsForNick

func (g *Game) TurnsForNick(nick string) int

func (*Game) Uid

func (g *Game) Uid() string

func (*Game) UnplayLastMove

func (g *Game) UnplayLastMove()

UnplayLastMove is a tricky but crucial function for any sort of simming / minimax search / etc. It restores the state after playing a move, without having to store a giant amount of data. The alternative is to store the entire game state with every node which quickly becomes unfeasible.

func (*Game) ValidateMove

func (g *Game) ValidateMove(m *move.Move) ([]alphabet.MachineWord, error)

ValidateMove validates the given move. It is meant to be used to validate user input games (perhaps from live play or GCGs). It does not check the validity of the words formed (unless the challenge rule is VOID), but it validates that the rules of the game are followed. It returns an array of `alphabet.MachineWord`s formed, or an error if the play is not game legal.

type GameRules

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

GameRules is a simple struct that encapsulates the instantiated objects needed to actually play a game.

func NewBasicGameRules

func NewBasicGameRules(cfg *config.Config,
	lexiconName, boardLayoutName, letterDistributionName, csetGenName string,
	variant Variant) (*GameRules, error)

func (GameRules) Board

func (g GameRules) Board() *board.GameBoard

func (GameRules) BoardName

func (g GameRules) BoardName() string

func (GameRules) Config

func (g GameRules) Config() *config.Config

func (GameRules) CrossSetGen

func (g GameRules) CrossSetGen() cross_set.Generator

func (GameRules) LetterDistribution

func (g GameRules) LetterDistribution() *alphabet.LetterDistribution

func (GameRules) LetterDistributionName

func (g GameRules) LetterDistributionName() string

func (GameRules) Lexicon

func (g GameRules) Lexicon() lexicon.Lexicon

func (GameRules) LexiconName

func (g GameRules) LexiconName() string

func (GameRules) Variant

func (g GameRules) Variant() Variant

type Variant

type Variant string

func HistoryToVariant

func HistoryToVariant(h *pb.GameHistory) (boardLayoutName, letterDistributionName string, variant Variant)

HistoryToVariant takes in a game history and returns the board configuration and letter distribution name.

Jump to

Keyboard shortcuts

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