From 7d9383ad1ef57db35cac912d15cfec7e258168e7 Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Wed, 16 Oct 2024 10:04:55 -0500 Subject: [PATCH] Add basic main CLI I'm a visual type of person, I like to watch things evolve on the command line. So I've added a basic main.rs to the project to watch things on the command line as they evolve. --- src/board.rs | 24 ++++++++++++++++++++++++ src/cell.rs | 17 +++++++++++++++++ src/lib.rs | 8 ++++++-- src/main.rs | 10 ++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/main.rs diff --git a/src/board.rs b/src/board.rs index c82d2f0..1465e32 100644 --- a/src/board.rs +++ b/src/board.rs @@ -1,14 +1,32 @@ use crate::cell::{Cell, CellValue}; use std::collections::HashSet; +use std::fmt; use ndarray::prelude::*; const REGION_SIZE: usize = 9; const SUB_REGION_SIZE: usize = 3; +#[derive(Debug)] pub struct Board { cells: Array2, } +impl fmt::Display for Board { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let mut str = String::from(""); + for row in self.cells.outer_iter() { + for cell in row.iter() { + match cell.value() { + Some(s) => str += &s.to_string(), + None => str += "?", + } + } + str += "\n"; + } + fmt.write_str(&str) + } +} + impl Board { pub fn new() -> Board { let cells = Array2::zeros((REGION_SIZE, REGION_SIZE)); @@ -65,6 +83,12 @@ pub struct Point { } impl Point { + pub fn new(row: usize, col: usize) -> Point { + Point { + row, col + } + } + fn coordinates(&self) -> [usize; 2] { [self.row, self.col] } diff --git a/src/cell.rs b/src/cell.rs index 33f4439..68b679f 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -1,6 +1,7 @@ use std::collections::HashSet; use num_traits::identities::Zero; use std::ops::Add; +use std::string::ToString; #[derive(Clone, Debug, Copy, Hash, Eq, PartialEq)] pub enum CellValue { @@ -15,6 +16,22 @@ pub enum CellValue { Nine, } +impl ToString for CellValue { + fn to_string(&self) -> String { + match self { + CellValue::One => String::from("1"), + CellValue::Two => String::from("2"), + CellValue::Three => String::from("3"), + CellValue::Four => String::from("4"), + CellValue::Five => String::from("5"), + CellValue::Six => String::from("6"), + CellValue::Seven => String::from("7"), + CellValue::Eight => String::from("8"), + CellValue::Nine => String::from("9") + } + } +} + #[derive(Clone, Debug)] pub struct Cell { value: Option, diff --git a/src/lib.rs b/src/lib.rs index 50b0b5f..299024b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,6 @@ -pub mod cell; -pub mod board; +mod cell; +mod board; + +pub use board::Board; +pub use board::Point; +pub use cell::CellValue; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d388d50 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,10 @@ +use rustdoku::Board; +use rustdoku::CellValue; +use rustdoku::Point; + +fn main() { + let mut board = Board::new(); + + board.set_value(&Point::new(1, 2), CellValue::One); + println!("{}", board); +} \ No newline at end of file