Skip to content

Commit

Permalink
fix: bug with initial versus current sudoku mutable cells
Browse files Browse the repository at this point in the history
  • Loading branch information
storopoli committed Jan 22, 2024
1 parent 02b9120 commit 7ec8131
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sudoku-dioxus"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Jose Storopoli <[email protected]>"]
description = "Sudoku PWA with Dioxus"
Expand All @@ -11,7 +11,7 @@ readme = "README.md"
dioxus = "0.4"
dioxus-web = "0.4"
sudoku = "0.8"
# WebAssembly Debug
# WebAssembly debug
wasm-logger = "0.2"
console_error_panic_hook = "0.1"

Expand Down
11 changes: 9 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use dioxus::prelude::*;

use crate::components::board::{SudokuBoard, SudokuPuzzle};
use crate::components::board::{InitialSudokuPuzzle, SudokuBoard, SudokuPuzzle};

/// This function sets up the main environment for
/// the Sudoku game in a web browser, initializes the necessary state,
Expand All @@ -19,7 +19,14 @@ use crate::components::board::{SudokuBoard, SudokuPuzzle};
/// orchestrating the entire Sudoku game and its user interface.
#[must_use]
pub fn App(cx: Scope) -> Element {
use_shared_state_provider(cx, SudokuPuzzle::new);
use_shared_state_provider(cx, InitialSudokuPuzzle::new);
let initial_sudoku = use_shared_state::<InitialSudokuPuzzle>(cx)
.expect("failed to get sudoku puzzle shared state")
.read()
.0;
use_shared_state_provider(cx, || SudokuPuzzle {
0: initial_sudoku.clone(),
});
cx.render(rsx!(
h1 {
class: "input",
Expand Down
21 changes: 14 additions & 7 deletions src/components/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,22 @@ pub struct Related(pub Vec<u8>);
pub struct Conflicting(pub Vec<u8>);

/// Shared State for the initial [`SudokuBoard`] puzzle
pub struct SudokuPuzzle(pub [u8; 81]);
impl SudokuPuzzle {
pub struct InitialSudokuPuzzle(pub [u8; 81]);
impl InitialSudokuPuzzle {
#[must_use]
pub fn new() -> Self {
Self(create_sudoku())
}
}
impl Default for SudokuPuzzle {
impl Default for InitialSudokuPuzzle {
fn default() -> Self {
Self::new()
}
}

/// Shared State for the current [`SudokuBoard`] puzzle
pub struct SudokuPuzzle(pub [u8; 81]);

/// Component Props for [`NumberButton`]
///
/// - `number: u8`: the value to be rendered in the button and also the value
Expand Down Expand Up @@ -124,8 +127,8 @@ fn NumberButton(cx: Scope<NumberButtonProps>) -> Element {
/// fresh new puzzle for the user.
fn NewButton(cx: Scope) -> Element {
// Unpack shared states
let sudoku =
use_shared_state::<SudokuPuzzle>(cx).expect("failed to get sudoku puzzle shared state");
let initial_sudoku = use_shared_state::<InitialSudokuPuzzle>(cx)
.expect("failed to get sudoku puzzle shared state");
let clicked = use_shared_state::<Clicked>(cx).expect("failed to get clicked cell shared state");
let mutable = use_shared_state::<Mutable>(cx)
.expect("failed to get clicked cell mutability shared state");
Expand All @@ -136,7 +139,7 @@ fn NewButton(cx: Scope) -> Element {
class: "input icon new",
onclick: move |_| {
// resetting the board with a new puzzle
sudoku.write().0 = create_sudoku();
initial_sudoku.write().0 = create_sudoku();
// resetting the clicked cell
clicked.write().0 = 90;
// resetting the mutable cell
Expand Down Expand Up @@ -164,6 +167,10 @@ fn NewButton(cx: Scope) -> Element {
#[must_use]
pub fn SudokuBoard(cx: Scope) -> Element {
// Unpack shared states
let initial_sudoku = use_shared_state::<InitialSudokuPuzzle>(cx)
.expect("failed to get sudoku puzzle shared state")
.read()
.0;
let sudoku = use_shared_state::<SudokuPuzzle>(cx)
.expect("failed to get sudoku puzzle shared state")
.read()
Expand All @@ -189,7 +196,7 @@ pub fn SudokuBoard(cx: Scope) -> Element {
selected: clicked.expect("failed to get clicked shared state").read().0 == u8::try_from(index).expect("cannot convert from u8"),
highlighted: false,
class: get_class(u8::try_from(index).expect("cannot convert from u8")),
mutable: sudoku[index] == 0,
mutable: initial_sudoku[index] == 0,
})
}

Expand Down

0 comments on commit 7ec8131

Please sign in to comment.