Add option to print possibilities in generated PDF puzzles
This commit is contained in:
parent
8dc7954f53
commit
5c96f47765
2 changed files with 28 additions and 5 deletions
|
@ -102,6 +102,7 @@ fn main() {
|
||||||
let mut filename: Option<String> = None;
|
let mut filename: Option<String> = None;
|
||||||
let mut difficulty = Difficulty::Challenge;
|
let mut difficulty = Difficulty::Challenge;
|
||||||
let mut threads = 1;
|
let mut threads = 1;
|
||||||
|
let mut print_possibilities = false;
|
||||||
|
|
||||||
{
|
{
|
||||||
// this block limits scope of borrows by ap.refer() method
|
// 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",
|
"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();
|
ap.parse_args_or_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,7 +210,7 @@ fn main() {
|
||||||
Some(filename) => {
|
Some(filename) => {
|
||||||
// check if we save to a csv or a pdf
|
// check if we save to a csv or a pdf
|
||||||
if filename.ends_with(".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);
|
println!("Grid saved as pdf to {}", filename);
|
||||||
} else {
|
} else {
|
||||||
save_grid_csv(&grid, &filename).unwrap();
|
save_grid_csv(&grid, &filename).unwrap();
|
||||||
|
|
23
src/pdf.rs
23
src/pdf.rs
|
@ -9,12 +9,13 @@ const GRID_DIMENSION: f64 = 190.0;
|
||||||
|
|
||||||
const A4: (Mm, Mm) = (Mm(215.0), Mm(279.0));
|
const A4: (Mm, Mm) = (Mm(215.0), Mm(279.0));
|
||||||
|
|
||||||
pub fn draw_grid(grid: &Grid, filename: &str) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn draw_grid(grid: &Grid, filename: &str, print_possibilities: bool) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let (doc, page1, layer1) = PdfDocument::new("Sudoku Puzzle", A4.0, A4.1, "Layer 1");
|
let (doc, page1, layer1) = PdfDocument::new("Sudoku Puzzle", A4.0, A4.1, "Layer 1");
|
||||||
let layer = doc.get_page(page1).get_layer(layer1);
|
let layer = doc.get_page(page1).get_layer(layer1);
|
||||||
|
|
||||||
let font = doc.add_builtin_font(BuiltinFont::HelveticaBold)?;
|
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);
|
draw_empty_grid(&layer);
|
||||||
|
|
||||||
|
@ -39,9 +40,23 @@ pub fn draw_grid(grid: &Grid, filename: &str) -> Result<(), Box<dyn std::error::
|
||||||
match value {
|
match value {
|
||||||
CellValue::Fixed(digit) => {
|
CellValue::Fixed(digit) => {
|
||||||
let text = digit.to_string();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue