Documentation ¶
Overview ¶
Model is the internal component of the application, used by the controller and the view.
Index ¶
- Constants
- func Blocks(ilist []int) <-chan Block
- func Complement(ilist []int) []int
- func Connect() (*sql.DB, error)
- func CreateDatabase()
- func DumpPuzzle(puzzle *Puzzle)
- func GetDDL() string
- func GetMatchingWords(pattern string, stop <-chan struct{}) <-chan string
- func Hash256(s string) []byte
- func LoadDictionary()
- func Pattern(ilist []int) string
- func PuzzleToSimpleMatrix(puzzle *Puzzle) [][]byte
- func Regexp(letters string) string
- func TracePuzzle(puzzle *Puzzle)
- type BlackCell
- type Block
- type Cell
- type Constraint
- type Dictionary
- type Direction
- type Doable
- type Importer
- type LetterCell
- type NumberedCell
- type Point
- type Puzzle
- func (puzzle *Puzzle) BlackCellIterator() <-chan BlackCell
- func (puzzle *Puzzle) CellIterator() <-chan Cell
- func (puzzle *Puzzle) CountBlackCells() int
- func (puzzle *Puzzle) DeletePuzzle(userid int, puzzlename string) error
- func (puzzle *Puzzle) Equal(other *Puzzle) bool
- func (puzzle *Puzzle) GetCell(point Point) Cell
- func (puzzle *Puzzle) GetClue(word *Word) (string, error)
- func (puzzle *Puzzle) GetConstraints(word *Word) []*Constraint
- func (puzzle *Puzzle) GetLength(word *Word) (int, error)
- func (puzzle *Puzzle) GetLetter(point Point) string
- func (puzzle *Puzzle) GetPuzzleList(userid int) []string
- func (puzzle *Puzzle) GetPuzzleName() string
- func (puzzle *Puzzle) GetText(word *Word) string
- func (puzzle *Puzzle) GetWordNumber(word *Word) *WordNumber
- func (puzzle *Puzzle) IsBlackCell(point Point) bool
- func (puzzle *Puzzle) LetterCellIterator() <-chan LetterCell
- func (puzzle *Puzzle) LookupWord(point Point, dir Direction) *Word
- func (puzzle *Puzzle) LookupWordByNumber(seq int, dir Direction) *Word
- func (puzzle *Puzzle) LookupWordNumber(seq int) *WordNumber
- func (puzzle *Puzzle) LookupWordNumberForStartingPoint(point Point) *WordNumber
- func (puzzle *Puzzle) PointIterator() <-chan Point
- func (puzzle *Puzzle) PuzzleNameUsed(userid int, puzzlename string) bool
- func (puzzle *Puzzle) RedoBlackCell()
- func (puzzle *Puzzle) RedoWord()
- func (puzzle *Puzzle) RenamePuzzle(userid int, oldPuzzleName, newPuzzleName string) error
- func (puzzle *Puzzle) RenumberCells()
- func (puzzle *Puzzle) SavePuzzle(userid int) error
- func (puzzle *Puzzle) SetCell(point Point, cell Cell)
- func (puzzle *Puzzle) SetClue(word *Word, clue string) error
- func (puzzle *Puzzle) SetLetter(point Point, letter string)
- func (puzzle *Puzzle) SetPuzzleName(name string)
- func (puzzle *Puzzle) SetText(word *Word, text string) error
- func (puzzle *Puzzle) SetTextWithoutPush(word *Word, text string)
- func (puzzle *Puzzle) String() string
- func (puzzle *Puzzle) SymmetricPoint(point Point) Point
- func (puzzle *Puzzle) Toggle(point Point)
- func (puzzle *Puzzle) UndoBlackCell()
- func (puzzle *Puzzle) UndoWord()
- func (puzzle *Puzzle) ValidIndex(point Point) error
- func (puzzle *Puzzle) WordIterator(point Point, dir Direction) <-chan Point
- type Word
- type WordNumber
Constants ¶
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
BLACK_CELL = cwcomp.BLACK_CELL
)
Variables ¶
This section is empty.
Functions ¶
func Blocks ¶ added in v0.10.0
Blocks is a generator that will take a list of indices and yield pairs of (first, last) indices of sublists that are consecutive integers. For example:
ilist = [3, 2, 3, 4, 5, 1, 1, 2, 3] yields 4 pairs: (3, 3) (2, 5) - 2 through 5 (1, 1) (1, 3) - 1 through 3
Call it like this:
c := Blocks(ilist) // c is the channel for pair := range c { // but you only need to use range first := pair.First last := pair.Last ... }
func Complement ¶ added in v0.10.0
Complement is a function that will return a list of indices into the ALPHABET string that are not in the list passed as a parameter
func CreateDatabase ¶ added in v0.10.0
func CreateDatabase()
CreateDatabase creates the database, either the production one or the test one, depending on Configuration.DATABASE.NAME
func DumpPuzzle ¶ added in v0.10.0
func DumpPuzzle(puzzle *Puzzle)
DumpPuzzle is a diagnostic function that shows the exact composition of each cell in the grid.
func GetDDL ¶ added in v0.10.0
func GetDDL() string
GetDDL returns a string containing the contents of the tables.sql file.
func GetMatchingWords ¶ added in v0.10.0
GetMatchingWords returns a slice of words that match the regular expression pattern.
func LoadDictionary ¶ added in v0.10.0
func LoadDictionary()
LoadDictionary loads the dictionary at startup. I was going to get fancy and use a goroutine to do this, but it takes only less than a second on my Linux machine (as opposed to 5-6 seconds in the Python version!)
func Pattern ¶ added in v0.10.0
Pattern creates part of a regular expression string that will go between brackets. For example, if the input list is []int{2, 3, 4, 5, 25}, it will return "C-DF"
func PuzzleToSimpleMatrix ¶ added in v0.10.0
PuzzleToSimpleMatrix builds a simple representation of a grid as an n x n matrix of bytes, where '\x00' represents a black cell, and the rest are the letters in that cell.
func Regexp ¶ added in v0.10.0
Regexp will return a regular expression representing the list of letters
func TracePuzzle ¶ added in v0.10.0
func TracePuzzle(puzzle *Puzzle)
TracePuzzle is a diagnostic function that shows how each cell is constructed
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 for this cell // 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 Dictionary ¶ added in v0.10.0
type Dictionary []string
Dictionary is the in-memory word list. There is an entry in this map for each word encountered in the words.txt file.
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 Importer ¶ added in v0.10.0
type Importer interface { // Returns the number of rows or columns in this puzzle GetSize() int // Returns the puzzle name, which will be used as part of the key in // the database representation. // // This is not the same as the puzzle title GetName() string // Returns the puzzle title, which is a descriptive string that is // typically used as the heading of the page it is printed on in the // newspaper. GetTitle() string // Returns the letter at a given point in the grid. These are // relative to 1, not 0, so // // r = 1, 2, ..., n c = 1, 2, ..., n // // If the letter value is '\x00', it is a black cell. Otherwise, it is // converted to uppercase. If the letter is not a black cell and // not in the alphabet A-Z, an error is returned. GetCell(r, c int) (byte, error) // Returns a map of across word numbers to clues GetAcrossClues() map[int]string // Returns a map of down word numbers to clues GetDownClues() map[int]string }
Importer is an interface that must be implemented by any source of puzzle data that can be imported (e.g., AcrossLite)
type LetterCell ¶
type LetterCell struct {
// contains filtered or unexported fields
}
Letter cell is an ordinary point in the grid. It contains:
- The location of the cell, a Point(r, c)
- 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 Puzzle ¶ added in v0.10.0
type Puzzle struct {
// contains filtered or unexported fields
}
Puzzle contains the cells of a grid.
The grid is constructed with the single parameter n, which is the size (n x n) of the grid.
Any of the cells in the puzzle 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 puzzle assigns the next available word number to the cell and keeps track of the lengths of the across and down words.
Puzzle 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 ImportPuzzle ¶ added in v0.10.0
ImportPuzzle creates a puzzle from an external source
func LoadPuzzle ¶ added in v0.10.0
LoadPuzzle reads puzzle data from the database and creates a Puzzle object from it.
func (*Puzzle) BlackCellIterator ¶ added in v0.10.0
BlackCellIterator is a generator for all the black cells in the grid.
func (*Puzzle) CellIterator ¶ added in v0.10.0
CellIterator is a generator for all the cells in the grid, from top to bottom, left to right (same as PointIterator).
func (*Puzzle) CountBlackCells ¶ added in v0.10.0
CountBlackCells returns the number of black cells in the grid
func (*Puzzle) DeletePuzzle ¶ added in v0.10.0
DeletePuzzle deletes the specified puzzle
func (*Puzzle) Equal ¶ added in v0.10.0
Equal returns true if this puzzle is essentially equal to the other.
func (*Puzzle) GetCell ¶ added in v0.10.0
GetCell returns the cell at the specified point, which may be a black cell or a letter cell.
func (*Puzzle) GetConstraints ¶ added in v0.10.0
func (puzzle *Puzzle) GetConstraints(word *Word) []*Constraint
GetConstraints finds the constraints imposed on this word by its crossing words.
func (*Puzzle) GetLetter ¶ added in v0.10.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 (*Puzzle) GetPuzzleList ¶ added in v0.10.0
GetPuzzleList returns a list of puzzles for the specified user.
func (*Puzzle) GetPuzzleName ¶ added in v0.10.0
GetPuzzleName returns the puzzle name
func (*Puzzle) GetWordNumber ¶ added in v0.10.0
func (puzzle *Puzzle) GetWordNumber(word *Word) *WordNumber
Given a word, returns the word number for it
func (*Puzzle) IsBlackCell ¶ added in v0.10.0
IsBlackCell returns true if the specified point is a black cell.
func (*Puzzle) LetterCellIterator ¶ added in v0.10.0
func (puzzle *Puzzle) LetterCellIterator() <-chan LetterCell
LetterCellIterator is a generator for all the LetterCells in the grid.
func (*Puzzle) LookupWord ¶ added in v0.10.0
LookupWord returns the word containing this point and direction
func (*Puzzle) LookupWordByNumber ¶ added in v0.10.0
LookupWordByNumber returns the word at this point and direction
func (*Puzzle) LookupWordNumber ¶ added in v0.10.0
func (puzzle *Puzzle) LookupWordNumber(seq int) *WordNumber
LookupWordNumber returns the WordNumber for this number
func (*Puzzle) LookupWordNumberForStartingPoint ¶ added in v0.10.0
func (puzzle *Puzzle) LookupWordNumberForStartingPoint(point Point) *WordNumber
LookupWordNumberForStartingPoint returns the WordNumber starting at this point.
func (*Puzzle) PointIterator ¶ added in v0.10.0
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 (*Puzzle) PuzzleNameUsed ¶ added in v0.10.0
PuzzleNameUsed returns true if the specified puzzle name for this user is already saved in the database
func (*Puzzle) RedoBlackCell ¶ added in v0.10.0
func (puzzle *Puzzle) RedoBlackCell()
RedoBlackCell pops a point from the redo stack and toggles the black cell at that point.
func (*Puzzle) RedoWord ¶ added in v0.10.0
func (puzzle *Puzzle) RedoWord()
RedoWord gets the last word change to the puzzle and re-applies it
func (*Puzzle) RenamePuzzle ¶ added in v0.10.0
RenamePuzzle renames a puzzle in the database
func (*Puzzle) RenumberCells ¶ added in v0.10.0
func (puzzle *Puzzle) RenumberCells()
RenumberCells assigns the word numbers based on the locations of the black cells.
func (*Puzzle) SavePuzzle ¶ added in v0.10.0
SavePuzzle adds or updates a record for this puzzle in the database
func (*Puzzle) SetLetter ¶ added in v0.10.0
SetLetter sets the letter value of the cell at the specified point
func (*Puzzle) SetPuzzleName ¶ added in v0.10.0
SetPuzzleName sets the puzzle name
func (*Puzzle) SetTextWithoutPush ¶ added in v0.10.0
func (*Puzzle) SymmetricPoint ¶ added in v0.10.0
SymmetricPoint returns the point of the cell at 180 degrees rotation.
func (*Puzzle) Toggle ¶ added in v0.10.0
Toggle switches a point between black cell and letter cell. Does so also to the symmetric point.
func (*Puzzle) UndoBlackCell ¶ added in v0.10.0
func (puzzle *Puzzle) UndoBlackCell()
UndoBlackCell pops a point from the undo stack and toggles the black cell at that point.
func (*Puzzle) UndoWord ¶ added in v0.10.0
func (puzzle *Puzzle) UndoWord()
UndoWord undoes the last push to the undoWordStack
func (*Puzzle) ValidIndex ¶ added in v0.10.0
ValidIndex whether a point is a valid index in this grid.
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 puzzle. 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