Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
t4ccer committed Nov 5, 2023
1 parent 2ee46e2 commit c13cf6d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 65 deletions.
7 changes: 4 additions & 3 deletions cgt-py/src/domineering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cgt::{
},
};
use pyo3::prelude::*;
use std::str::FromStr;

crate::wrap_struct!(Domineering, PyDomineering, "Domineering", Clone);
crate::wrap_struct!(
Expand All @@ -19,10 +20,10 @@ crate::wrap_struct!(
impl PyDomineering {
#[new]
fn py_new(position: &str) -> PyResult<Self> {
let grid = SmallBitGrid::parse(position)
.ok_or(PyErr::new::<pyo3::exceptions::PyValueError, _>(
let grid = SmallBitGrid::from_str(position)
.or(Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
"Parse error",
))?;
)))?;
Ok(Self::from(Domineering::new(grid)))
}

Expand Down
9 changes: 5 additions & 4 deletions src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@ pub trait FiniteGrid: Grid + Sized {
Ok(())
}

/// Parse grid from string following notation from [`display`]
/// Parse grid from string following notation from [`Self::display`]
fn parse(input: &str) -> Option<Self>
where
Self: Sized,
Self::Item: CharTile + Default,
{
let width = input.split('|').next()?.len() as u8;
let height = input.chars().filter(|c| *c == '|').count() as u8 + 1;
let row_separator = '|';
let width = input.split(row_separator).next()?.len() as u8;
let height = input.chars().filter(|c| *c == row_separator).count() as u8 + 1;

let mut grid = Self::filled(width, height, Default::default())?;
let mut x = 0;
let mut y = 0;

for chr in input.chars() {
if chr == '|' {
if chr == row_separator {
if x == width {
x = 0;
y += 1;
Expand Down
58 changes: 0 additions & 58 deletions src/grid/small_bit_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,64 +143,6 @@ impl SmallBitGrid {
Self::from_number(width, height, arr_to_bits(grid))
}

// NOTE: I'm not sure if that's a good place, or is it too domineering-specific
/// Parses a grid from `.#` notation.
///
/// # Arguments
///
/// * `input` - `.#` notation with `|` as rows separator
///
/// # Examples
///
/// ```
/// use cgt::grid::small_bit_grid::SmallBitGrid;
/// use std::str::FromStr;
///
/// SmallBitGrid::from_str("..#|.#.|##.").unwrap();
/// ```
///
/// # Errors
/// - Grid has more than 64 tiles
/// - Input is in invalid format
pub fn parse(input: &str) -> Option<Self> {
// number of chars till first '|' or eof is the width
// number of '|' + 1 is the height
let width = input.split('|').next()?.len() as u8;
let height = input.chars().filter(|c| *c == '|').count() as u8 + 1;

let mut grid = Self::empty(width, height)?;
let mut x = 0;
let mut y = 0;

for chr in input.chars() {
if chr == '|' {
if x == width {
x = 0;
y += 1;
continue;
}
// Not a rectangle
return None;
}
grid.set(
x,
y,
match chr {
'.' => false,
'#' => true,
_ => return None,
},
);
x += 1;
}

if x != width {
// Not a rectangle in the last row
return None;
}
Some(grid)
}

/// Rotate grid 90° clockwise
#[must_use]
#[cfg_attr(feature = "cargo-clippy", allow(clippy::missing_panics_doc))]
Expand Down

0 comments on commit c13cf6d

Please sign in to comment.