Documentation ¶
Overview ¶
Package game encapsulates the main mechanics for a Crossword Game. It interacts heavily with the protobuf data structures.
a fast unique time-based ID algorithm from the mongo mgo driver
Index ¶
- Constants
- func CalculateCoordsFromStringPosition(evt *pb.GameEvent)
- func MaxCanExchange(inbag, exchLimit int) int
- func MoveFromEvent(evt *pb.GameEvent, alph *tilemapping.TileMapping, board *board.GameBoard) (*move.Move, error)
- type BackupMode
- type Game
- func (g *Game) AddFinalScoresToHistory()
- func (g *Game) AddNote(note string) error
- func (g *Game) Alphabet() *tilemapping.TileMapping
- func (g *Game) Bag() *tilemapping.Bag
- func (g *Game) BingosForNick(nick string) int
- func (g *Game) Board() *board.GameBoard
- func (g *Game) ChallengeEvent(addlBonus int, millis int) (bool, error)
- func (g *Game) Config() *config.Config
- func (g *Game) Copy() *Game
- func (g *Game) CreateAndScorePlacementMove(coords string, tiles string, rack string) (*move.Move, error)
- func (g *Game) CurrentSpread() int
- func (g *Game) EventFromMove(m *move.Move) *pb.GameEvent
- func (g *Game) ExchangeLimit() int
- func (g *Game) FirstPlayer() *pb.PlayerInfo
- func (g *Game) FlipPlayers()
- func (g *Game) History() *pb.GameHistory
- func (g *Game) LastEvent() *pb.GameEvent
- func (g *Game) LastScorelessTurns() int
- func (g *Game) LastWordsFormed() []tilemapping.MachineWord
- func (g *Game) Lexicon() lexicon.Lexicon
- func (g *Game) LexiconName() string
- func (g *Game) NextPlayer() int
- func (g *Game) NickOnTurn() string
- func (g *Game) NumPlayers() int
- func (g *Game) PlayLatestEvent() error
- func (g *Game) PlayMove(m *move.Move, addToHistory bool, millis int) error
- func (g *Game) PlayScoringMove(coords, word string, addToHistory bool) (*move.Move, error)
- func (g *Game) PlaySmallMove(m *tinymove.SmallMove) (*[board.MaxBoardDim]tilemapping.MachineLetter, error)
- func (g *Game) PlayToTurn(turnnum int) error
- func (g *Game) PlayTurn(t int) error
- func (g *Game) PlayerIDOnTurn() string
- func (g *Game) PlayerOnTurn() int
- func (g *Game) Playing() pb.PlayState
- func (g *Game) PointsFor(playerIdx int) int
- func (g *Game) PointsForNick(nick string) int
- func (g *Game) RackFor(playerIdx int) *tilemapping.Rack
- func (g *Game) RackLettersFor(playerIdx int) string
- func (g *Game) RecalculateBoard()
- func (g *Game) RenamePlayer(idx int, playerinfo *pb.PlayerInfo) error
- func (g *Game) ResetToFirstState()
- func (g *Game) Rules() *GameRules
- func (g *Game) ScorelessTurns() int
- func (g *Game) SetBackupMode(m BackupMode)
- func (g *Game) SetBoard(b *board.GameBoard)
- func (g *Game) SetChallengeRule(rule pb.ChallengeRule)
- func (g *Game) SetCrossSetGen(gen cross_set.Generator)
- func (g *Game) SetEndgameMode(m bool)
- func (g *Game) SetHistory(h *pb.GameHistory)
- func (g *Game) SetMaxScorelessTurns(m int)
- func (g *Game) SetPlayerOnTurn(onTurn int)
- func (g *Game) SetPlaying(s pb.PlayState)
- func (g *Game) SetPointsFor(player, pts int)
- func (g *Game) SetRackFor(playerIdx int, rack *tilemapping.Rack) error
- func (g *Game) SetRackForOnly(playerIdx int, rack *tilemapping.Rack) error
- func (g *Game) SetRacksForBoth(racks []*tilemapping.Rack) error
- func (g *Game) SetRandomRack(playerIdx int, knownRack []tilemapping.MachineLetter) ([]tilemapping.MachineLetter, error)
- func (g *Game) SetRules(r *GameRules)
- func (g *Game) SetScorelessTurns(n int)
- func (g *Game) SetStateStackLength(length int)
- func (g *Game) SpreadFor(playerIdx int) int
- func (g *Game) StartGame()
- func (g *Game) ThrowRacksIn()
- func (g *Game) ThrowRacksInFor(pidx int)
- func (g *Game) ToCGP(formatForBot bool) string
- func (g *Game) ToDisplayText() string
- func (g *Game) Turn() int
- func (g *Game) TurnsForNick(nick string) int
- func (g *Game) Uid() string
- func (g *Game) UnplayLastMove()
- func (g *Game) ValidateMove(m *move.Move) ([]tilemapping.MachineWord, error)
- func (g *Game) ValidateWords(lex lexicon.Lexicon, words []tilemapping.MachineWord) error
- type GameRules
- func (g GameRules) Board() *board.GameBoard
- func (g GameRules) BoardName() string
- func (g GameRules) Config() *config.Config
- func (g GameRules) CrossSetGen() cross_set.Generator
- func (g GameRules) ExchangeLimit() int
- func (g GameRules) LetterDistribution() *tilemapping.LetterDistribution
- func (g GameRules) LetterDistributionName() string
- func (g GameRules) Lexicon() lexicon.Lexicon
- func (g GameRules) LexiconName() string
- func (g *GameRules) SetExchangeLimit(l int)
- func (g GameRules) Variant() Variant
- type RequestId
- type Variant
Constants ¶
const ( //IdentificationAuthority is the authority that gives out game IDs IdentificationAuthority = "org.macondo" MacondoCreation = "Created with Macondo" RackTileLimit = 7 DefaultMaxScorelessTurns = 6 CurrentGameHistoryVersion = 2 )
const ( VarClassic Variant = "classic" VarWordSmog = "wordsmog" // Redundant information, but we are deciding to treat different board // layouts as different variants. VarClassicSuper = "classic_super" VarWordSmogSuper = "wordsmog_super" )
const ( CrossScoreOnly = "cs" CrossScoreAndSet = "css" )
const DefaultExchangeLimit = 7
Variables ¶
This section is empty.
Functions ¶
func CalculateCoordsFromStringPosition ¶
CalculateCoordsFromStringPosition turns a "position" on the board such as H7 and turns it into a numeric row, col, and direction.
func MaxCanExchange ¶ added in v0.9.9
func MoveFromEvent ¶ added in v0.4.4
func MoveFromEvent(evt *pb.GameEvent, alph *tilemapping.TileMapping, board *board.GameBoard) (*move.Move, error)
MoveFromEvent generates a move from an event
Types ¶
type BackupMode ¶ added in v0.4.5
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 ¶
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! Note: NewFromHistory does not currently parse any CGP included in the starting_cgp. We should fix this.
func NewFromSnapshot ¶ added in v0.4.5
func NewFromSnapshot(rules *GameRules, players []*pb.PlayerInfo, lastKnownRacks []string, scores []int, boardRows [][]tilemapping.MachineLetter) (*Game, error)
func NewGame ¶
func NewGame(rules *GameRules, playerinfo []*pb.PlayerInfo) (*Game, error)
NewGame is how one instantiates a brand new game. playerinfo must be in the order of who goes first. It is the caller's responsibility to alternate firsts.
func (*Game) AddFinalScoresToHistory ¶ added in v0.4.5
func (g *Game) AddFinalScoresToHistory()
AddFinalScoresToHistory adds the final scores and winner to the history.
func (*Game) Alphabet ¶
func (g *Game) Alphabet() *tilemapping.TileMapping
func (*Game) BingosForNick ¶ added in v0.4.4
func (*Game) ChallengeEvent ¶ added in v0.4.5
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) Copy ¶
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 (*Game) EventFromMove ¶ added in v0.4.5
func (*Game) ExchangeLimit ¶ added in v0.9.9
func (*Game) FirstPlayer ¶
func (g *Game) FirstPlayer() *pb.PlayerInfo
func (*Game) FlipPlayers ¶ added in v0.4.9
func (g *Game) FlipPlayers()
func (*Game) History ¶
func (g *Game) History() *pb.GameHistory
func (*Game) LastScorelessTurns ¶ added in v0.7.0
func (*Game) LastWordsFormed ¶ added in v0.4.5
func (g *Game) LastWordsFormed() []tilemapping.MachineWord
func (*Game) LexiconName ¶ added in v0.4.5
func (*Game) NextPlayer ¶ added in v0.4.5
func (*Game) NickOnTurn ¶
func (*Game) PlayLatestEvent ¶ added in v0.4.5
PlayLatestEvent "plays" the latest event on the board. This is used for replaying a game from a GCG.
func (*Game) PlayMove ¶
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 ¶
PlayScoringMove plays a move on a board that is described by the coordinates and word only. It returns the move.
func (*Game) PlaySmallMove ¶ added in v0.8.7
func (g *Game) PlaySmallMove(m *tinymove.SmallMove) ( *[board.MaxBoardDim]tilemapping.MachineLetter, error)
PlaySmallMove plays a SmallMove in the game. SmallMove can only be a pass or a tile move play right now. It is meant to be used for pre-endgames and endgames. It is meant for "simulation" mode as opposed to real play mode. A lot of this function is copied from PlayMove. We should try to unite those somehow.
func (*Game) PlayToTurn ¶
func (*Game) PlayerIDOnTurn ¶ added in v0.4.5
func (*Game) PlayerOnTurn ¶
func (*Game) PointsForNick ¶
func (*Game) RackFor ¶
func (g *Game) RackFor(playerIdx int) *tilemapping.Rack
RackFor returns the rack for the player with the passed-in index
func (*Game) RackLettersFor ¶
RackLettersFor returns a user-visible representation of the player's rack letters
func (*Game) RecalculateBoard ¶ added in v0.4.5
func (g *Game) RecalculateBoard()
func (*Game) RenamePlayer ¶ added in v0.4.10
func (g *Game) RenamePlayer(idx int, playerinfo *pb.PlayerInfo) error
func (*Game) ResetToFirstState ¶
func (g *Game) ResetToFirstState()
ResetToFirstState unplays all moves on the stack.
func (*Game) ScorelessTurns ¶ added in v0.4.5
func (*Game) SetBackupMode ¶ added in v0.4.5
func (g *Game) SetBackupMode(m BackupMode)
func (*Game) SetChallengeRule ¶ added in v0.4.5
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) SetCrossSetGen ¶ added in v0.4.5
func (*Game) SetEndgameMode ¶ added in v0.8.7
func (*Game) SetHistory ¶ added in v0.4.5
func (g *Game) SetHistory(h *pb.GameHistory)
func (*Game) SetMaxScorelessTurns ¶ added in v0.4.5
func (*Game) SetPlayerOnTurn ¶
func (*Game) SetPlaying ¶ added in v0.4.5
func (*Game) SetPointsFor ¶
func (*Game) SetRackFor ¶
func (g *Game) SetRackFor(playerIdx int, rack *tilemapping.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) SetRackForOnly ¶ added in v0.5.0
func (g *Game) SetRackForOnly(playerIdx int, rack *tilemapping.Rack) error
SetRackForOnly is like SetRackFor, but it doesn't redraw random racks for opponent, or throw racks in. It assumes these tasks have already been done, or will be done properly.
func (*Game) SetRacksForBoth ¶
func (g *Game) SetRacksForBoth(racks []*tilemapping.Rack) error
SetRacksForBoth sets both racks at the same time.
func (*Game) SetRandomRack ¶
func (g *Game) SetRandomRack(playerIdx int, knownRack []tilemapping.MachineLetter) ([]tilemapping.MachineLetter, error)
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. If a second argument (knownRack) is provided, the randomRack will contain the known rack. Any extra drawn tiles are returned as well, in this case.
func (*Game) SetScorelessTurns ¶ added in v0.4.5
func (*Game) SetStateStackLength ¶
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) ThrowRacksInFor ¶ added in v0.8.0
func (*Game) ToDisplayText ¶
ToDisplayText turns the current state of the game into a displayable string.
func (*Game) TurnsForNick ¶ added in v0.4.5
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 ¶ added in v0.4.4
func (g *Game) ValidateMove(m *move.Move) ([]tilemapping.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 `tilemapping.MachineWord`s formed, or an error if the play is not game legal.
func (*Game) ValidateWords ¶ added in v0.4.9
func (g *Game) ValidateWords(lex lexicon.Lexicon, words []tilemapping.MachineWord) error
ValidateWords validates all `words` with the passed-in lexicon, and the game's variant. We don't use the game's lexicon because of Reasons.
type GameRules ¶ added in v0.4.5
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 ¶ added in v0.4.5
func (GameRules) CrossSetGen ¶ added in v0.4.5
func (GameRules) ExchangeLimit ¶ added in v0.9.9
func (GameRules) LetterDistribution ¶ added in v0.4.5
func (g GameRules) LetterDistribution() *tilemapping.LetterDistribution
func (GameRules) LetterDistributionName ¶ added in v0.4.5
func (GameRules) LexiconName ¶ added in v0.4.5
func (*GameRules) SetExchangeLimit ¶ added in v0.9.9
type RequestId ¶ added in v0.4.10
type RequestId string
RequestId is used for tagging each incoming http request for logging purposes. The actual implementation is just the ObjectId implementation found in launchpad.net/mgo/bson. This will most likely change and evolve into its own format.
type Variant ¶ added in v0.4.5
type Variant string
func HistoryToVariant ¶ added in v0.4.5
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.