Test: Simple row populate test
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(PartialEq, Debug, Clone)]
|
#[derive(PartialEq, Debug, Clone)]
|
||||||
pub struct Playfield {
|
pub struct Playfield {
|
||||||
size: usize,
|
size: usize,
|
||||||
pub fields: Vec<Vec<Field>>,
|
pub fields: Vec<Vec<Field>>,
|
||||||
open_fields: Vec<u32>,
|
pub open_fields: Vec<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Playfield {
|
impl Playfield {
|
||||||
@@ -11,7 +12,7 @@ impl Playfield {
|
|||||||
Self {
|
Self {
|
||||||
size,
|
size,
|
||||||
fields: vec![vec![Field::default(); size]; size],
|
fields: vec![vec![Field::default(); size]; size],
|
||||||
open_fields: Vec::<u32>::new(),
|
open_fields: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn new(field: &String, size: usize) -> Playfield {
|
pub fn new(field: &String, size: usize) -> Playfield {
|
||||||
@@ -34,10 +35,12 @@ impl Playfield {
|
|||||||
|
|
||||||
for row in 0..self.size {
|
for row in 0..self.size {
|
||||||
for col in 0..self.size {
|
for col in 0..self.size {
|
||||||
|
let index = row * self.size + col;
|
||||||
let field = &mut self.fields[row][col];
|
let field = &mut self.fields[row][col];
|
||||||
let char_field = chars.get(row * self.size + col).unwrap();
|
let char_field = chars.get(index).unwrap();
|
||||||
|
|
||||||
if *char_field == "0" {
|
if *char_field == "0" {
|
||||||
|
self.open_fields.push(index);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +78,7 @@ pub struct Field {
|
|||||||
impl Field {
|
impl Field {
|
||||||
fn default() -> Field {
|
fn default() -> Field {
|
||||||
Self {
|
Self {
|
||||||
possible_values: Vec::<u32>::new(),
|
possible_values: Vec::new(),
|
||||||
value: None,
|
value: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,7 +91,7 @@ mod tests {
|
|||||||
impl Field {
|
impl Field {
|
||||||
fn new(value: u32) -> Field {
|
fn new(value: u32) -> Field {
|
||||||
Self {
|
Self {
|
||||||
possible_values: Vec::<u32>::new(),
|
possible_values: Vec::new(),
|
||||||
value: Some(value),
|
value: Some(value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
struct SudokuSolver {
|
|
||||||
playfield: Playfield
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SudokuSolver {
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
|
use crate::playfield::Field;
|
||||||
use crate::playfield::Playfield;
|
use crate::playfield::Playfield;
|
||||||
|
|
||||||
pub struct SudokuSolver {
|
pub struct SudokuSolver {
|
||||||
playfield: Playfield,
|
playfield: Playfield,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SudokuSolver {
|
impl<'a> SudokuSolver {
|
||||||
pub fn new(playfield: &Playfield) -> SudokuSolver {
|
pub fn new(playfield: &Playfield) -> SudokuSolver {
|
||||||
Self {
|
Self {
|
||||||
playfield: playfield.clone(),
|
playfield: playfield.clone(),
|
||||||
@@ -15,27 +16,41 @@ impl SudokuSolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn populate_possible_values(&self) {
|
fn populate_possible_values(&self) {
|
||||||
for row in &self.playfield.fields {
|
for open_field in &self.playfield.open_fields {}
|
||||||
for field in row {
|
|
||||||
if field.value.is_some() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
fn possible_values_from_row(&self, row: Vec<Field>) -> Vec<u32> {}
|
||||||
}
|
}
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::sudoku_solver::SudokuSolver;
|
||||||
|
use crate::Playfield;
|
||||||
|
fn create_test_playfield(input: &str, size: usize) -> Playfield {
|
||||||
|
Playfield::new(&input.to_string(), size)
|
||||||
|
}
|
||||||
mod solve {
|
mod solve {
|
||||||
use super::super::*;
|
use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn simple() {
|
fn simple() {
|
||||||
let playfield = Playfield::new(&"1 2 3 2 3 1 3 1 0".to_string(), 3);
|
let playfield = create_test_playfield("1 2 3 2 3 1 3 1 0", 3);
|
||||||
let expected = Playfield::new(&"1 2 3 2 3 1 3 1 2".to_string(), 3);
|
let expected = create_test_playfield("1 2 3 2 3 1 3 1 2", 3);
|
||||||
|
|
||||||
let solved = SudokuSolver::new(&playfield).solve();
|
let solved = SudokuSolver::new(&playfield).solve();
|
||||||
|
|
||||||
assert_eq!(solved, expected);
|
assert_eq!(solved, expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mod populate_possible_values {
|
||||||
|
mod possible_values_from_row {
|
||||||
|
use super::super::*;
|
||||||
|
#[test]
|
||||||
|
fn simple() {
|
||||||
|
let playfield = create_test_playfield("1 2 0 1 2 0 1 2 0", 3);
|
||||||
|
let expected = vec![3];
|
||||||
|
|
||||||
|
let possible_values =
|
||||||
|
SudokuSolver::new(&playfield).possible_values_from_row(playfield.fields[0]);
|
||||||
|
assert_eq!(possible_values, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user