Skip to main content

NonogramGame

Struct NonogramGame 

Source
pub struct NonogramGame {
    pub width: usize,
    pub height: usize,
    pub cells: Vec<CellState>,
    pub row_clues: Vec<Clue>,
    pub col_clues: Vec<Clue>,
    pub status: GameStatus,
    /* private fields */
}
Expand description

The nonogram game state: board dimensions, the hidden solution, the player’s grid, derived clues, and undo/redo history.

There is no losing state — every action is reversible via Self::undo/Self::redo, and the game only ever transitions from GameStatus::Playing to GameStatus::Won (or back, if an undo reverts a winning move).

Fields§

§width: usize

Board width in cells.

§height: usize

Board height in cells.

§cells: Vec<CellState>

Flat row-major grid of the player’s current marks (cells[y * width + x]).

§row_clues: Vec<Clue>

Clue for each row, top to bottom.

§col_clues: Vec<Clue>

Clue for each column, left to right.

§status: GameStatus

Current game status.

Implementations§

Source§

impl NonogramGame

Source

pub fn from_grid(grid: Vec<Vec<bool>>) -> Self

Build a puzzle from an explicit solution grid (row-major, grid[y][x]). Every row must have the same width. Use this for curated/picture puzzles.

§Panics

Panics if grid is empty, any row is empty, or rows have differing lengths.

Source

pub fn random(width: usize, height: usize, density: f32, seed: u64) -> Self

Build a procedurally-generated puzzle: each cell is independently filled with probability density, reproducible via seed. Regenerates until the result isn’t degenerate (not all-empty, not all-filled), so the clues are always meaningful. density is clamped to 0.05..=0.95 — the exact extremes would make a non-degenerate result impossible (and the regeneration loop would never terminate).

§Panics

Panics if width or height is zero.

Source

pub fn reset(&mut self)

Clear the player’s grid and undo/redo history, keeping the same puzzle (solution and clues unchanged). To get a different puzzle, construct a new NonogramGame.

Source

pub fn idx(&self, x: usize, y: usize) -> usize

Source

pub fn is_solution_filled(&self, x: usize, y: usize) -> bool

Whether (x, y) is part of the hidden solution.

Source

pub fn toggle_fill(&mut self, x: usize, y: usize)

Toggle a cell between Filled and Empty (clearing any Crossed mark in the process). This is the left-click action.

Source

pub fn toggle_cross(&mut self, x: usize, y: usize)

Toggle a cell between Crossed and Empty (clearing any Filled mark in the process). This is the right-click action.

Source

pub fn clear_cell(&mut self, x: usize, y: usize)

Directly clear a cell back to Empty.

Source

pub fn fill(&mut self, x: usize, y: usize)

Directly set a cell to Filled, no toggling. Idempotent — a no-op (no undo entry pushed) if the cell is already Filled. Intended for click-and-drag painting, where the same target state is applied to every cell the pointer passes over.

Source

pub fn cross(&mut self, x: usize, y: usize)

Directly set a cell to Crossed, no toggling. Idempotent, for the same click-and-drag painting use case as Self::fill.

Source

pub fn row_satisfied(&self, y: usize) -> bool

Whether row y’s currently-filled cells already match its clue. Used both as a hint for the player (dim/strikethrough the clue) and, together with Self::col_satisfied, as the win condition.

Source

pub fn col_satisfied(&self, x: usize) -> bool

Whether column x’s currently-filled cells already match its clue. See Self::row_satisfied.

Source

pub fn can_undo(&self) -> bool

Source

pub fn can_redo(&self) -> bool

Source

pub fn undo(&mut self)

Source

pub fn redo(&mut self)

Source

pub fn random_logical( width: usize, height: usize, density: f32, seed: u64, ) -> Self

Generate a puzzle guaranteed solvable via line-solving only (no guessing). Uses rejection sampling: generates random puzzles and keeps only those that pass the line-solvability check.

The odds of a random puzzle being line-solvable depend heavily on width, height, and density, and not in an intuitive way — bigger grids need higher density, not lower, because line-solving relies on runs long enough to trigger overlap deduction. Some combinations are effectively unreachable at any density: e.g. on a square grid, a caller-supplied density much below the values found empirically below will retry until it panics. Measured on square grids (density is the lowest value found to reliably converge):

grid sizedensity
10x10~0.45
11x11~0.40
12x12~0.45
13x13~0.50
14x14~0.55
15x15~0.60

This table is empirical, not derived from a closed-form formula, and does not extend cleanly to rectangular grids: a 20x8 grid and a 20x11 grid behave very differently even though they share a longer dimension, so neither width nor height alone predicts difficulty. If you’re picking density for a grid shape not covered above, measure your own hit rate first — generate a batch of puzzles with NonogramGame::random at your intended size/density and check how many pass is_logically_solvable — rather than assuming this function will find one quickly.

Per-attempt cost also grows with grid size, and is markedly higher right at the solvability threshold than comfortably above it (a near-miss puzzle takes many line-solving passes to stabilize before giving up, where a puzzle solvable outright — or clearly hopeless — converges fast). Pick a density with margin above the threshold, not the bare minimum.

§Panics

Panics if no logically solvable puzzle is found within 200 attempts. That cap is intentionally small: unlike a puzzle generator that can afford to grind for a good result, this runs synchronously wherever it’s called (e.g. on a UI thread), so a hopeless (width, height, density) combination should fail fast rather than block for a long time before eventually failing anyway.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.