model

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2023 License: MIT Imports: 12 Imported by: 0

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

View Source
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

func GridToSimpleMatrix(grid *Grid) [][]byte

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

func NewBlackCell(point Point) BlackCell

NewBlackCell creates a new BlackCell at the specified location.

func (BlackCell) GetPoint

func (bc BlackCell) GetPoint() Point

GetPoint returns the location of this cell (for the Cell interface).

func (BlackCell) String

func (bc BlackCell) String() string

String returns a string representation of this black cell.

type Cell

type Cell interface {
	GetPoint() Point
	String() string
}

Cell can be either a black cell or a letter cell.

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).

const (
	ACROSS Direction = "A"
	DOWN   Direction = "D"
)

func DirectionFromString added in v0.5.2

func DirectionFromString(s string) Direction

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.

func (Direction) Other added in v0.5.0

func (dir Direction) Other() Direction

Other returns the other direction.

func (Direction) String added in v0.4.0

func (dir Direction) String() string

String returns a string representation of this object

type Doable added in v0.5.0

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

Doable is an entry in the undoWord/redoWord stacks

func NewDoable added in v0.5.0

func NewDoable(grid *Grid, word *Word) Doable

NewDoable creates a Doable from the specified word in the grid

func (Doable) String added in v0.5.0

func (d Doable) String() string

String returns a string representation of the Doable

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

func LoadGrid(userid int, gridname string) (*Grid, error)

LoadGrid reads grid data from the database and creates a Grid object from it.

func NewGrid

func NewGrid(n int) *Grid

NewGrid creates a grid of the specified size.

func (*Grid) BlackCellIterator

func (grid *Grid) BlackCellIterator() <-chan BlackCell

BlackCellIterator is a generator for all the black cells in the grid.

func (*Grid) CellIterator

func (grid *Grid) CellIterator() <-chan Cell

CellIterator is a generator for all the cells in the grid, from top to bottom, left to right (same as PointIterator).

func (*Grid) CountBlackCells

func (grid *Grid) CountBlackCells() int

CountBlackCells returns the number of black cells in the grid

func (*Grid) DeleteGrid added in v0.5.0

func (grid *Grid) DeleteGrid(userid int, gridname string) error

DeleteGrid deletes the specified grid

func (*Grid) Equal added in v0.5.2

func (grid *Grid) Equal(other *Grid) bool

Equal returns true if this grid is essentially equal to the other.

func (*Grid) GetCell

func (grid *Grid) GetCell(point Point) Cell

GetCell returns the cell at the specified point, which may be a black cell or a letter cell.

func (*Grid) GetClue added in v0.3.0

func (grid *Grid) GetClue(word *Word) (string, error)

GetClue returns the clue for the word.

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

func (grid *Grid) GetGridList(userid int) []string

GetGridList returns a list of grids for the specified user.

func (*Grid) GetGridName added in v0.5.0

func (grid *Grid) GetGridName() string

GetGridName returns the grid name

func (*Grid) GetLength added in v0.3.0

func (grid *Grid) GetLength(word *Word) (int, error)

GetLength returns the length of the word.

func (*Grid) GetLetter added in v0.3.0

func (grid *Grid) GetLetter(point Point) string

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) GetText added in v0.3.0

func (grid *Grid) GetText(word *Word) string

GetText returns the text of the word.

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

func (grid *Grid) GridNameUsed(userid int, gridname string) bool

GridNameUsed returns true if the specified grid name for this user is already saved in the database

func (*Grid) IsBlackCell

func (grid *Grid) IsBlackCell(point Point) bool

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

func (grid *Grid) LookupWord(point Point, dir Direction) *Word

LookupWord returns the word containing this point and direction

func (*Grid) LookupWordByNumber added in v0.4.0

func (grid *Grid) LookupWordByNumber(seq int, dir Direction) *Word

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

func (grid *Grid) PointIterator() <-chan Point

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

func (grid *Grid) RenameGrid(userid int, oldGridName, newGridName string) error

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

func (grid *Grid) SaveGrid(userid int) error

SaveGrid adds or updates a record for this grid in the database

func (*Grid) SetCell

func (grid *Grid) SetCell(point Point, cell Cell)

SetCell sets the cell at the specified point

func (*Grid) SetClue added in v0.5.2

func (grid *Grid) SetClue(word *Word, clue string) error

SetClue sets the specified clue in the specified word.

func (*Grid) SetGridName added in v0.5.0

func (grid *Grid) SetGridName(name string)

SetGridName sets the grid title

func (*Grid) SetLetter added in v0.3.0

func (grid *Grid) SetLetter(point Point, letter string)

SetLetter sets the letter value of the cell at the specified point

func (*Grid) SetText added in v0.4.0

func (grid *Grid) SetText(word *Word, text string) error

SetText sets the text in the grid for a specified word.

func (*Grid) SetTextWithoutPush added in v0.5.0

func (grid *Grid) SetTextWithoutPush(word *Word, text string)

func (*Grid) String

func (grid *Grid) String() string

String returns a string representation of the grid

func (*Grid) SymmetricPoint

func (grid *Grid) SymmetricPoint(point Point) Point

SymmetricPoint returns the point of the cell at 180 degrees rotation.

func (*Grid) Toggle

func (grid *Grid) Toggle(point Point)

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

func (grid *Grid) ValidIndex(point Point) error

ValidIndex whether a point is a valid index in this grid.

func (*Grid) WordIterator added in v0.4.0

func (grid *Grid) WordIterator(point Point, dir Direction) <-chan Point

WordIterator iterates through the points in a word, stopping when it encounters a black cell or the edge of the 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 NewPoint added in v0.3.0

func NewPoint(r, c int) Point

Creates a new point with the supplied row and column.

func (*Point) Compare

func (p *Point) Compare(other Point) int

Compare returns -1, 0, or 1 depending on whether this point is less than, equal to, or greater than another.

func (*Point) Equal

func (p *Point) Equal(other Point) bool

Equal is true if this point has the same row and column of another point.

func (*Point) String

func (p *Point) String() string

String returns a string representation of this type

func (*Point) ToXY

func (p *Point) ToXY() (int, int)

ToXY converts a point (that uses 1-based coordinates) to a pair (x, y) that uses zero-based ones.

type Word added in v0.4.0

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

Word consists of a point and a direction

func NewWord added in v0.4.0

func NewWord(point Point, dir Direction, length int, clue string) *Word

NewWord is the constructor for Word

func (*Word) GetCrossingWords added in v0.5.0

func (word *Word) GetCrossingWords(grid *Grid) []*Word

GetCrossingWords returns the words that intersect the specified word.

func (*Word) String added in v0.4.0

func (word *Word) String() string

String returns a string representation of this object.

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

Jump to

Keyboard shortcuts

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