model

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Model is the internal component of the application, used by the controller and the view.

Index

Constants

View Source
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
View Source
const (
	BLACK_CELL = cwcomp.BLACK_CELL
)

Variables

This section is empty.

Functions

func Blocks added in v0.10.0

func Blocks(ilist []int) <-chan Block

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

func Complement(ilist []int) []int

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 Connect added in v0.10.0

func Connect() (*sql.DB, error)

Connect opens a connection to the cwcomp database.

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

func GetMatchingWords(pattern string, stop <-chan struct{}) <-chan string

GetMatchingWords returns a slice of words that match the regular expression pattern.

func Hash256 added in v0.10.0

func Hash256(s string) []byte

Hash256 returns the sha256 of the specified string

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

func Pattern(ilist []int) string

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

func PuzzleToSimpleMatrix(puzzle *Puzzle) [][]byte

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

func Regexp(letters string) string

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

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 Block added in v0.10.0

type Block struct {
	First int
	Last  int
}

Used by GetBlocks

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

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(puzzle *Puzzle, 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 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 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 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

func ImportPuzzle(source Importer) (*Puzzle, error)

ImportPuzzle creates a puzzle from an external source

func LoadPuzzle added in v0.10.0

func LoadPuzzle(userid int, puzzlename string) (*Puzzle, error)

LoadPuzzle reads puzzle data from the database and creates a Puzzle object from it.

func NewPuzzle added in v0.10.0

func NewPuzzle(n int) *Puzzle

NewPuzzle creates a puzzle of the specified size.

func (*Puzzle) BlackCellIterator added in v0.10.0

func (puzzle *Puzzle) BlackCellIterator() <-chan BlackCell

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

func (*Puzzle) CellIterator added in v0.10.0

func (puzzle *Puzzle) 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 (*Puzzle) CountBlackCells added in v0.10.0

func (puzzle *Puzzle) CountBlackCells() int

CountBlackCells returns the number of black cells in the grid

func (*Puzzle) DeletePuzzle added in v0.10.0

func (puzzle *Puzzle) DeletePuzzle(userid int, puzzlename string) error

DeletePuzzle deletes the specified puzzle

func (*Puzzle) Equal added in v0.10.0

func (puzzle *Puzzle) Equal(other *Puzzle) bool

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

func (*Puzzle) GetCell added in v0.10.0

func (puzzle *Puzzle) GetCell(point Point) Cell

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

func (*Puzzle) GetClue added in v0.10.0

func (puzzle *Puzzle) GetClue(word *Word) (string, error)

GetClue returns the clue for the word.

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) GetLength added in v0.10.0

func (puzzle *Puzzle) GetLength(word *Word) (int, error)

GetLength returns the length of the word.

func (*Puzzle) GetLetter added in v0.10.0

func (puzzle *Puzzle) 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 (*Puzzle) GetPuzzleList added in v0.10.0

func (puzzle *Puzzle) GetPuzzleList(userid int) []string

GetPuzzleList returns a list of puzzles for the specified user.

func (*Puzzle) GetPuzzleName added in v0.10.0

func (puzzle *Puzzle) GetPuzzleName() string

GetPuzzleName returns the puzzle name

func (*Puzzle) GetText added in v0.10.0

func (puzzle *Puzzle) GetText(word *Word) string

GetText returns the text of the word.

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

func (puzzle *Puzzle) IsBlackCell(point Point) bool

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

func (puzzle *Puzzle) LookupWord(point Point, dir Direction) *Word

LookupWord returns the word containing this point and direction

func (*Puzzle) LookupWordByNumber added in v0.10.0

func (puzzle *Puzzle) LookupWordByNumber(seq int, dir Direction) *Word

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

func (puzzle *Puzzle) 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 (*Puzzle) PuzzleNameUsed added in v0.10.0

func (puzzle *Puzzle) PuzzleNameUsed(userid int, puzzlename string) bool

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

func (puzzle *Puzzle) RenamePuzzle(userid int, oldPuzzleName, newPuzzleName string) error

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

func (puzzle *Puzzle) SavePuzzle(userid int) error

SavePuzzle adds or updates a record for this puzzle in the database

func (*Puzzle) SetCell added in v0.10.0

func (puzzle *Puzzle) SetCell(point Point, cell Cell)

SetCell sets the cell at the specified point

func (*Puzzle) SetClue added in v0.10.0

func (puzzle *Puzzle) SetClue(word *Word, clue string) error

SetClue sets the specified clue in the specified word.

func (*Puzzle) SetLetter added in v0.10.0

func (puzzle *Puzzle) SetLetter(point Point, letter string)

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

func (*Puzzle) SetPuzzleName added in v0.10.0

func (puzzle *Puzzle) SetPuzzleName(name string)

SetPuzzleName sets the puzzle name

func (*Puzzle) SetText added in v0.10.0

func (puzzle *Puzzle) SetText(word *Word, text string) error

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

func (*Puzzle) SetTextWithoutPush added in v0.10.0

func (puzzle *Puzzle) SetTextWithoutPush(word *Word, text string)

func (*Puzzle) String added in v0.10.0

func (puzzle *Puzzle) String() string

String returns a string representation of the puzzle

func (*Puzzle) SymmetricPoint added in v0.10.0

func (puzzle *Puzzle) SymmetricPoint(point Point) Point

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

func (*Puzzle) Toggle added in v0.10.0

func (puzzle *Puzzle) Toggle(point Point)

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

func (puzzle *Puzzle) ValidIndex(point Point) error

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

func (*Puzzle) WordIterator added in v0.10.0

func (puzzle *Puzzle) 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 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(puzzle *Puzzle) []*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 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

Jump to

Keyboard shortcuts

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