From 9759d64925331faaab6ecfe595ae9d402e199cba Mon Sep 17 00:00:00 2001 From: afterlifepro Date: Sun, 2 Aug 2026 15:39:12 +0100 Subject: [PATCH] refactor pos type --- sudoku-rust/src/grid/reduce.rs | 2 +- sudoku-rust/src/grid/validate.rs | 32 ++---------------- sudoku-rust/src/types/grid.rs | 2 +- sudoku-rust/src/types/mod.rs | 42 +---------------------- sudoku-rust/src/types/pos.rs | 58 ++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 73 deletions(-) create mode 100644 sudoku-rust/src/types/pos.rs diff --git a/sudoku-rust/src/grid/reduce.rs b/sudoku-rust/src/grid/reduce.rs index 2cc0e17..9e816ee 100644 --- a/sudoku-rust/src/grid/reduce.rs +++ b/sudoku-rust/src/grid/reduce.rs @@ -6,13 +6,13 @@ use wasm_bindgen::prelude::*; use crate::{ grid::validate::{Validate, ValidationError}, types::{ - Pos, cell::{ Note, PlayingCell::{self}, SudokuCell, }, grid::PlayingGrid, + pos::Pos, }, }; diff --git a/sudoku-rust/src/grid/validate.rs b/sudoku-rust/src/grid/validate.rs index 7817811..1c4bd97 100644 --- a/sudoku-rust/src/grid/validate.rs +++ b/sudoku-rust/src/grid/validate.rs @@ -76,21 +76,7 @@ impl Validate for PlayingGrid { } fn get_box(&self, pos: Pos) -> [Self::Cell; GRID_SIZE] { - let box_pos = pos.box_pos(); - - let box_pos = [ - Pos::new(box_pos.x, box_pos.y), - Pos::new(box_pos.x + 1, box_pos.y), - Pos::new(box_pos.x + 2, box_pos.y), - Pos::new(box_pos.x, box_pos.y + 1), - Pos::new(box_pos.x + 1, box_pos.y + 1), - Pos::new(box_pos.x + 2, box_pos.y + 1), - Pos::new(box_pos.x, box_pos.y + 2), - Pos::new(box_pos.x + 1, box_pos.y + 2), - Pos::new(box_pos.x + 2, box_pos.y + 2), - ]; - - box_pos.map(|pos| self[pos]) + pos.all_box_pos().map(|pos| self[pos]) } } @@ -113,20 +99,6 @@ where } fn get_box(&self, pos: Pos) -> [Self::Cell; GRID_SIZE] { - let box_pos = Pos::new((pos.x / 3) * 3, (pos.y / 3) * 3); - - let box_pos = [ - Pos::new(box_pos.x, box_pos.y), - Pos::new(box_pos.x + 1, box_pos.y), - Pos::new(box_pos.x + 2, box_pos.y), - Pos::new(box_pos.x, box_pos.y + 1), - Pos::new(box_pos.x + 1, box_pos.y + 1), - Pos::new(box_pos.x + 2, box_pos.y + 1), - Pos::new(box_pos.x, box_pos.y + 2), - Pos::new(box_pos.x + 1, box_pos.y + 2), - Pos::new(box_pos.x + 2, box_pos.y + 2), - ]; - - box_pos.map(|pos| self[pos]) + pos.all_box_pos().map(|pos| self[pos]) } } diff --git a/sudoku-rust/src/types/grid.rs b/sudoku-rust/src/types/grid.rs index ab822c2..e2cbf88 100644 --- a/sudoku-rust/src/types/grid.rs +++ b/sudoku-rust/src/types/grid.rs @@ -7,7 +7,7 @@ use std::{ use crate::{ GRID_SIZE, grid::validate::{Validate, ValidationError}, - types::{Pos, cell::*}, + types::{cell::*, pos::Pos}, }; use serde::{Deserialize, Serialize}; diff --git a/sudoku-rust/src/types/mod.rs b/sudoku-rust/src/types/mod.rs index 7758f6d..4adc561 100644 --- a/sudoku-rust/src/types/mod.rs +++ b/sudoku-rust/src/types/mod.rs @@ -1,43 +1,3 @@ -use std::fmt::Display; - -use serde::{Deserialize, Serialize}; -use tsify::Tsify; - -use crate::GRID_SIZE; - pub mod cell; pub mod grid; - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Tsify, Serialize, Deserialize)] -#[tsify(into_wasm_abi, from_wasm_abi)] -pub struct Pos { - pub x: usize, - pub y: usize, -} - -impl Pos { - pub fn new(x: usize, y: usize) -> Self { - Self { x, y } - } - - pub fn new_from_idx(idx: usize) -> Self { - Self { - x: idx % GRID_SIZE, - y: idx / GRID_SIZE, - } - } - - pub fn as_idx(&self) -> usize { - self.y * GRID_SIZE + self.x - } - - pub fn box_pos(&self) -> Self { - Pos::new((self.x / 3) * 3, (self.y / 3) * 3) - } -} - -impl Display for Pos { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "({}, {})", self.x, self.y) - } -} +pub mod pos; diff --git a/sudoku-rust/src/types/pos.rs b/sudoku-rust/src/types/pos.rs new file mode 100644 index 0000000..bb8228a --- /dev/null +++ b/sudoku-rust/src/types/pos.rs @@ -0,0 +1,58 @@ +use std::fmt::Display; + +use serde::{Deserialize, Serialize}; +use tsify::Tsify; + +use wasm_bindgen::prelude::*; + +use crate::GRID_SIZE; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Tsify, Serialize, Deserialize)] +#[tsify(into_wasm_abi, from_wasm_abi)] +pub struct Pos { + pub x: usize, + pub y: usize, +} + +impl Pos { + pub fn new(x: usize, y: usize) -> Self { + Self { x, y } + } + + pub fn new_from_idx(idx: usize) -> Self { + Self { + x: idx % GRID_SIZE, + y: idx / GRID_SIZE, + } + } + + pub fn as_idx(&self) -> usize { + self.y * GRID_SIZE + self.x + } + + pub fn box_pos(&self) -> Self { + Pos::new((self.x / 3) * 3, (self.y / 3) * 3) + } + + pub fn all_box_pos(&self) -> [Self; 9] { + let box_pos = self.box_pos(); + + [ + Self::new(box_pos.x, box_pos.y), + Self::new(box_pos.x + 1, box_pos.y), + Self::new(box_pos.x + 2, box_pos.y), + Self::new(box_pos.x, box_pos.y + 1), + Self::new(box_pos.x + 1, box_pos.y + 1), + Self::new(box_pos.x + 2, box_pos.y + 1), + Self::new(box_pos.x, box_pos.y + 2), + Self::new(box_pos.x + 1, box_pos.y + 2), + Self::new(box_pos.x + 2, box_pos.y + 2), + ] + } +} + +impl Display for Pos { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "({}, {})", self.x, self.y) + } +} -- 2.51.2