board

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package board stores the tiles for a game and handles queries to read and update tiles

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Board

type Board struct {
	UnusedTiles   map[tile.ID]tile.Tile
	UnusedTileIDs []tile.ID
	UsedTiles     map[tile.ID]tile.Position
	UsedTileLocs  map[tile.X]map[tile.Y]tile.Tile
	Config
}

Board represents the positioning of a player's tiles from a game. (Each player has their own board)

func New

func New(tiles []tile.Tile, tilePositions []tile.Position) *Board

New creates a new board with the tiles as unusedTiles and tilePositions as used tiles.

func (*Board) AddTile

func (b *Board) AddTile(t tile.Tile) error

AddTile adds a tile to the board's unused tiles. An error is returned and the tile is not added if the player already has it.

func (*Board) CanBeFinished

func (b *Board) CanBeFinished() bool

CanBeFinished determines if all the bord's tiles are used and form a single group.

func (Board) CanMoveTiles

func (b Board) CanMoveTiles(tilePositions map[tile.ID]tile.Position) bool

CanMoveTiles determines if the player's tiles can be moved to/in the used area without overlapping any other tiles

func (Board) MarshalJSON

func (b Board) MarshalJSON() ([]byte, error)

MarshalJSON implements the encoding/json.Marshaler interface. Returns an object containing the array of unused tiles, map of tile positions, and the board config.

func (*Board) MoveTiles

func (b *Board) MoveTiles(tilePositions map[tile.ID]tile.Position) error

MoveTiles moves the tiles to the specified positions. No action is taken and an error is returned if the tiles cannot be moved.

func (*Board) RemoveTile

func (b *Board) RemoveTile(t tile.Tile) error

RemoveTile removes a single tile from the board's tiles. An error is returned if the board does not have the tile.

func (*Board) Resize

func (b *Board) Resize(cfg Config) (*ResizeResult, error)

Resize resizes the board to use the new config. Any board size change information is returned in the message.

func (*Board) UnmarshalJSON

func (b *Board) UnmarshalJSON(d []byte) error

UnmarshalJSON implements the encoding/json.Unmarshaler interface. UsedTiles and UnusedTiles are read from arrays and converted into maps for quick lookup.

func (Board) UsedTileWords

func (b Board) UsedTileWords() []string

UsedTileWords computes all the horizontal and vertical words formed by used tiles.

type Config

type Config struct {
	NumRows int `json:"r"`
	NumCols int `json:"c"`
}

Config stores fields for creating a board.

func (Config) New

func (cfg Config) New(unusedTiles []tile.Tile) (*Board, error)

New creates a new board with the unused tiles.

func (Config) Validate

func (cfg Config) Validate() error

Validate returns an error if the number of rows or columns is invalid.

type ResizeResult

type ResizeResult struct {
	Info          string
	Tiles         []tile.Tile
	TilePositions []tile.Position
}

ResizeResult contains the result of a successful board resize operation.

Jump to

Keyboard shortcuts

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