From 5c96f477651ecc86ea79caf8c0849234312add15 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Sun, 18 Oct 2020 14:55:33 -0700 Subject: [PATCH] Add option to print possibilities in generated PDF puzzles --- src/bin/generator.rs | 10 +++++++++- src/pdf.rs | 23 +++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/bin/generator.rs b/src/bin/generator.rs index 87e1b75..e5aa3d9 100644 --- a/src/bin/generator.rs +++ b/src/bin/generator.rs @@ -102,6 +102,7 @@ fn main() { let mut filename: Option = None; let mut difficulty = Difficulty::Challenge; let mut threads = 1; + let mut print_possibilities = false; { // this block limits scope of borrows by ap.refer() method @@ -137,6 +138,13 @@ fn main() { "Number of threads to use when generating possible puzzles", ); + ap.refer(&mut print_possibilities) + .add_option( + &["-p", "--possibilities"], + argparse::StoreTrue, + "Include each cell's possibilities in the output; applies only to PDF output" + ); + ap.parse_args_or_exit(); } @@ -202,7 +210,7 @@ fn main() { Some(filename) => { // check if we save to a csv or a pdf if filename.ends_with(".pdf") { - sudoku_solver::pdf::draw_grid(&grid, &filename).unwrap(); + sudoku_solver::pdf::draw_grid(&grid, &filename, print_possibilities).unwrap(); println!("Grid saved as pdf to {}", filename); } else { save_grid_csv(&grid, &filename).unwrap(); diff --git a/src/pdf.rs b/src/pdf.rs index 35b72b2..38ec23c 100644 --- a/src/pdf.rs +++ b/src/pdf.rs @@ -9,12 +9,13 @@ const GRID_DIMENSION: f64 = 190.0; const A4: (Mm, Mm) = (Mm(215.0), Mm(279.0)); -pub fn draw_grid(grid: &Grid, filename: &str) -> Result<(), Box> { +pub fn draw_grid(grid: &Grid, filename: &str, print_possibilities: bool) -> Result<(), Box> { let (doc, page1, layer1) = PdfDocument::new("Sudoku Puzzle", A4.0, A4.1, "Layer 1"); let layer = doc.get_page(page1).get_layer(layer1); let font = doc.add_builtin_font(BuiltinFont::HelveticaBold)?; - let font_size = 45; + let fixed_value_font_size = 45; + let possibility_font_size = 12; draw_empty_grid(&layer); @@ -39,9 +40,23 @@ pub fn draw_grid(grid: &Grid, filename: &str) -> Result<(), Box { let text = digit.to_string(); - layer.use_text(text, font_size, x, y, &font); + layer.use_text(text, fixed_value_font_size, x, y, &font); + } + CellValue::Unknown(possibilities) => { + if print_possibilities { + for (_, possibility) in possibilities.iter().enumerate() { + + let sub_row = (possibility - 1) / 3; + let sub_column = (possibility - 1) % 3; + // Need to adjust x & y + let x = Mm(x.0 - 4.0 + (GRID_DIMENSION / 27.0) * (sub_column as f64)); + let y = Mm(y.0 - 9.5 + (GRID_DIMENSION / 27.0) * (3.0 - sub_row as f64)); + + let text = possibility.to_string(); + layer.use_text(text, possibility_font_size, x, y, &font); + } + } } - _ => {} } } }