Documentation ¶
Overview ¶
Package model contains types and functions that support the internal workings of the application.
A grid consists of an nxn matrix of cells, which are of two types:
Black cells: Blocks in the grid
Letter cells: Ordinary cells where letters of words can be placed.
The grid also supports undo/redo for black cells in this grid.
Index ¶
- Constants
- func GridToSimpleMatrix(grid *Grid) [][]byte
- type BlackCell
- type Cell
- type Constraint
- type Direction
- type Doable
- type Grid
- func (grid *Grid) BlackCellIterator() <-chan BlackCell
- func (grid *Grid) CellIterator() <-chan Cell
- func (grid *Grid) CountBlackCells() int
- func (grid *Grid) DeleteGrid(userid int, gridname string) error
- func (grid *Grid) Equal(other *Grid) bool
- func (grid *Grid) GetCell(point Point) Cell
- func (grid *Grid) GetClue(word *Word) (string, error)
- func (grid *Grid) GetConstraints(word *Word) []*Constraint
- func (grid *Grid) GetGridList(userid int) []string
- func (grid *Grid) GetGridName() string
- func (grid *Grid) GetLength(word *Word) (int, error)
- func (grid *Grid) GetLetter(point Point) string
- func (grid *Grid) GetText(word *Word) string
- func (grid *Grid) GetWordNumber(word *Word) *WordNumber
- func (grid *Grid) GridNameUsed(userid int, gridname string) bool
- func (grid *Grid) IsBlackCell(point Point) bool
- func (grid *Grid) LetterCellIterator() <-chan LetterCell
- func (grid *Grid) LookupWord(point Point, dir Direction) *Word
- func (grid *Grid) LookupWordByNumber(seq int, dir Direction) *Word
- func (grid *Grid) LookupWordNumber(seq int) *WordNumber
- func (grid *Grid) LookupWordNumberForStartingPoint(point Point) *WordNumber
- func (grid *Grid) PointIterator() <-chan Point
- func (grid *Grid) RedoBlackCell()
- func (grid *Grid) RedoWord()
- func (grid *Grid) RenameGrid(userid int, oldGridName, newGridName string) error
- func (grid *Grid) RenumberCells()
- func (grid *Grid) SaveGrid(userid int) error
- func (grid *Grid) SetCell(point Point, cell Cell)
- func (grid *Grid) SetClue(word *Word, clue string) error
- func (grid *Grid) SetGridName(name string)
- func (grid *Grid) SetLetter(point Point, letter string)
- func (grid *Grid) SetText(word *Word, text string) error
- func (grid *Grid) SetTextWithoutPush(word *Word, text string)
- func (grid *Grid) String() string
- func (grid *Grid) SymmetricPoint(point Point) Point
- func (grid *Grid) Toggle(point Point)
- func (grid *Grid) UndoBlackCell()
- func (grid *Grid) UndoWord()
- func (grid *Grid) ValidIndex(point Point) error
- func (grid *Grid) WordIterator(point Point, dir Direction) <-chan Point
- type LetterCell
- type NumberedCell
- type Point
- type Word
- type WordNumber
Constants ¶
const BLACK_CELL = "\x00"
Letter value of a black cell in the cells table
Variables ¶
This section is empty.
Functions ¶
func GridToSimpleMatrix ¶ added in v0.6.0
GridToSimpleMatrix builds a simple representation of a grid as an n x n matrix of bytes, where 0 represents a black cell, and the rest are the letters in that cell.
Types ¶
type BlackCell ¶
type BlackCell struct {
// contains filtered or unexported fields
}
BlackCell is a point in the grid that can have no letters. It marks the boundaries for the starting and stopping point of words.
func NewBlackCell ¶
NewBlackCell creates a new BlackCell at the specified location.
type Constraint ¶ added in v0.5.0
type Constraint struct { // Index within the main word (1, 2, ..., length) Pos int `json:"pos"` // Index within the crossing word (1, 2, ..., length) Index int `json:"index"` // Letter at index Letter string `json:"letter"` // Text of crossing word Text string `json:"text"` // Word number of crossing word Seq int `json:"seq"` // Direction of crossing word Dir Direction `json:"dir"` // Regular expression for possibilities Pattern string `json:"pattern"` // Number of words that match that pattern NChoices int `json:"nChoices"` }
Constraint is a structure that describes constraints imposed on this word by its crossing words.
func (*Constraint) ToJSON ¶ added in v0.5.0
func (cst *Constraint) ToJSON() string
ToJSON returns the JSON representation of a constraints object
type Direction ¶
type Direction string
Direction is either Across or Down (according to the enumerated constants of those names).
func DirectionFromString ¶ added in v0.5.2
DirectionFromString parses a string for a direction. It will accept anything that starts with the direction letter value. Panics if the parameter is not a valid direction.
type Doable ¶ added in v0.5.0
type Doable struct {
// contains filtered or unexported fields
}
Doable is an entry in the undoWord/redoWord stacks
type Grid ¶
type Grid struct {
// contains filtered or unexported fields
}
Grid contains the cells of a puzzle.
A grid is constructed with the single parameter n, which is the size (n x n) of the grid.
Any of the cells in the grid can be "black cells", which act as the boundaries of where the words can go. The model automatically takes care of matching a black cell with its symmetric twin 180 degrees from it.
Wherever an across or down word starts, the grid assigns the next available word number to the cell and keeps track of the lengths of the across and down words.
Grid supports a full "undo/redo" capability for the current session (from load to save). Any black cell additions or deletions are pushed on an undo stack.
func LoadGrid ¶ added in v0.5.2
LoadGrid reads grid data from the database and creates a Grid object from it.
func (*Grid) BlackCellIterator ¶
BlackCellIterator is a generator for all the black cells in the grid.
func (*Grid) CellIterator ¶
CellIterator is a generator for all the cells in the grid, from top to bottom, left to right (same as PointIterator).
func (*Grid) CountBlackCells ¶
CountBlackCells returns the number of black cells in the grid
func (*Grid) DeleteGrid ¶ added in v0.5.0
DeleteGrid deletes the specified grid
func (*Grid) Equal ¶ added in v0.5.2
Equal returns true if this grid is essentially equal to the other.
func (*Grid) GetCell ¶
GetCell returns the cell at the specified point, which may be a black cell or a letter cell.
func (*Grid) GetConstraints ¶ added in v0.5.0
func (grid *Grid) GetConstraints(word *Word) []*Constraint
GetConstraints finds the constraints imposed on this word by its crossing words.
func (*Grid) GetGridList ¶ added in v0.5.0
GetGridList returns a list of grids for the specified user.
func (*Grid) GetGridName ¶ added in v0.5.0
GetGridName returns the grid name
func (*Grid) GetLetter ¶ added in v0.3.0
GetLetter returns the value of the cell at this point. The length of the returned value is always 1, unless the point refers to a black cell, in which case the length is zero.
func (*Grid) GetWordNumber ¶ added in v0.5.0
func (grid *Grid) GetWordNumber(word *Word) *WordNumber
Given a word, returns the word number for it
func (*Grid) GridNameUsed ¶ added in v0.5.0
GridNameUsed returns true if the specified grid name for this user is already saved in the database
func (*Grid) IsBlackCell ¶
IsBlackCell returns true if the specified point is a black cell.
func (*Grid) LetterCellIterator ¶
func (grid *Grid) LetterCellIterator() <-chan LetterCell
LetterCellIterator is a generator for all the LetterCells in the grid.
func (*Grid) LookupWord ¶ added in v0.4.0
LookupWord returns the word containing this point and direction
func (*Grid) LookupWordByNumber ¶ added in v0.4.0
LookupWordByNumber returns the word at this point and direction
func (*Grid) LookupWordNumber ¶ added in v0.4.0
func (grid *Grid) LookupWordNumber(seq int) *WordNumber
LookupWordNumber returns the WordNumber for this number
func (*Grid) LookupWordNumberForStartingPoint ¶ added in v0.5.0
func (grid *Grid) LookupWordNumberForStartingPoint(point Point) *WordNumber
LookupWordNumberForStartingPoint returns the WordNumber starting at this point.
func (*Grid) PointIterator ¶
PointIterator is a generator for all the points in the grid, from top bottom and left to right (i.e, (1, 1), (1, 2), ..., (1, n), (2, 1), (2, 2), ..., (2, n), ..., (n, 1) (n, 2), ..., (n, n)).
func (*Grid) RedoBlackCell ¶
func (grid *Grid) RedoBlackCell()
RedoBlackCell pops a point from the redo stack and toggles the black cell at that point.
func (*Grid) RedoWord ¶ added in v0.5.0
func (grid *Grid) RedoWord()
RedoWord gets the last word change to the grid and re-applies it
func (*Grid) RenameGrid ¶ added in v0.5.2
RenameGrid renames a grid in the database
func (*Grid) RenumberCells ¶
func (grid *Grid) RenumberCells()
RenumberCells assigns the word numbers based on the locations of the black cells.
func (*Grid) SaveGrid ¶ added in v0.5.0
SaveGrid adds or updates a record for this grid in the database
func (*Grid) SetGridName ¶ added in v0.5.0
SetGridName sets the grid title
func (*Grid) SetLetter ¶ added in v0.3.0
SetLetter sets the letter value of the cell at the specified point
func (*Grid) SetTextWithoutPush ¶ added in v0.5.0
func (*Grid) SymmetricPoint ¶
SymmetricPoint returns the point of the cell at 180 degrees rotation.
func (*Grid) Toggle ¶
Toggle switches a point between black cell and letter cell. Does so also to the symmetric point.
func (*Grid) UndoBlackCell ¶
func (grid *Grid) UndoBlackCell()
UndoBlackCell pops a point from the undo stack and toggles the black cell at that point.
func (*Grid) UndoWord ¶ added in v0.5.0
func (grid *Grid) UndoWord()
UndoWord undoes the last push to the undoWordStack
func (*Grid) ValidIndex ¶
ValidIndex whether a point is a valid index in this grid.
type LetterCell ¶
type LetterCell struct {
// contains filtered or unexported fields
}
Letter cell is an ordinary point in the grid. It contains:
- A pointer to the numbered cell for the across word (if any)
- A pointer to the numbered cell for the down word (if any)
- The character in the cell
func NewLetterCell ¶
func NewLetterCell(point Point) LetterCell
NewLetterCell creates a new LetterCell at the specified location.
func (LetterCell) GetPoint ¶
func (lc LetterCell) GetPoint() Point
GetPoint returns the location of this cell (for the Cell interface)
func (LetterCell) String ¶
func (lc LetterCell) String() string
String returns a string representation of this letter cell.
type NumberedCell ¶ added in v0.6.0
type NumberedCell struct { Seq int // The word number (1, 2, ...) Row int // The row number (1, 2, ..., n) Col int // The column number (1, 2, ..., n) StartA bool // This is the start of an across word StartD bool // This is the start of a down word }
func GetNumberedCells ¶ added in v0.6.0
func GetNumberedCells(cells [][]byte) []NumberedCell
GetNumberedCells determines the points in the grid that are the start of an across word and/or a down word.
func (NumberedCell) String ¶ added in v0.6.0
func (nc NumberedCell) String() string
String returns a string representation of a numbered cell.
type Point ¶
type Point struct {
// contains filtered or unexported fields
}
Point is a row and column pair
func (*Point) Compare ¶
Compare returns -1, 0, or 1 depending on whether this point is less than, equal to, or greater than another.
type Word ¶ added in v0.4.0
type Word struct {
// contains filtered or unexported fields
}
Word consists of a point and a direction
func (*Word) GetCrossingWords ¶ added in v0.5.0
GetCrossingWords returns the words that intersect the specified word.
type WordNumber ¶
type WordNumber struct {
// contains filtered or unexported fields
}
WordNumber is a type that exists for each numbered cell in the grid. It contains the word number and the location at which it exists in the grid
func NewWordNumber ¶
func NewWordNumber(seq int, point Point) *WordNumber
NewWordNumber creates a new WordNumber structure and returns a pointer to it.
func (*WordNumber) String ¶ added in v0.3.0
func (wn *WordNumber) String() string
String returns a string representation of this word number