Class Nonogram
- Namespace
- NonoSharp
- Assembly
- NonoSharp.dll
Class for a Nonogram puzzle. Can be initialised with static methods such as CreateRandomPuzzle(int, int, NonogramOptions?) or LoadPuzzle(string, NonogramOptions?), or by using the constructor with your own solution, Nonogram(int, int, ISet<CellPosition>, NonogramOptions?).
public class Nonogram
- Inheritance
-
Nonogram
- Inherited Members
Constructors
Nonogram(int, int, Clues[], Clues[], NonogramOptions?)
Creates a Nonogram with a solution based on columnClues and rowClues.
public Nonogram(int width, int height, Clues[] columnClues, Clues[] rowClues, NonogramOptions? options = null)
Parameters
widthintWidth of the puzzle.
heightintHeight of the puzzle.
columnCluesClues[]Array of Clues instances, one for each column of the puzzle.
rowCluesClues[]Array of Clues instances, one for each row of the puzzle.
optionsNonogramOptionsThe NonogramOptions to use. Leave as
nullto use the default options.
Remarks
The puzzle must be uniquely solvable.
Exceptions
- ArgumentException
Thrown when the lengths of
columnCluesandrowCluesdo not matchwidthorheightrespectively.- ArgumentOutOfRangeException
Thrown when
widthorheightare non-positive.- PuzzleNotSolvableException
Thrown when the puzzle constructed from the clues and dimensions is not uniquely solvable.
Nonogram(int, int, ISet<CellPosition>, NonogramOptions?)
Creates a Nonogram instance with a width of width, height of
height and sets the solution to solution.
public Nonogram(int width, int height, ISet<CellPosition> solution, NonogramOptions? options = null)
Parameters
widthintThe width of the puzzle.
heightintThe height of the puzzle.
solutionISet<CellPosition>The solution of the puzzle.
optionsNonogramOptionsThe NonogramOptions to use. Leave as
nullto use the default options.
Remarks
It is not checked whether the created puzzle can be solved. As such, using this constructor with an improper
solution can lead to the puzzle being unsolvable. To create puzzles that are guaranteed to be
uniquely solvable, use NonogramBuilder or create a random solvable puzzle with methods such as
CreateRandomPuzzle(int, int, NonogramOptions?).
Exceptions
- ArgumentOutOfRangeException
Thrown when width or height are non-positive.
- IndexOutOfRangeException
Thrown when at least one of the CellPositions found in
solutionis out of the bounds set bywidthandheight.
Properties
CanRedo
A boolean indicating whether a redo via Redo() is possible.
public bool CanRedo { get; }
Property Value
CanUndo
A boolean indicating whether an undo via Undo() is possible.
public bool CanUndo { get; }
Property Value
ColumnClues
The clues, i.e. the numbers on the side of a grid, for the columns of the grid. In Nonogram puzzles these are usually shown at the top of the grid.
public Clues[] ColumnClues { get; }
Property Value
- Clues[]
Columns
The columns of this instance's puzzle grid.
public Line[] Columns { get; }
Property Value
- Line[]
Height
The height of the game grid.
public int Height { get; }
Property Value
this[int, int]
Gets or sets the CellType of the cell at (x, y).
public CellType this[int x, int y] { get; set; }
Parameters
Property Value
- CellType
CellType of the cell located at (
x,y).
Options
The NonogramOptions for this instance.
public NonogramOptions Options { get; set; }
Property Value
RowClues
The clues, i.e. the numbers on the side of a grid, for the rows of the grid. In Nonogram puzzles these are usually shown at the left side of the grid.
public Clues[] RowClues { get; }
Property Value
- Clues[]
Rows
The rows of this instance's puzzle grid.
public Line[] Rows { get; }
Property Value
- Line[]
Solution
The solution for the current puzzle, i.e. the cells (and only those cells) that must be filled for the puzzle to be solved.
public FrozenSet<CellPosition> Solution { get; }
Property Value
Width
The width of the game grid.
public int Width { get; }
Property Value
Methods
CreateRandomPuzzle(int, int, NonogramOptions?)
Creates a Nonogram instance with a random puzzle. See CreateRandomPuzzleAsync(int, int, NonogramOptions?) for the asynchronous method.
public static Nonogram CreateRandomPuzzle(int width, int height, NonogramOptions? options = null)
Parameters
widthintWidth of the grid for the game.
heightintHeight of the grid for the game.
optionsNonogramOptionsThe NonogramOptions to use. Leave as
nullto use the default options.
Returns
- Nonogram
Nonogram instance as described above.
Remarks
Generating solvable puzzles is computationally expensive and the runtime increases drastically with
width and height. To ensure a fast runtime, try to keep puzzle dimensions
small. The library has no (theoretical) restrictions on width or height,
but it is not recommend to generate large puzzles.
Exceptions
- ArgumentOutOfRangeException
Thrown when width or height are non-positive.
CreateRandomPuzzle(int, int, int, NonogramOptions?)
Creates a Nonogram instance with a random puzzle. See CreateRandomPuzzleAsync(int, int, NonogramOptions?) for the asynchronous method.
public static Nonogram CreateRandomPuzzle(int width, int height, int seed, NonogramOptions? options = null)
Parameters
widthintWidth of the grid for the game.
heightintHeight of the grid for the game.
seedintThe seed to use for randomisation when generating a puzzle.
optionsNonogramOptionsThe NonogramOptions to use. Leave as
nullto use the default options.
Returns
- Nonogram
Nonogram instance as described above.
Remarks
Generating solvable puzzles is computationally expensive and the runtime increases drastically with
width and height. To ensure a fast runtime, try to keep puzzle dimensions
small. The library has no (theoretical) restrictions on width or height,
but it is not recommend to generate large puzzles.
Exceptions
- ArgumentOutOfRangeException
Thrown when width or height are non-positive.
CreateRandomPuzzleAsync(int, int, NonogramOptions?)
Creates a Nonogram instance with a random puzzle asynchronously by running CreateRandomPuzzle(int, int, NonogramOptions?) on the ThreadPool as it is computionally expensive.
public static Task<Nonogram> CreateRandomPuzzleAsync(int width, int height, NonogramOptions? options = null)
Parameters
widthintWidth of the grid for the game.
heightintHeight of the grid for the game.
optionsNonogramOptionsThe NonogramOptions to use. Leave as
nullto use the default options.
Returns
Remarks
Generating solvable puzzles is computationally expensive and the runtime increases drastically with
width and height. To ensure a fast runtime, try to keep puzzle dimensions
small. The library has no (theoretical) restrictions on width or height,
but it is not recommend to generate large puzzles.
Exceptions
- ArgumentOutOfRangeException
Thrown when width or height are non-positive.
CreateRandomPuzzleAsync(int, int, int, NonogramOptions?)
Creates a Nonogram instance with a random puzzle asynchronously by running CreateRandomPuzzle(int, int, NonogramOptions?) on the ThreadPool as it is computionally expensive.
public static Task<Nonogram> CreateRandomPuzzleAsync(int width, int height, int seed, NonogramOptions? options = null)
Parameters
widthintWidth of the grid for the game.
heightintHeight of the grid for the game.
seedintThe seed to use for randomisation when generating a puzzle.
optionsNonogramOptionsThe NonogramOptions to use. Leave as
nullto use the default options.
Returns
Remarks
Generating solvable puzzles is computationally expensive and the runtime increases drastically with
width and height. To ensure a fast runtime, try to keep puzzle dimensions
small. The library has no (theoretical) restrictions on width or height,
but it is not recommend to generate large puzzles.
Exceptions
- ArgumentOutOfRangeException
Thrown when width or height are non-positive.
CrossCell(CellPosition)
Marks the cell at position as crossed.
public void CrossCell(CellPosition position)
Parameters
positionCellPositionThe position of the cell to cross.
Exceptions
- ArgumentOutOfRangeException
Thrown when
xoryfalls outside the bounds of the grid.
CrossCell(int, int)
Marks the cell at (x, y) as crossed.
public void CrossCell(int x, int y)
Parameters
xintx-coordinate of the cell, zero-indexed from the left.
yinty-coordinate of the cell, zero-indexed from the top.
Exceptions
- ArgumentOutOfRangeException
Thrown when
xoryfalls outside the bounds of the grid.
EmptyCell(CellPosition)
Clears the cell at position, returning it to blank.
public void EmptyCell(CellPosition position)
Parameters
positionCellPositionThe position of the cell to empty.
Exceptions
- ArgumentOutOfRangeException
Thrown when
xoryfalls outside the bounds of the grid.
EmptyCell(int, int)
Clears the cell at (x, y), returning it to blank.
public void EmptyCell(int x, int y)
Parameters
xintx-coordinate of the cell, zero-indexed from the left.
yinty-coordinate of the cell, zero-indexed from the top.
Exceptions
- ArgumentOutOfRangeException
Thrown when
xoryfalls outside the bounds of the grid.
FillCell(CellPosition)
Fills the cell at position.
public void FillCell(CellPosition position)
Parameters
positionCellPositionThe position of the cell to fill.
Exceptions
- ArgumentOutOfRangeException
Thrown when
xoryfalls outside the bounds of the grid.
FillCell(int, int)
Fills the cell at (x, y).
public void FillCell(int x, int y)
Parameters
xintx-coordinate of the cell, zero-indexed from the left.
yinty-coordinate of the cell, zero-indexed from the top.
Exceptions
- ArgumentOutOfRangeException
Thrown when
xoryfalls outside the bounds of the grid.
GetCell(CellPosition)
Gets the CellType of the cell at position.
public CellType GetCell(CellPosition position)
Parameters
positionCellPositionThe position of the cell.
Returns
- CellType
CellType of the cell at
position.
Exceptions
- ArgumentOutOfRangeException
Thrown when
xoryis out of bounds of the grid.
GetCell(int, int)
Gets the CellType of the cell located at (x, y).
public CellType GetCell(int x, int y)
Parameters
Returns
- CellType
CellType of the cell at (
x,y).
Exceptions
- ArgumentOutOfRangeException
Thrown when
xoryis out of bounds of the grid.
IsCellCorrect(CellPosition)
Determines whether the cell at position is correctly placed according to the solution.
public bool IsCellCorrect(CellPosition position)
Parameters
positionCellPositionThe position to check.
Returns
- bool
trueif the cell is placed correctly,falseotherwise.
Exceptions
- ArgumentOutOfRangeException
Thrown when the cell to check is out of the grid bounds.
IsCellCorrect(int, int)
Determines whether the cell at (x, y) is correctly placed
according to the solution.
public bool IsCellCorrect(int x, int y)
Parameters
Returns
- bool
trueif the cell is placed correctly,falseotherwise.
Exceptions
- ArgumentOutOfRangeException
Thrown when the cell to check is out of the grid bounds.
IsCellCrossed(CellPosition)
Determines whether the cell at position is crossed.
public bool IsCellCrossed(CellPosition position)
Parameters
positionCellPositionPosition to check.
Returns
- bool
trueif the cell is crossed,falseotherwise.
Exceptions
- ArgumentOutOfRangeException
Thrown when the cell to check is out of the grid bounds.
IsCellCrossed(int, int)
Determines whether the cell at (x, y) is crossed.
public bool IsCellCrossed(int x, int y)
Parameters
Returns
- bool
trueif the cell is crossed,falseotherwise.
Exceptions
- ArgumentOutOfRangeException
Thrown when the cell to check is out of the grid bounds.
IsCellEmpty(CellPosition)
Determines whether the cell at position is empty.
public bool IsCellEmpty(CellPosition position)
Parameters
positionCellPositionPosition to check.
Returns
- bool
trueif the cell is empty,falseotherwise.
Exceptions
- ArgumentOutOfRangeException
Thrown when the cell to check is out of the grid bounds.
IsCellEmpty(int, int)
Determines whether the cell at (x, y) is empty.
public bool IsCellEmpty(int x, int y)
Parameters
Returns
- bool
trueif the cell is empty,falseotherwise.
Exceptions
- ArgumentOutOfRangeException
Thrown when the cell to check is out of the grid bounds.
IsCellFilled(CellPosition)
Determines whether the cell at position is filled.
public bool IsCellFilled(CellPosition position)
Parameters
positionCellPositionPosition to check.
Returns
- bool
trueif the cell is filled,falseotherwise.
Exceptions
- ArgumentOutOfRangeException
Thrown when the cell to check is out of the grid bounds.
IsCellFilled(int, int)
Determines whether the cell at (x, y) is filled.
public bool IsCellFilled(int x, int y)
Parameters
Returns
- bool
trueif the cell is filled,falseotherwise.
Exceptions
- ArgumentOutOfRangeException
Thrown when the cell to check is out of the grid bounds.
IsPuzzleSolved()
Determines whether the puzzle is solved, i.e. the filled cells match the solution exactly.
public bool IsPuzzleSolved()
Returns
- bool
trueif the puzzle is solved,falseotherwise.
LoadPuzzle(Stream, NonogramOptions?)
Loads the puzzle in stream and returns a new Nonogram instance.
public static Nonogram LoadPuzzle(Stream stream, NonogramOptions? options = null)
Parameters
streamStreamStream to read the puzzle from. To avoid false positives on InvalidFileFormatException exceptions, the stream must consist of ONLY one valid puzzle, such as one provided by SavePuzzle(string).
optionsNonogramOptionsThe NonogramOptions to use. Leave as
nullto use the default options.
Returns
- Nonogram
Nonogram instance of the puzzle.
Remarks
stream is left open. Do not forget to close it.
Exceptions
- InvalidFileFormatException
Thrown when the given file format is not supported.
- NotSupportedException
Thrown when the version of the save system is not supported.
- ArgumentNullException
Thrown when
streamisnull.
LoadPuzzle(string, NonogramOptions?)
Loads the puzzle at path and returns a new Nonogram instance.
public static Nonogram LoadPuzzle(string path, NonogramOptions? options = null)
Parameters
pathstringPuzzle to load.
optionsNonogramOptionsThe NonogramOptions to use. Leave as
nullto use the default options.
Returns
- Nonogram
Nonogram instance of the puzzle located at the given path.
Exceptions
- InvalidFileFormatException
Thrown when the given file format is not supported.
- NotSupportedException
Thrown when the version of the save system is not supported.
- PuzzleLoadingFailedException
Thrown when loading files fails, e.g. because of an I/O Exception. See the inner exception for more details.
- ArgumentNullException
Thrown when
pathisnullor empty.
LoadPuzzleAsync(Stream, NonogramOptions?)
Loads the puzzle in stream and then asynchronously converts the read data into a usable
Nonogram.
public static Task<Nonogram> LoadPuzzleAsync(Stream stream, NonogramOptions? options = null)
Parameters
streamStreamStream to read the puzzle from. To avoid false positives on InvalidFileFormatException exceptions, the stream must consist of ONLY one valid puzzle, such as one provided by SavePuzzle(string).
optionsNonogramOptionsThe NonogramOptions to use. Leave as
nullto use the default options
Returns
Remarks
Contents of stream are expected to be relatively small and is read synchronously.
Larger stream contents might cause noticeable blocking.
stream is left open. Do not forget to close it.
Exceptions
- InvalidFileFormatException
Thrown when the given file format is not supported
- NotSupportedException
Thrown when the version of the save system is not supported
- ArgumentNullException
Thrown when
streamisnull
LoadPuzzleAsync(string, NonogramOptions?)
Loads the puzzle at path asynchronously and returns a new Nonogram instance.
public static Task<Nonogram> LoadPuzzleAsync(string path, NonogramOptions? options = null)
Parameters
pathstringPuzzle to load
optionsNonogramOptionsThe NonogramOptions to use. Leave as
nullto use the default options
Returns
Exceptions
- InvalidFileFormatException
Thrown when the given file format is not supported
- NotSupportedException
Thrown when the version of the save system is not supported
- PuzzleLoadingFailedException
Thrown when loading files fails, e.g. because of an I/O Exception. See the inner exception for more details
- ArgumentNullException
Thrown when
pathisnullor empty
Redo()
Redo the last undone move (if any). Silently returns if there is no command to redo.
public void Redo()
SaveAsFile(string, string?)
Saves a serialized version of the puzzle (that is, the solution and dimensions) to path.
If path already exists, it is overwritten.
public void SaveAsFile(string path, string? title = null)
Parameters
Exceptions
- PuzzleSerializationFailedException
Thrown when serialization failed. For example, when the given title is too long, or an I/O exception occurs. Usually, there is an inner exception giving more details.
- PuzzleSavingFailedException
Thrown when saving files fails, e.g. because of an I/O Exception. See the inner exception for more details.
- ArgumentNullException
Thrown when
pathisnullor empty.
SaveAsFileAsync(string, string?)
Saves a serialized version of the puzzle (that is, the solution and dimensions) to path.
If path already exists, it is overwritten.
public Task SaveAsFileAsync(string path, string? title = null)
Parameters
Returns
Exceptions
- PuzzleSerializationFailedException
Thrown when serialization failed. For example, when the given title is too long, or an I/O exception occurs. Usually, there is an inner exception giving more details.
- PuzzleSavingFailedException
Thrown when saving files fails, e.g. because of an I/O Exception. See the inner exception for more details.
- ArgumentNullException
Thrown when
pathisnullor empty.
SetCell(int, int, CellType)
Sets the cell at (x, y) to newCellType.
public void SetCell(int x, int y, CellType newCellType)
Parameters
xintx-coordinate of the cell, zero-indexed from the left.
yinty-coordinate of the cell, zero-indexed from the top.
newCellTypeCellTypeThe CellType to set the cell at (
x,y) to.
Exceptions
- ArgumentOutOfRangeException
Thrown when
xoryfalls outside the bounds of the grid.
ToString()
Converts this instance into a nice string representation of the grid. This includes all cells and their respective states and the clues at the sides.
public override string ToString()
Returns
- string
A string showing the current state of the game
Undo()
Undoes the last move (if any). Silently returns if there is no command to undo.
public void Undo()
Events
CellCorrected
Raised when a cell is changed incorrectly and is therefore corrected to the expected state of the cell.
public event EventHandler<CorrectionEventArgs>? CellCorrected
Event Type
CellStateChanged
CellStateChanged is raised when one or more cells change to a new state.
public event EventHandler<CellStateEventArgs>? CellStateChanged
Event Type
PuzzleSolved
Raised when the puzzle has been solved.
public event EventHandler? PuzzleSolved