Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions cspuz_solver_backend/src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,86 @@ impl Board {
}
}

/// Renders grid edges (for puzzles like Slitherlink, Litherslink, Crosswall).
///
/// This helper function adds edge items to the board based on a GridEdges structure.
/// - Vertical edges are rendered at positions (y * 2 + 1, x * 2)
/// - Horizontal edges are rendered at positions (y * 2, x * 2 + 1)
///
/// # Arguments
/// * `edges` - The grid edges to render (with .vertical and .horizontal fields)
/// * `color` - Color to use for the rendered edges
/// * `true_kind` - ItemKind to use when edge value is true
/// * `false_kind` - ItemKind to use when edge value is false
pub fn add_grid_edges(
&mut self,
edges: &graph::GridEdges<Vec<Vec<Option<bool>>>>,
color: &'static str,
true_kind: ItemKind,
false_kind: ItemKind,
) {
let height = self.height;
let width = self.width;

// Render vertical edges
for y in 0..height {
for x in 0..=width {
if let Some(b) = edges.vertical[y][x] {
self.push(Item {
y: y * 2 + 1,
x: x * 2,
color,
kind: if b { true_kind.clone() } else { false_kind.clone() },
});
}
}
}

// Render horizontal edges
for y in 0..=height {
for x in 0..width {
if let Some(b) = edges.horizontal[y][x] {
self.push(Item {
y: y * 2,
x: x * 2 + 1,
color,
kind: if b { true_kind.clone() } else { false_kind.clone() },
});
}
}
}
}

/// Renders Block/Dot items based on boolean answer grid (for region-based puzzles).
///
/// This helper function adds Block or Dot items to the board for cells that have
/// determined values in the answer grid.
///
/// # Arguments
/// * `is_black` - Answer grid with Option<bool> values
/// * `color` - Color to use for the rendered items
pub fn add_block_dot_answer(
&mut self,
is_black: &Vec<Vec<Option<bool>>>,
color: &'static str,
) {
let height = self.height;
let width = self.width;

for y in 0..height {
for x in 0..width {
if let Some(b) = is_black[y][x] {
self.push(Item::cell(
y,
x,
color,
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
}

pub fn to_json(&self) -> String {
let kind = "grid";
let height = self.height;
Expand Down
15 changes: 2 additions & 13 deletions cspuz_solver_backend/src/puzzle/akichiwake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,8 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {

board.add_borders(&borders, "black");

for y in 0..height {
for x in 0..width {
if let Some(is_black) = &is_black {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
if let Some(is_black) = &is_black {
board.add_block_dot_answer(is_black, "green");
}
let rooms = graph::borders_to_rooms(&borders);
assert_eq!(rooms.len(), clues.len());
Expand Down
15 changes: 2 additions & 13 deletions cspuz_solver_backend/src/puzzle/aqre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,8 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {

board.add_borders(&borders, "black");

for y in 0..height {
for x in 0..width {
if let Some(is_black) = &is_black {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
if let Some(is_black) = &is_black {
board.add_block_dot_answer(is_black, "green");
}
let rooms = graph::borders_to_rooms(&borders);
assert_eq!(rooms.len(), clues.len());
Expand Down
13 changes: 1 addition & 12 deletions cspuz_solver_backend/src/puzzle/chocona.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,7 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
board.add_borders(&borders, "black");

if let Some(is_black) = &is_black {
for y in 0..height {
for x in 0..width {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
board.add_block_dot_answer(is_black, "green");
}
let rooms = graph::borders_to_rooms(&borders);
assert_eq!(rooms.len(), clues.len());
Expand Down
15 changes: 2 additions & 13 deletions cspuz_solver_backend/src/puzzle/cocktail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,8 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {

board.add_borders(&borders, "black");

if let Some(is_black) = is_black {
for y in 0..height {
for x in 0..width {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
if let Some(is_black) = &is_black {
board.add_block_dot_answer(is_black, "green");
}

let rooms = graph::borders_to_rooms(&borders);
Expand Down
27 changes: 2 additions & 25 deletions cspuz_solver_backend/src/puzzle/crosswall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,8 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
}
}

if let Some(is_line) = ans {
for y in 0..height {
for x in 0..=width {
if let Some(b) = is_line.vertical[y][x] {
board.push(Item {
y: y * 2 + 1,
x: x * 2,
color: "green",
kind: if b { ItemKind::Wall } else { ItemKind::Cross },
})
}
}
}
for y in 0..=height {
for x in 0..width {
if let Some(b) = is_line.horizontal[y][x] {
board.push(Item {
y: y * 2,
x: x * 2 + 1,
color: "green",
kind: if b { ItemKind::Wall } else { ItemKind::Cross },
})
}
}
}
if let Some(is_line) = &ans {
board.add_grid_edges(is_line, "green", ItemKind::Wall, ItemKind::Cross);
}

Ok(board)
Expand Down
15 changes: 2 additions & 13 deletions cspuz_solver_backend/src/puzzle/double_lits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,8 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {

board.add_borders(&borders, "black");

if let Some(is_black) = ans {
for y in 0..height {
for x in 0..width {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
if let Some(is_black) = &ans {
board.add_block_dot_answer(is_black, "green");
}

Ok(board)
Expand Down
15 changes: 2 additions & 13 deletions cspuz_solver_backend/src/puzzle/heyawake_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,8 @@ pub fn solve(url: &str, is_ayeheya: bool) -> Result<Board, &'static str> {

board.add_borders(&borders, "black");

for y in 0..height {
for x in 0..width {
if let Some(is_black) = &is_black {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
if let Some(is_black) = &is_black {
board.add_block_dot_answer(is_black, "green");
}
let rooms = graph::borders_to_rooms(&borders);
assert_eq!(rooms.len(), clues.len());
Expand Down
13 changes: 1 addition & 12 deletions cspuz_solver_backend/src/puzzle/inverse_litso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,7 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
board.add_borders(&borders, "black");

if let Some(is_black) = &is_black {
for y in 0..height {
for x in 0..width {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
board.add_block_dot_answer(is_black, "green");
}

Ok(board)
Expand Down
25 changes: 1 addition & 24 deletions cspuz_solver_backend/src/puzzle/litherslink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,7 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
}
}
if let Some(is_line) = &ans {
for y in 0..height {
for x in 0..=width {
if let Some(b) = is_line.vertical[y][x] {
board.push(Item {
y: y * 2 + 1,
x: x * 2,
color: "green",
kind: if b { ItemKind::Wall } else { ItemKind::Cross },
})
}
}
}
for y in 0..=height {
for x in 0..width {
if let Some(b) = is_line.horizontal[y][x] {
board.push(Item {
y: y * 2,
x: x * 2 + 1,
color: "green",
kind: if b { ItemKind::Wall } else { ItemKind::Cross },
})
}
}
}
board.add_grid_edges(is_line, "green", ItemKind::Wall, ItemKind::Cross);
}

Ok(board)
Expand Down
13 changes: 1 addition & 12 deletions cspuz_solver_backend/src/puzzle/lits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,7 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
board.add_borders(&borders, "black");

if let Some(is_black) = &ans {
for y in 0..height {
for x in 0..width {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
board.add_block_dot_answer(is_black, "green");
}

Ok(board)
Expand Down
13 changes: 1 addition & 12 deletions cspuz_solver_backend/src/puzzle/norinori.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,7 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
board.add_borders(&borders, "black");

if let Some(is_black) = &ans {
for y in 0..height {
for x in 0..width {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
board.add_block_dot_answer(is_black, "green");
}

Ok(board)
Expand Down
13 changes: 1 addition & 12 deletions cspuz_solver_backend/src/puzzle/nuritwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,7 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
board.add_borders(&borders, "black");

if let Some(is_black) = &ans {
for y in 0..height {
for x in 0..width {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
board.add_block_dot_answer(is_black, "green");
}
let rooms = graph::borders_to_rooms(&borders);
assert_eq!(rooms.len(), clues.len());
Expand Down
13 changes: 1 addition & 12 deletions cspuz_solver_backend/src/puzzle/shimaguni.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,7 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
board.add_borders(&borders, "black");

if let Some(is_black) = &is_black {
for y in 0..height {
for x in 0..width {
if let Some(b) = is_black[y][x] {
board.push(Item::cell(
y,
x,
"green",
if b { ItemKind::Block } else { ItemKind::Dot },
));
}
}
}
board.add_block_dot_answer(is_black, "green");
}
let rooms = graph::borders_to_rooms(&borders);
assert_eq!(rooms.len(), clues.len());
Expand Down
Loading