From 79b20a515515ad3d679f76cd6f112ddefa3e35ec Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Sun, 6 Sep 2020 12:29:42 -0700 Subject: [PATCH] Brought code up to point where it can run and fill in and print a grid. --- src/main.rs | 198 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 121 insertions(+), 77 deletions(-) diff --git a/src/main.rs b/src/main.rs index 449cb89..0e2ff5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,12 @@ -use std::borrow::BorrowMut; +use std::rc::Rc; +use std::cell::RefCell; + +struct Grid { + + rows: Vec>>>, // Read from top to bottom + columns: Vec>>>, + sections: Vec>>> +} enum CellValue { FIXED(u8), @@ -6,33 +14,28 @@ enum CellValue { UNKNOWN(Vec) } -struct Cell(CellValue); +impl Grid { + fn get(&self, r: usize, c: usize) -> Result>, &str> { + if (r > 9) | (c > 9) { + return Err("Row or column indices are out of bounds"); + } -struct Grid<'a> { - rows: Vec<[&'a mut Cell; 9]>, // Read from top to bottom - columns: Vec<[&'a Cell; 9]>, // Left to right - sections: Vec<[&'a Cell; 9]> // left to right, top to bottom -} + let row = match self.rows.get(r) { + Some(x) => x, + None => {return Err("Row index is out of bounds")} + }; -impl<'a> Grid<'a> { - fn get(&self, r: usize, c: usize) -> Option<&Cell> { - let row = self.rows.get(r)?; - let cell = &row[c]; - - return Some(&cell); - } - - fn get_mut(&'a mut self, r: usize, c: usize) -> Option<&'a mut Cell> { - let row : &'a mut [&'a mut Cell; 9] = self.rows.get_mut(r)?; - let cell : &'a mut Cell = row[c].borrow_mut(); - - return Some(cell); + let cell = match row.get(c) { + Some(x) => x, + None => {return Err("Column index is out of bounds")} + }; + return Ok(Rc::clone(cell)); } fn print(&self) { for r in 0..9 { - if (r & 3 == 0) && (r > 0) { + if (r % 3 == 0) && (r > 0) { println!("---+---+---"); } @@ -41,19 +44,72 @@ impl<'a> Grid<'a> { print!("|"); } - let value = &self.get(r, c).unwrap().0; - match value { + let value = self.get(r, c).unwrap_or_else(|err| panic!()); + match *value.borrow() { CellValue::FIXED(x) => print!("{}", x), _ => print!(" ") }; - } print!("\n"); } } + + fn new() -> Grid { + // Rows first; we need to create cells for all of them + let mut rows: Vec>>> = Vec::new(); + for _r in 0..9 { + let mut new_row: Vec>> = Vec::new(); + + for _i in 0..9 { + let empty_cell = initial_empty_cell(); + new_row.push(Rc::new(empty_cell)); + + } + rows.push(new_row); + } + + // Columns next; now we have to retrieve the cells from the different rows + let mut columns : Vec>>> = Vec::new(); + for c in 0..9 { + let mut new_column : Vec>> = Vec::new(); + for r in 0..9{ + new_column.push(Rc::clone(&rows.get(r).unwrap()[c])); + } + columns.push(new_column); + } + + // Sections next; now we have to retrieve the cells from different rows and columns + // We read sections from left to right, top to bottom + let mut sections : Vec>>> = Vec::new(); + for r in 0..3 { + for c in 0..3 { + let mut new_section : Vec>> = Vec::new(); + + for internal_r in 0..3 { + let global_r = 3*r + internal_r; + + for internal_c in 0..3 { + let global_c = 3*c + internal_c; + + new_section.push(Rc::clone(&rows.get(global_r).unwrap()[global_c])); + } + } + + sections.push(new_section); + + } + } + + return Grid { rows, columns, sections }; + } } +fn initial_empty_cell() -> RefCell { + return RefCell::new(CellValue::UNKNOWN(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])); +} + + fn main() { println!("Hello, world!"); let grid = retrieve_grid(); @@ -61,66 +117,52 @@ fn main() { } -fn empty_grid<'a>() -> Grid<'a> { - let mut placeholder_cell : Cell = Cell(CellValue::FIXED(0)); - // Rows first; we need to create cells for all of them - let mut rows : Vec<[&'a mut Cell; 9]> = Vec::new(); - for _r in 0..9 { - let mut new_row : [&'a mut Cell; 9] = [&mut placeholder_cell; 9]; - for i in 0..9{ - new_row[i] = &mut initial_empty_cell(); - } - rows.push(new_row); - } - - // Columns next; now we have to retrieve the cells from the different rows - let mut columns : Vec<[&'a Cell; 9]> = Vec::new(); - for c in 0..9 { - let mut new_column : [&'a Cell; 9] = [&placeholder_cell; 9]; - for r in 0..9{ - new_column[r] = rows.get(r).unwrap()[c]; // TODO - improve performance by using get_unchecked - } - columns.push(new_column); - } - - // Sections next; now we have to retrieve the cells from different rows and columns - // We read sections from left to right, top to bottom - let mut sections : Vec<[&'a Cell; 9]> = Vec::new(); - for r in 0..3 { - for c in 0..3 { - let mut new_section : [&'a Cell; 9] = [&placeholder_cell; 9]; - - for internal_r in 0..3 { - let global_r = 3*r + internal_r; - - for internal_c in 0..3 { - let global_c = 3*c + internal_c; - let index = 3*internal_c + internal_r; - - new_section[index] = rows.get(global_r).unwrap()[global_c]; - } - } - - sections.push(new_section); - - } - } - - return Grid {rows, columns, sections}; -} - -fn initial_empty_cell() -> Cell { - return Cell(CellValue::UNKNOWN(vec![1, 2, 3, 4, 5, 6, 7, 8, 9])); -} /** For now this is a stub with a pre-programmed grid; later I'll add functionality to read a CSV file */ -fn retrieve_grid<'a>() -> Grid<'a> { - let mut grid = empty_grid(); +fn retrieve_grid() -> Grid { + let grid = Grid::new(); + + grid.get(0, 4).unwrap().replace(CellValue::FIXED(8)); + grid.get(0, 5).unwrap().replace(CellValue::FIXED(5)); + grid.get(0, 6).unwrap().replace(CellValue::FIXED(6)); + + grid.get(2, 3).unwrap().replace(CellValue::FIXED(9)); + grid.get(2, 4).unwrap().replace(CellValue::FIXED(4)); + grid.get(2, 5).unwrap().replace(CellValue::FIXED(3)); + grid.get(2, 6).unwrap().replace(CellValue::FIXED(5)); + grid.get(2, 7).unwrap().replace(CellValue::FIXED(7)); + + grid.get(3, 0).unwrap().replace(CellValue::FIXED(8)); + grid.get(3, 2).unwrap().replace(CellValue::FIXED(2)); + grid.get(3, 3).unwrap().replace(CellValue::FIXED(6)); + grid.get(3, 4).unwrap().replace(CellValue::FIXED(7)); + grid.get(3, 5).unwrap().replace(CellValue::FIXED(4)); + grid.get(3, 6).unwrap().replace(CellValue::FIXED(9)); + + grid.get(4, 4).unwrap().replace(CellValue::FIXED(9)); + grid.get(4, 8).unwrap().replace(CellValue::FIXED(5)); + + grid.get(5, 1).unwrap().replace(CellValue::FIXED(6)); + grid.get(5, 6).unwrap().replace(CellValue::FIXED(2)); + + grid.get(6, 1).unwrap().replace(CellValue::FIXED(8)); + grid.get(6, 8).unwrap().replace(CellValue::FIXED(2)); + + grid.get(7, 3).unwrap().replace(CellValue::FIXED(7)); + grid.get(7, 5).unwrap().replace(CellValue::FIXED(6)); + grid.get(7, 7).unwrap().replace(CellValue::FIXED(5)); + grid.get(7, 8).unwrap().replace(CellValue::FIXED(4)); + + grid.get(8, 2).unwrap().replace(CellValue::FIXED(7)); + grid.get(8, 3).unwrap().replace(CellValue::FIXED(4)); + + /* + grid.get_mut(0, 4).unwrap().0 = CellValue::FIXED(8); grid.get_mut(0, 5).unwrap().0 = CellValue::FIXED(5); grid.get_mut(0, 6).unwrap().0 = CellValue::FIXED(6); @@ -154,6 +196,8 @@ fn retrieve_grid<'a>() -> Grid<'a> { grid.get_mut(8, 2).unwrap().0 = CellValue::FIXED(7); grid.get_mut(8, 3).unwrap().0 = CellValue::FIXED(4); + + */ return grid;