Skip to content

Commit

Permalink
fix: bug in shuffle sequence on button click
Browse files Browse the repository at this point in the history
The passed empty value index was wrong.
  • Loading branch information
sgasse committed Oct 18, 2024
1 parent e45ad24 commit 98d4328
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "slide-puzzle"
version = "0.3.0"
version = "0.3.1"
edition = "2021"

[lib]
Expand Down
6 changes: 5 additions & 1 deletion src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ where

/// Get the index of a value in a slice.
///
/// This is a convenience wrapper and panics if the value cannot be found.
/// This is a convenience wrapper which should not be used in a hot path.
pub(crate) fn get_idx_of_val(slice: &[u8], value: u8) -> Result<usize, Error> {
slice
.iter()
Expand All @@ -127,6 +127,10 @@ pub(crate) fn initialize_fields(num_elements: usize) -> Vec<u8> {
(0..num_elements).collect()
}

pub(crate) fn get_empty_field_idx(fields: &[u8]) -> Result<usize, Error> {
get_idx_of_val(fields, fields.len() as u8 - 1)
}

/// Get the indices of neighbours that can be swapped with the empty field.
pub(crate) fn get_swappable_neighbours(
width: usize,
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,3 @@ pub fn wasm_main() {

unlock_ui();
}

// TODO: Solver not attempting / button greyed out at a certain size
7 changes: 3 additions & 4 deletions src/solver/divide_and_conquer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use simple_error::bail;

use crate::{
board::{
get_coords_from_idx, get_idx_from_coords, get_idx_of_val, in_bounds, initialize_fields,
Coords,
get_coords_from_idx, get_empty_field_idx, get_idx_from_coords, get_idx_of_val, in_bounds,
initialize_fields, Coords,
},
Error,
};
Expand Down Expand Up @@ -47,8 +47,7 @@ impl DacPuzzleSolver {
bail!("DacPuzzleSolver: Puzzles below 3x3 are not supported");
}

let empty_field_val = (width * height - 1) as u8;
let empty_field_idx = get_idx_of_val(fields, empty_field_val)? as i32;
let empty_field_idx = get_empty_field_idx(fields)? as i32;
let empty_field_pos = get_coords_from_idx(empty_field_idx, width);

Ok(Self {
Expand Down
5 changes: 2 additions & 3 deletions src/solver/optimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use fnv::FnvHasher;
use rustc_hash::FxHashMap;

use crate::{
board::{get_idx_of_val, get_swappable_neighbours, initialize_fields},
board::{get_empty_field_idx, get_swappable_neighbours, initialize_fields},
Error,
};

Expand All @@ -40,8 +40,7 @@ pub fn find_swap_order(
return Ok(Vec::with_capacity(0));
}

let empty_field_id = (width * height - 1) as u8;
let empty_field_idx = get_idx_of_val(&fields, empty_field_id)?;
let empty_field_idx = get_empty_field_idx(&fields)?;

// Map from a state hash to its parent hash and the last swap that led to
// this state from the parent. We need to the swap information to trace back
Expand Down
4 changes: 3 additions & 1 deletion src/ui/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use wasm_bindgen::{prelude::Closure, JsCast as _};
use web_sys::{window, CssStyleDeclaration, Document, MouseEvent, Node};

use crate::{
board::get_row_col_from_idx, board::Board, ui::search_params::Parameters, ui::ui_locked, BOARD,
board::{get_row_col_from_idx, Board},
ui::{search_params::Parameters, ui_locked},
BOARD,
};

pub(crate) struct UiBoard {
Expand Down
8 changes: 5 additions & 3 deletions src/ui/buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use wasm_bindgen::prelude::*;
use web_sys::{window, HtmlElement, MouseEvent};

use crate::{
board::get_shuffle_sequence,
board::{get_empty_field_idx, get_shuffle_sequence},
solver::{divide_and_conquer::DacPuzzleSolver, optimal::find_swap_order},
ui::{
lock_ui,
Expand Down Expand Up @@ -60,7 +60,8 @@ fn get_quick_swap_callback(size: usize) -> Closure<dyn FnMut(MouseEvent)> {
return;
}

let empty_field_idx = size - 1;
let empty_field_idx =
BOARD.with_borrow(|b| get_empty_field_idx(b.board().indices2ids()).unwrap());

match get_shuffle_sequence(size, empty_field_idx, 20) {
Ok(shuffle_sequence) => {
Expand Down Expand Up @@ -88,7 +89,8 @@ fn get_granular_swap_callback(size: usize) -> Closure<dyn FnMut(MouseEvent)> {
}

let num_shuffles = NUM_SHUFFLES;
let empty_field_idx = size - 1;
let empty_field_idx =
BOARD.with_borrow(|b| get_empty_field_idx(b.board().indices2ids()).unwrap());

match get_shuffle_sequence(size, empty_field_idx, num_shuffles) {
Ok(shuffle_sequence) => {
Expand Down
1 change: 1 addition & 0 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ pub(crate) fn ui_locked() -> bool {
}

// TODO: Change button colors when locking UI.
// TODO: Solver not attempting / button greyed out at a certain size

0 comments on commit 98d4328

Please sign in to comment.