Test: First simple solving test
This commit is contained in:
10
src/main.rs
10
src/main.rs
@@ -1,8 +1,10 @@
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
mod playfield;
|
mod playfield;
|
||||||
|
mod sudoku_solver;
|
||||||
|
|
||||||
use playfield::Playfield;
|
use playfield::Playfield;
|
||||||
|
use sudoku_solver::SudokuSolver;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(version, about, long_about = None)]
|
#[command(version, about, long_about = None)]
|
||||||
@@ -11,7 +13,7 @@ struct Cli {
|
|||||||
// absense
|
// absense
|
||||||
field: String,
|
field: String,
|
||||||
|
|
||||||
// Size of one size of the field. For standard 9x9 input 9 (default)
|
// Size of one side of the field. For standard 9x9 input 9 (default)
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
field_size: usize,
|
field_size: usize,
|
||||||
}
|
}
|
||||||
@@ -21,5 +23,9 @@ fn main() {
|
|||||||
|
|
||||||
let playfield = Playfield::new(&cli.field, cli.field_size);
|
let playfield = Playfield::new(&cli.field, cli.field_size);
|
||||||
|
|
||||||
println!("Input:\n{}", playfield)
|
println!("Input:\n{}", playfield);
|
||||||
|
|
||||||
|
let solved_playfield = SudokuSolver::new(&playfield).solve();
|
||||||
|
|
||||||
|
println!("Solved:\n{}", solved_playfield);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug, Clone)]
|
||||||
pub struct Playfield {
|
pub struct Playfield {
|
||||||
size: usize,
|
size: usize,
|
||||||
fields: Vec<Vec<Field>>,
|
pub fields: Vec<Vec<Field>>,
|
||||||
open_fields: Vec<u32>,
|
open_fields: Vec<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,9 +67,9 @@ impl fmt::Display for Playfield {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug)]
|
||||||
struct Field {
|
pub struct Field {
|
||||||
possible_values: Vec<u32>,
|
pub possible_values: Vec<u32>,
|
||||||
value: Option<u32>,
|
pub value: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Field {
|
impl Field {
|
||||||
|
|||||||
7
src/sudoku_solver
Normal file
7
src/sudoku_solver
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
struct SudokuSolver {
|
||||||
|
playfield: Playfield
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SudokuSolver {
|
||||||
|
|
||||||
|
}
|
||||||
41
src/sudoku_solver.rs
Normal file
41
src/sudoku_solver.rs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
use crate::playfield::Playfield;
|
||||||
|
|
||||||
|
pub struct SudokuSolver {
|
||||||
|
playfield: Playfield,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SudokuSolver {
|
||||||
|
pub fn new(playfield: &Playfield) -> SudokuSolver {
|
||||||
|
Self {
|
||||||
|
playfield: playfield.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn solve(&self) -> Playfield {
|
||||||
|
return self.playfield.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn populate_possible_values(&self) {
|
||||||
|
for row in &self.playfield.fields {
|
||||||
|
for field in row {
|
||||||
|
if field.value.is_some() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
mod solve {
|
||||||
|
use super::super::*;
|
||||||
|
#[test]
|
||||||
|
fn simple() {
|
||||||
|
let playfield = Playfield::new(&"1 2 3 2 3 1 3 1 0".to_string(), 3);
|
||||||
|
let expected = Playfield::new(&"1 2 3 2 3 1 3 1 2".to_string(), 3);
|
||||||
|
|
||||||
|
let solved = SudokuSolver::new(&playfield).solve();
|
||||||
|
|
||||||
|
assert_eq!(solved, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user