diff --git a/src/board.rs b/src/board.rs index c159567..1cb9cb7 100644 --- a/src/board.rs +++ b/src/board.rs @@ -5,49 +5,49 @@ use crate::Error; #[derive(Debug)] pub(crate) struct Board { - /// Vector mapping indices to IDs. - /// e.g. ids[5] = 6 -> index 5 has tile 6 on the board - indices2ids: Vec, + /// Vector of field IDs in row-major order. + /// e.g. fields[5] = 6 -> index 5 has the field with ID 6 on the board + fields: Vec, /// Vector mapping IDs to indices. - /// e.g. indices[2] = 4 -> tile 2 is at index 4 on the board - ids2indices: Vec, + /// e.g. id2idx[2] = 4 -> field with ID 2 is at index 4 on the board + id2idx: Vec, } impl Board { pub(crate) const fn new() -> Self { Self { - indices2ids: Vec::new(), - ids2indices: Vec::new(), + fields: Vec::new(), + id2idx: Vec::new(), } } pub(crate) fn init(&mut self, size: usize) { let num_elements = size * size; - self.indices2ids = (0..(num_elements as u8)).collect(); - self.ids2indices = (0..num_elements).collect(); + self.fields = (0..(num_elements as u8)).collect(); + self.id2idx = (0..num_elements).collect(); } - pub(crate) fn indices2ids(&self) -> &Vec { - &self.indices2ids + pub(crate) fn fields(&self) -> &Vec { + &self.fields } - pub(crate) fn ids2indices(&self) -> &Vec { - &self.ids2indices + pub(crate) fn id2idx(&self) -> &Vec { + &self.id2idx } pub(crate) fn swap_ids(&mut self, id_a: u8, id_b: u8) { - debug_assert!((id_a as usize) < self.indices2ids.len()); - debug_assert!((id_b as usize) < self.indices2ids.len()); + debug_assert!((id_a as usize) < self.fields.len()); + debug_assert!((id_b as usize) < self.fields.len()); // Swap IDs / indices in maps. // Look up at which index which ID is. // Swap the IDs in both maps. - let idx_a = self.ids2indices[id_a as usize]; - let idx_b = self.ids2indices[id_b as usize]; - self.indices2ids.swap(idx_a, idx_b); + let idx_a = self.id2idx[id_a as usize]; + let idx_b = self.id2idx[id_b as usize]; + self.fields.swap(idx_a, idx_b); - self.ids2indices[id_a as usize] = idx_b; - self.ids2indices[id_b as usize] = idx_a; + self.id2idx[id_a as usize] = idx_b; + self.id2idx[id_b as usize] = idx_a; } } diff --git a/src/ui/board.rs b/src/ui/board.rs index 74c956f..46c0fda 100644 --- a/src/ui/board.rs +++ b/src/ui/board.rs @@ -41,8 +41,8 @@ impl UiBoard { } pub(crate) fn swap_indices(&mut self, idx_a: usize, idx_b: usize) { - let id_a = self.inner.indices2ids()[idx_a]; - let id_b = self.inner.indices2ids()[idx_b]; + let id_a = self.inner.fields()[idx_a]; + let id_b = self.inner.fields()[idx_b]; self.swap_ui_fields(id_a, id_b); self.inner.swap_ids(id_a, id_b); @@ -60,7 +60,7 @@ impl UiBoard { // Adjust field size depending on puzzle size let field_size = 12 / params.size; - for id in self.inner.indices2ids().iter() { + for id in self.inner.fields().iter() { let div = create_div(*id, params.size, field_size, ¶ms.bg_url); board.append_child(&div).unwrap(); } @@ -177,9 +177,9 @@ fn is_empty_field(clicked_id: usize, size: usize) -> bool { fn is_swappable_with_empty(clicked_id: usize, size: usize) -> Option { BOARD.with_borrow(|b| { - let empty_id = b.board().ids2indices().len() - 1; - let clicked_idx = b.board().ids2indices()[clicked_id]; - let empty_idx = b.board().ids2indices()[empty_id]; + let empty_id = b.board().id2idx().len() - 1; + let clicked_idx = b.board().id2idx()[clicked_id]; + let empty_idx = b.board().id2idx()[empty_id]; if is_swappable_neighbour(clicked_idx, empty_idx, size) { Some(empty_id) diff --git a/src/ui/buttons.rs b/src/ui/buttons.rs index 396f155..4ca6091 100644 --- a/src/ui/buttons.rs +++ b/src/ui/buttons.rs @@ -61,7 +61,7 @@ fn get_quick_swap_callback(size: usize) -> Closure { } let empty_field_idx = - BOARD.with_borrow(|b| get_empty_field_idx(b.board().indices2ids()).unwrap()); + BOARD.with_borrow(|b| get_empty_field_idx(b.board().fields()).unwrap()); match get_shuffle_sequence(size, empty_field_idx, 20) { Ok(shuffle_sequence) => { @@ -90,7 +90,7 @@ fn get_granular_swap_callback(size: usize) -> Closure { let num_shuffles = NUM_SHUFFLES; let empty_field_idx = - BOARD.with_borrow(|b| get_empty_field_idx(b.board().indices2ids()).unwrap()); + BOARD.with_borrow(|b| get_empty_field_idx(b.board().fields()).unwrap()); match get_shuffle_sequence(size, empty_field_idx, num_shuffles) { Ok(shuffle_sequence) => { @@ -146,7 +146,7 @@ fn get_optimal_solve_callback(size: usize) -> Closure { } // TODO: Solver aborting after a certain size? - let ids = BOARD.with_borrow(|b| b.board().indices2ids().clone()); + let ids = BOARD.with_borrow(|b| b.board().fields().clone()); match find_swap_order(&ids, size, size) { Ok(solve_sequence) => { apply_solve_sequence(solve_sequence, SWAP_TIMEOUT_SLOW); @@ -165,7 +165,7 @@ fn get_dac_solve_callback(size: usize) -> Closure { return; } - let ids = BOARD.with_borrow(|b| b.board().indices2ids().clone()); + let ids = BOARD.with_borrow(|b| b.board().fields().clone()); match DacPuzzleSolver::new(&ids, size as i32, size as i32) { Ok(mut solver) => match solver.solve_puzzle() { Ok(solve_sequence) => { diff --git a/src/ui/touch.rs b/src/ui/touch.rs index 77a04da..f2d1fff 100644 --- a/src/ui/touch.rs +++ b/src/ui/touch.rs @@ -120,7 +120,7 @@ fn get_touch_direction( fn handle_touch_move(size: usize, direction: TouchMoveDirection) -> Result { let empty_field_id = size * size - 1; - let empty_field_idx = BOARD.with_borrow(|b| b.board().ids2indices()[empty_field_id]); + let empty_field_idx = BOARD.with_borrow(|b| b.board().id2idx()[empty_field_id]); let (empty_row, empty_col): (usize, usize) = get_row_col_from_idx(empty_field_idx, size); let (empty_row, empty_col) = (empty_row as i32, empty_col as i32);