banana

package
v0.0.0-...-f64dbd0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package banana contains the domain types for playing a game of Bananagrams.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartingTileCount

func StartingTileCount(pc, scale int) int

Types

type Board

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

Board represents an individual players game board.

func NewBoard

func NewBoard(c *Config) *Board

func NewBoardWithWords

func NewBoardWithWords(c *Config, words []Word) (*Board, error)

func (*Board) AddWord

func (b *Board) AddWord(w Word) error

AddWord attempts to add a word to the board, and calculates a few common data structures for later Board validation steps. If the word doesn't fit with existing words on the board, an error is returned.

func (*Board) AddWords

func (b *Board) AddWords(words []Word) error

func (*Board) AsTiles

func (b *Board) AsTiles() *Tiles

func (*Board) Clone

func (b *Board) Clone() *Board

func (*Board) Count

func (b *Board) Count() int

func (*Board) Diff

func (b *Board) Diff(tiles *Tiles) *Tiles

func (*Board) Validate

func (b *Board) Validate(tiles *Tiles, dict Dictionary) *BoardValidation

Validate returns the status of the board.

func (*Board) Words

func (b *Board) Words() []Word

type BoardStatus

type BoardStatus struct {
	Code   BoardStatusCode
	Errors []string
}

BoardStatus describes if a board is valid, or how it is invalid.

type BoardStatusCode

type BoardStatusCode int

BoardStatusCode describes the current validity of a board.

const (
	Success BoardStatusCode = iota
	InvalidWord
	DetachedBoard
	NotAllLetters
	ExtraLetters
	InvalidBoard
)

type BoardValidation

type BoardValidation struct {
	InvalidWords  []CharLocs
	ShortWords    []CharLocs
	InvalidBoard  bool
	DetachedBoard bool
	UnusedLetters []string
	ExtraLetters  []string
}

type Bunch

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

func NewBunch

func NewBunch(dist Distribution) *Bunch

func (*Bunch) Clone

func (b *Bunch) Clone() *Bunch

func (*Bunch) Count

func (b *Bunch) Count() int

func (*Bunch) Inc

func (b *Bunch) Inc(l Letter)

func (*Bunch) RemoveN

func (b *Bunch) RemoveN(n int, r *rand.Rand) (*Tiles, error)

RemoveN retrieves N tiles from the bunch.

func (*Bunch) Tile

func (b *Bunch) Tile(r *rand.Rand) (Letter, error)

type CharLoc

type CharLoc struct {
	Letter Letter
	Loc    Loc
}

type CharLocs

type CharLocs struct {
	// The word that is made of the charlocs.
	Word string
	Locs []CharLoc
}

CharLocs is a list of letters and the word they make up, this is currently only used for returning bad words.

type Config

type Config struct {
	// The minimum number of letters that a word needs to have to be considered
	// valid.
	MinLettersInWord int
}

func (*Config) Clone

func (c *Config) Clone() *Config

type DB

type DB interface {
	// Creates a new game with the given name and creator.
	NewGame(name string, creator PlayerID, config *Config) (GameID, error)
	// Get all of the games.
	Games() ([]*Game, error)
	// Loads a game with the given ID.
	Game(id GameID) (*Game, error)
	// Loads the bunch for the game with the given ID.
	Bunch(id GameID) (*Bunch, error)
	// Registers a player in our system.
	RegisterPlayer(name string) (PlayerID, error)
	// Adds a player to a not-yet-started game.
	AddPlayerToGame(gID GameID, pID PlayerID) error
	// Get all the players for a game.
	Players(id GameID) ([]*Player, error)
	// Loads a player with the given ID.
	Player(id PlayerID) (*Player, error)
	// Loads the board for the given game and player IDs.
	Board(gID GameID, pID PlayerID) (*Board, error)
	// Loads tiles for the given game and player IDs.
	Tiles(gID GameID, pID PlayerID) (*Tiles, error)
	// Updates a player's board.
	UpdateBoard(gID GameID, pID PlayerID, board *Board) error
	// Updates a player's tiles.
	UpdateTiles(gID GameID, pID PlayerID, tiles *Tiles) error
	// Updates the bunch for the game.
	UpdateBunch(id GameID, bunch *Bunch) error
	// Starts a game, and sets everyone's initial tile sets.
	StartGame(id GameID, players map[PlayerID]*Tiles, bunch *Bunch) error
	// Ends a given game, stops players from adding more to their boards.
	EndGame(id GameID) error
}

type Dictionary

type Dictionary interface {
	HasWord(word string) bool
}

func NewDictionary

func NewDictionary(r io.Reader) (Dictionary, error)

type Distribution

type Distribution map[int][]Letter

func Bananagrams

func Bananagrams() Distribution

Bananagrams returns the distribution of letters on a Bananagrams board.

func None

func None() Distribution

func Scale

func Scale(dist Distribution, scale int) (Distribution, error)

func TestDistribution

func TestDistribution() Distribution

TestDistribution returns a small distribution for more easily testing end-game scenarios.

type Game

type Game struct {
	ID        GameID
	Creator   PlayerID
	Name      string
	Status    GameStatus
	CreatedAt time.Time
	Config    *Config
}

func (*Game) Clone

func (g *Game) Clone() *Game

type GameID

type GameID string

type GameStatus

type GameStatus int
const (
	UnknownStatus GameStatus = iota
	WaitingForPlayers
	InProgress
	Finished
)

func (GameStatus) String

func (g GameStatus) String() string

type Letter

type Letter rune

Letter represents a tile character.

func (Letter) String

func (l Letter) String() string

String turns the letter into a string.

type Loc

type Loc struct {
	X, Y int
}

type Orientation

type Orientation int

Orientation describes how a board is placed on the board.

const (
	// NoOrientation is a catch-all for unknown orientations.
	NoOrientation Orientation = iota
	// Horizontal means the word is placed on the board from left to right.
	Horizontal
	// Vertical means the word is placed on the board from top to bottom.
	Vertical
)

type Player

type Player struct {
	ID      PlayerID
	Name    string
	AddedAt time.Time
}

func (*Player) Clone

func (p *Player) Clone() *Player

type PlayerID

type PlayerID string

type Tiles

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

Tiles represents a set of tiles.

func NewTiles

func NewTiles() *Tiles

NewTiles returns an initialized and empty tile set.

func (*Tiles) Add

func (t *Tiles) Add(o *Tiles)

Add adds tiles from one tile set to another, without modifying the tile set to add.

func (*Tiles) AsList

func (t *Tiles) AsList() []string

func (*Tiles) Clone

func (t *Tiles) Clone() *Tiles

Clone returns a deep copy of the set of tiles.

func (*Tiles) Count

func (t *Tiles) Count() int

func (*Tiles) Dec

func (t *Tiles) Dec(l Letter)

Dec removes one from the count of the given letter. This is allowed to be negative and is actually a key part of diffing tile sets.

func (*Tiles) Freq

func (t *Tiles) Freq(l Letter) int

Freq returns the frequency of a given letter.

func (*Tiles) Inc

func (t *Tiles) Inc(l Letter)

Inc adds one to the count of the given letter.

func (*Tiles) Update

func (t *Tiles) Update(l Letter, freq int)

type Word

type Word struct {
	Orientation Orientation
	Text        string
	Loc         Loc
}

Word represents the placement of a single word on a Bananagrams board.

func (Word) CharLocs

func (w Word) CharLocs() []CharLoc

Jump to

Keyboard shortcuts

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