Documentation ¶
Index ¶
- Constants
- Variables
- func GetAvgOptions(g *Grid, difficulty Difficulty) float64
- func IsDistinct(g *Grid) bool
- func Render(g *Grid, activeSquare int) string
- func Validate(g *Grid) bool
- func ValidateFast(g *Grid) bool
- func ValidateIncr(g *Grid, i int, border int) bool
- func ValidatePartial(g *Grid, ixs []int) bool
- type Difficulty
- type Grid
- func Enumerate(g *Grid) []*Grid
- func EnumerateFilter(g *Grid, filter gridPredicate) []*Grid
- func EnumerateLimited(g *Grid) []*Grid
- func EnumerateSquare(g *Grid, i int) []*Grid
- func Generate(width int, height int, duration time.Duration) *Grid
- func New(width int, height int) *Grid
- func Obfuscate(g *Grid, difficulty Difficulty, duration time.Duration) *Grid
- func Parse(s string) *Grid
- func Solve(g *Grid) *Grid
- func SolveBruteForce(g *Grid) *Grid
- func SolveIterative(g *Grid, difficulty Difficulty) *Grid
- func SolveTechnically(g *Grid, difficulty Difficulty) *Grid
- func (g *Grid) Clone() *Grid
- func (g *Grid) Coords(i int) (x int, y int)
- func (g *Grid) CountSquares(s Square) (count int)
- func (g *Grid) FillUndefined(s Square) *Grid
- func (g *Grid) HasSquare(val Square) bool
- func (g *Grid) ID() *big.Int
- func (g *Grid) Index(x int, y int) (int, bool)
- func (g *Grid) IsUndefined() bool
- func (g *Grid) Mirror(horizontal bool) *Grid
- func (g *Grid) NeighborCount(i int, square Square, adjacentOnly bool, includeUndefined bool) (count int)
- func (g *Grid) NeighborCount4(i int, square Square) int
- func (g *Grid) NeighborCount8(i int, square Square) int
- func (g *Grid) NeighborIndicesi(i int, adjacentOnly bool) []int
- func (g *Grid) Normalize() *Grid
- func (g *Grid) PuzzleRating() float64
- func (g *Grid) Rotate(left bool) *Grid
- func (g *Grid) SetDragon(i int) *Grid
- func (g *Grid) SetSquare(x int, y int, val Square) *Grid
- func (g *Grid) SetSquarei(i int, val Square) *Grid
- func (g *Grid) SetSquareiAndValidate(i int, val Square) bool
- func (g *Grid) Size() int
- func (g *Grid) SolutionRating() float64
- func (g *Grid) Square(x int, y int) Square
- func (g *Grid) Squarei(i int) Square
- func (g *Grid) String() string
- func (g *Grid) Undefinedness() float64
- type Square
Constants ¶
const ( // DifficultyUnknown represents unknown difficulty. DifficultyUnknown = iota // DifficultyEasy represents "easy" puzzles. DifficultyEasy // DifficultyMedium represents "medium" puzzles. DifficultyMedium // DifficultyHard represents "hard" puzzles. DifficultyHard // DifficultyBrutal represents "brutal" puzzles (only solvable using brute force). DifficultyBrutal )
Variables ¶
var AllFields = []Square{ SquareUndefined, SquareAir, SquareFire, SquareDragon, SquareOut, }
AllFields are a list of all possible field values.
Functions ¶
func GetAvgOptions ¶
func GetAvgOptions(g *Grid, difficulty Difficulty) float64
GetAvgOptions gets the average options of possible solve techniques that can be applied per square.
func IsDistinct ¶
IsDistinct returns true if there is exactly one solution to the given grid.
func Validate ¶
Validate validates, if a grid is in a valid state. This applies all validation rules to the grid state. A valid state does not necessarily mean, that it can lead to a solution.
func ValidateFast ¶
ValidateFast validates the grid using a lookup table to be more performant than Validate.
func ValidateIncr ¶
ValidateIncr validates only the changed squares and the squares around it. The goal is to optimize performance by validating only squares that were changed. This function is therefore best used when validating incrementally.
func ValidatePartial ¶
ValidatePartial validates only some squares (ixs) of the whole grid. The goal is to optimize performance by validating only squares that were changed. This function is therefore best used when validating incrementally.
Types ¶
type Difficulty ¶
type Difficulty int
Difficulty represents possible difficulty levels.
func GetDifficulty ¶
func GetDifficulty(g *Grid) Difficulty
GetDifficulty gets the difficulty of a puzzle.
func ParseDifficulty ¶
func ParseDifficulty(str string) Difficulty
ParseDifficulty takes a string and returns its Difficulty value.
func (Difficulty) String ¶
func (difficulty Difficulty) String() string
type Grid ¶
Grid represents the puzzles state.
func EnumerateFilter ¶
EnumerateFilter enumerates all possible successors from a given grid and allows to filter only the wanted grids.
func EnumerateLimited ¶
EnumerateLimited enumerates all possible successors from a given grid but stops as soon as there are more than one solution.
func EnumerateSquare ¶
EnumerateSquare enumerates all possible grids if a specified square is undefined. Otherwise, the given grid is returned as single possible solution.
func Generate ¶
Generate generates a random but solved grid with the given dimensions. The duration parameter controls the time that is invested in generating the best or most interesting grid possible.
func Obfuscate ¶
func Obfuscate(g *Grid, difficulty Difficulty, duration time.Duration) *Grid
Obfuscate creates a puzzle from a given solved or partially solved puzzle. The difficulty parameter controls how hard the puzzle would be to solve for a human.
The algorithm works like this: It takes a grid state and incrementally sets random squares to "undefined". After every step, it verifies if the puzzle is still solvable (i.e. has a distinct solution).
func Solve ¶
Solve solves a puzzle using multiple algorithms if necessary. it returns the only possible solution or "nil" otherwise.
func SolveBruteForce ¶
SolveBruteForce solves a puzzle using a brute force strategy (enumerating all possible states). It's the only algorithm that finds solutions to the hardest puzzles. It returns the only possible solution or "nil" otherwise.
func SolveIterative ¶
func SolveIterative(g *Grid, difficulty Difficulty) *Grid
SolveIterative solves a puzzle by iteratively checking possible options for a given square. It only uses the validation rules of the game - no domain knowledge required. Can be tuned by the maximum number of possible permutations that can exist in order to exclude a option. It returns the only possible solution or "nil" otherwise.
func SolveTechnically ¶
func SolveTechnically(g *Grid, difficulty Difficulty) *Grid
SolveTechnically solves a puzzle applying fix techniques (using domain knowledge). It returns the only possible solution or "nil" otherwise.
func (*Grid) CountSquares ¶
CountSquares counts all squares with a specified value.
func (*Grid) FillUndefined ¶
FillUndefined fills all undefined squares of a grid with the specified value.
TODO: move to Builder type
func (*Grid) HasSquare ¶
HasSquare returns "true" if at least one square with the specified value exists.
func (*Grid) ID ¶
ID creates a unique identifier for a puzzle. This helps when we want to normalize puzzles.
Code is constructed as follows:
most significant bit <------------------------------- least significant bit [ last square ] ... [ square 0 (2bits) ][ height (5bits) ][ width (5bits) ]
func (*Grid) IsUndefined ¶
IsUndefined return true if all squares are undefined
func (*Grid) NeighborCount ¶
func (g *Grid) NeighborCount(i int, square Square, adjacentOnly bool, includeUndefined bool) (count int)
NeighborCount counts the neighboured squares (all 8 or the 4 adjacent) that match the given type. If the 'includeUndefined' flag is set, also undefined squares are counted.
func (*Grid) NeighborCount4 ¶
NeighborCount4 counts the adacent neighboured squares that match the given type.
func (*Grid) NeighborCount8 ¶
NeighborCount8 counts the 8 neighboured squares that match the given type.
func (*Grid) NeighborIndicesi ¶
NeighborIndicesi gets the indices of all neighbor squares.
func (*Grid) Normalize ¶
Normalize normalizes a grid to prevent duplicates (rotated and mirrored grids).
func (*Grid) PuzzleRating ¶
PuzzleRating is used to generate puzzles from solutions. This value is used to find the best obfuscated puzzle among others.
TODO: use a sigmoid function or similar to clamp the value always between 0.0 and 1.0
func (*Grid) SetDragon ¶
SetDragon sets a dragon to a specific square and computes where fire must be.
TODO: move this into a Builder type
func (*Grid) SetSquarei ¶
SetSquarei sets the squares value at the specified index.
func (*Grid) SetSquareiAndValidate ¶
SetSquareiAndValidate sets a square to the specified value and validates the grid partially (only the changed square and its neighbors).
TODO: move to builde type
func (*Grid) SolutionRating ¶
SolutionRating returns the interestingness of a grid. This is useful if we want to sort possible solved grids. Density returns the density of a grid (number of dragons percentually).
func (*Grid) Undefinedness ¶
Undefinedness returns the undefinedness of a unsolved puzzle grid. This is especially useful to find "interesting" puzzle.
type Square ¶
type Square uint8
Square represents possible cell values.
const ( // SquareUndefined represents undefined squares. SquareUndefined Square = iota // SquareAir represents squares with air. SquareAir // SquareFire represents squares with fire. SquareFire // SquareDragon represents squares with dragons. SquareDragon // SquareOut represents squares that are outside of the grid. SquareOut // SquareNoDragon represents squares that can not be dragons. SquareNoDragon )