Skip to content

Commit

Permalink
misc: rename board members
Browse files Browse the repository at this point in the history
  • Loading branch information
sgasse committed Oct 19, 2024
1 parent b936aaf commit 9e6fb8c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
40 changes: 20 additions & 20 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
/// 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<u8>,
/// Vector mapping IDs to indices.
/// e.g. indices[2] = 4 -> tile 2 is at index 4 on the board
ids2indices: Vec<usize>,
/// e.g. id2idx[2] = 4 -> field with ID 2 is at index 4 on the board
id2idx: Vec<usize>,
}

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<u8> {
&self.indices2ids
pub(crate) fn fields(&self) -> &Vec<u8> {
&self.fields
}

pub(crate) fn ids2indices(&self) -> &Vec<usize> {
&self.ids2indices
pub(crate) fn id2idx(&self) -> &Vec<usize> {
&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;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/ui/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, &params.bg_url);
board.append_child(&div).unwrap();
}
Expand Down Expand Up @@ -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<usize> {
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)
Expand Down
8 changes: 4 additions & 4 deletions src/ui/buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn get_quick_swap_callback(size: usize) -> Closure<dyn FnMut(MouseEvent)> {
}

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) => {
Expand Down Expand Up @@ -90,7 +90,7 @@ fn get_granular_swap_callback(size: usize) -> Closure<dyn FnMut(MouseEvent)> {

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) => {
Expand Down Expand Up @@ -146,7 +146,7 @@ fn get_optimal_solve_callback(size: usize) -> Closure<dyn FnMut(MouseEvent)> {
}

// 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);
Expand All @@ -165,7 +165,7 @@ fn get_dac_solve_callback(size: usize) -> Closure<dyn FnMut(MouseEvent)> {
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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn get_touch_direction(

fn handle_touch_move(size: usize, direction: TouchMoveDirection) -> Result<bool, Error> {
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);

Expand Down

0 comments on commit 9e6fb8c

Please sign in to comment.