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.
This commit is contained in:
@@ -1,14 +1,32 @@
|
|||||||
use crate::cell::{Cell, CellValue};
|
use crate::cell::{Cell, CellValue};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
use std::fmt;
|
||||||
use ndarray::prelude::*;
|
use ndarray::prelude::*;
|
||||||
|
|
||||||
const REGION_SIZE: usize = 9;
|
const REGION_SIZE: usize = 9;
|
||||||
const SUB_REGION_SIZE: usize = 3;
|
const SUB_REGION_SIZE: usize = 3;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
cells: Array2<Cell>,
|
cells: Array2<Cell>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
impl Board {
|
||||||
pub fn new() -> Board {
|
pub fn new() -> Board {
|
||||||
let cells = Array2::zeros((REGION_SIZE, REGION_SIZE));
|
let cells = Array2::zeros((REGION_SIZE, REGION_SIZE));
|
||||||
@@ -65,6 +83,12 @@ pub struct Point {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Point {
|
impl Point {
|
||||||
|
pub fn new(row: usize, col: usize) -> Point {
|
||||||
|
Point {
|
||||||
|
row, col
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn coordinates(&self) -> [usize; 2] {
|
fn coordinates(&self) -> [usize; 2] {
|
||||||
[self.row, self.col]
|
[self.row, self.col]
|
||||||
}
|
}
|
||||||
|
|||||||
+17
@@ -1,6 +1,7 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use num_traits::identities::Zero;
|
use num_traits::identities::Zero;
|
||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
|
use std::string::ToString;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Copy, Hash, Eq, PartialEq)]
|
#[derive(Clone, Debug, Copy, Hash, Eq, PartialEq)]
|
||||||
pub enum CellValue {
|
pub enum CellValue {
|
||||||
@@ -15,6 +16,22 @@ pub enum CellValue {
|
|||||||
Nine,
|
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)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Cell {
|
pub struct Cell {
|
||||||
value: Option<CellValue>,
|
value: Option<CellValue>,
|
||||||
|
|||||||
+6
-2
@@ -1,2 +1,6 @@
|
|||||||
pub mod cell;
|
mod cell;
|
||||||
pub mod board;
|
mod board;
|
||||||
|
|
||||||
|
pub use board::Board;
|
||||||
|
pub use board::Point;
|
||||||
|
pub use cell::CellValue;
|
||||||
+10
@@ -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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user