Skip to content

Commit 7a18cd3

Browse files
authored
Add Board::add_borders_as_answer helper to eliminate duplicated border rendering (#187)
1 parent e1aa613 commit 7a18cd3

12 files changed

Lines changed: 83 additions & 660 deletions

File tree

cspuz_solver_backend/src/board.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,74 @@ impl Board {
311311
}
312312
}
313313

314+
pub fn add_borders_as_answer(
315+
&mut self,
316+
borders: Option<&graph::BoolInnerGridEdgesIrrefutableFacts>,
317+
) {
318+
let height = self.height;
319+
let width = self.width;
320+
for y in 0..height {
321+
for x in 0..width {
322+
if y < height - 1 {
323+
let mut need_default_edge = true;
324+
if let Some(ref border) = borders {
325+
if let Some(b) = border.horizontal[y][x] {
326+
self.push(Item {
327+
y: y * 2 + 2,
328+
x: x * 2 + 1,
329+
color: "green",
330+
kind: if b {
331+
ItemKind::BoldWall
332+
} else {
333+
ItemKind::Cross
334+
},
335+
});
336+
if b {
337+
need_default_edge = false;
338+
}
339+
}
340+
}
341+
if need_default_edge {
342+
self.push(Item {
343+
y: y * 2 + 2,
344+
x: x * 2 + 1,
345+
color: "#cccccc",
346+
kind: ItemKind::Wall,
347+
});
348+
}
349+
}
350+
if x < width - 1 {
351+
let mut need_default_edge = true;
352+
if let Some(ref border) = borders {
353+
if let Some(b) = border.vertical[y][x] {
354+
self.push(Item {
355+
y: y * 2 + 1,
356+
x: x * 2 + 2,
357+
color: "green",
358+
kind: if b {
359+
ItemKind::BoldWall
360+
} else {
361+
ItemKind::Cross
362+
},
363+
});
364+
if b {
365+
need_default_edge = false;
366+
}
367+
}
368+
}
369+
if need_default_edge {
370+
self.push(Item {
371+
y: y * 2 + 1,
372+
x: x * 2 + 2,
373+
color: "#cccccc",
374+
kind: ItemKind::Wall,
375+
});
376+
}
377+
}
378+
}
379+
}
380+
}
381+
314382
pub fn add_lines_irrefutable_facts(
315383
&mut self,
316384
lines: &graph::BoolGridEdgesIrrefutableFacts,

cspuz_solver_backend/src/puzzle/araf.rs

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -26,66 +26,8 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
2626
}
2727
}
2828
}
29-
for y in 0..height {
30-
for x in 0..width {
31-
if y < height - 1 {
32-
let mut need_default_edge = true;
33-
if let Some(ans) = &ans {
34-
if let Some(b) = ans.horizontal[y][x] {
35-
board.push(Item {
36-
y: y * 2 + 2,
37-
x: x * 2 + 1,
38-
color: "green",
39-
kind: if b {
40-
ItemKind::BoldWall
41-
} else {
42-
ItemKind::Cross
43-
},
44-
});
45-
if b {
46-
need_default_edge = false;
47-
}
48-
}
49-
}
50-
if need_default_edge {
51-
board.push(Item {
52-
y: y * 2 + 2,
53-
x: x * 2 + 1,
54-
color: "#cccccc",
55-
kind: ItemKind::Wall,
56-
});
57-
}
58-
}
59-
if x < width - 1 {
60-
let mut need_default_edge = true;
61-
if let Some(ans) = &ans {
62-
if let Some(b) = ans.vertical[y][x] {
63-
board.push(Item {
64-
y: y * 2 + 1,
65-
x: x * 2 + 2,
66-
color: "green",
67-
kind: if b {
68-
ItemKind::BoldWall
69-
} else {
70-
ItemKind::Cross
71-
},
72-
});
73-
if b {
74-
need_default_edge = false;
75-
}
76-
}
77-
}
78-
if need_default_edge {
79-
board.push(Item {
80-
y: y * 2 + 1,
81-
x: x * 2 + 2,
82-
color: "#cccccc",
83-
kind: ItemKind::Wall,
84-
});
85-
}
86-
}
87-
}
88-
}
29+
30+
board.add_borders_as_answer(ans.as_ref());
8931

9032
Ok(board)
9133
}

cspuz_solver_backend/src/puzzle/compass.rs

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -32,66 +32,7 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
3232
}
3333
}
3434

35-
for y in 0..height {
36-
for x in 0..width {
37-
if y < height - 1 {
38-
let mut need_default_edge = true;
39-
if let Some(ref ans) = ans {
40-
if let Some(b) = ans.horizontal[y][x] {
41-
board.push(Item {
42-
y: y * 2 + 2,
43-
x: x * 2 + 1,
44-
color: "green",
45-
kind: if b {
46-
ItemKind::BoldWall
47-
} else {
48-
ItemKind::Cross
49-
},
50-
});
51-
if b {
52-
need_default_edge = false;
53-
}
54-
}
55-
}
56-
if need_default_edge {
57-
board.push(Item {
58-
y: y * 2 + 2,
59-
x: x * 2 + 1,
60-
color: "#cccccc",
61-
kind: ItemKind::Wall,
62-
});
63-
}
64-
}
65-
if x < width - 1 {
66-
let mut need_default_edge = true;
67-
if let Some(ref ans) = ans {
68-
if let Some(b) = ans.vertical[y][x] {
69-
board.push(Item {
70-
y: y * 2 + 1,
71-
x: x * 2 + 2,
72-
color: "green",
73-
kind: if b {
74-
ItemKind::BoldWall
75-
} else {
76-
ItemKind::Cross
77-
},
78-
});
79-
if b {
80-
need_default_edge = false;
81-
}
82-
}
83-
}
84-
if need_default_edge {
85-
board.push(Item {
86-
y: y * 2 + 1,
87-
x: x * 2 + 2,
88-
color: "#cccccc",
89-
kind: ItemKind::Wall,
90-
});
91-
}
92-
}
93-
}
94-
}
35+
board.add_borders_as_answer(ans.as_ref());
9536

9637
Ok(board)
9738
}

cspuz_solver_backend/src/puzzle/dbchoco.rs

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -30,66 +30,7 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
3030
}
3131
}
3232

33-
for y in 0..height {
34-
for x in 0..width {
35-
if y < height - 1 {
36-
let mut need_default_edge = true;
37-
if let Some(border) = &ans {
38-
if let Some(b) = border.horizontal[y][x] {
39-
board.push(Item {
40-
y: y * 2 + 2,
41-
x: x * 2 + 1,
42-
color: "green",
43-
kind: if b {
44-
ItemKind::BoldWall
45-
} else {
46-
ItemKind::Cross
47-
},
48-
});
49-
if b {
50-
need_default_edge = false;
51-
}
52-
}
53-
}
54-
if need_default_edge {
55-
board.push(Item {
56-
y: y * 2 + 2,
57-
x: x * 2 + 1,
58-
color: "#cccccc",
59-
kind: ItemKind::Wall,
60-
});
61-
}
62-
}
63-
if x < width - 1 {
64-
let mut need_default_edge = true;
65-
if let Some(border) = &ans {
66-
if let Some(b) = border.vertical[y][x] {
67-
board.push(Item {
68-
y: y * 2 + 1,
69-
x: x * 2 + 2,
70-
color: "green",
71-
kind: if b {
72-
ItemKind::BoldWall
73-
} else {
74-
ItemKind::Cross
75-
},
76-
});
77-
if b {
78-
need_default_edge = false;
79-
}
80-
}
81-
}
82-
if need_default_edge {
83-
board.push(Item {
84-
y: y * 2 + 1,
85-
x: x * 2 + 2,
86-
color: "#cccccc",
87-
kind: ItemKind::Wall,
88-
});
89-
}
90-
}
91-
}
92-
}
33+
board.add_borders_as_answer(ans.as_ref());
9334

9435
Ok(board)
9536
}

cspuz_solver_backend/src/puzzle/fillomino.rs

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -33,66 +33,7 @@ pub fn solve(url: &str) -> Result<Board, &'static str> {
3333
}
3434
}
3535

36-
for y in 0..height {
37-
for x in 0..width {
38-
if y < height - 1 {
39-
let mut need_default_edge = true;
40-
if let Some((_, border)) = &ans {
41-
if let Some(b) = border.horizontal[y][x] {
42-
board.push(Item {
43-
y: y * 2 + 2,
44-
x: x * 2 + 1,
45-
color: "green",
46-
kind: if b {
47-
ItemKind::BoldWall
48-
} else {
49-
ItemKind::Cross
50-
},
51-
});
52-
if b {
53-
need_default_edge = false;
54-
}
55-
}
56-
}
57-
if need_default_edge {
58-
board.push(Item {
59-
y: y * 2 + 2,
60-
x: x * 2 + 1,
61-
color: "#cccccc",
62-
kind: ItemKind::Wall,
63-
});
64-
}
65-
}
66-
if x < width - 1 {
67-
let mut need_default_edge = true;
68-
if let Some((_, border)) = &ans {
69-
if let Some(b) = border.vertical[y][x] {
70-
board.push(Item {
71-
y: y * 2 + 1,
72-
x: x * 2 + 2,
73-
color: "green",
74-
kind: if b {
75-
ItemKind::BoldWall
76-
} else {
77-
ItemKind::Cross
78-
},
79-
});
80-
if b {
81-
need_default_edge = false;
82-
}
83-
}
84-
}
85-
if need_default_edge {
86-
board.push(Item {
87-
y: y * 2 + 1,
88-
x: x * 2 + 2,
89-
color: "#cccccc",
90-
kind: ItemKind::Wall,
91-
});
92-
}
93-
}
94-
}
95-
}
36+
board.add_borders_as_answer(ans.as_ref().map(|(_, border)| border));
9637

9738
Ok(board)
9839
}

0 commit comments

Comments
 (0)