Skip to content

Commit 1b0bcfa

Browse files
committed
Add some preload examples.
1 parent d6ff00b commit 1b0bcfa

File tree

6 files changed

+108
-20
lines changed

6 files changed

+108
-20
lines changed

assets/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,10 @@
148148
.piece-toggle.selected {
149149
border: 1px solid rgba(34, 34, 34, 0.6);
150150
}
151+
152+
.preset-buttons {
153+
display: flex;
154+
flex-wrap: wrap;
155+
gap: 8px;
156+
margin: 8px 0 16px;
157+
}

src/board.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ impl Board {
6060

6161
pub fn single_is_king(&self) -> bool {
6262
let mut it = self.pieces();
63-
let Some((_, _, p)) = it.next() else { return false; };
63+
let Some((_, _, p)) = it.next() else {
64+
return false;
65+
};
6466
it.next().is_none() && p.is_king()
6567
}
6668
}

src/main.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ fn App() -> Element {
6262
document::Link { rel: "stylesheet", href: STYLE }
6363
main {
6464
h1 { "Solo-Chess Solver" }
65+
section {
66+
div {
67+
class: "preset-buttons",
68+
for setup in EXAMPLE_SETUPS {
69+
button {
70+
key: "{setup.name}",
71+
class: "preset-button",
72+
r#type: "button",
73+
onclick: {
74+
let board_state = board_state;
75+
let selected_step = selected_step;
76+
let pieces = setup.pieces;
77+
move |_| apply_preset(board_state, selected_step, pieces)
78+
},
79+
"{setup.name}"
80+
}
81+
}
82+
}
83+
}
6584
div {
6685
class: "board-stack",
6786
Chessboard {
@@ -88,3 +107,58 @@ fn App() -> Element {
88107
}
89108
}
90109
}
110+
111+
struct ExampleSetup {
112+
name: &'static str,
113+
pieces: &'static [(usize, usize, PieceType)],
114+
}
115+
116+
const EXAMPLE_SETUPS: &[ExampleSetup] = &[
117+
ExampleSetup {
118+
name: "Clear",
119+
pieces: &[],
120+
},
121+
ExampleSetup {
122+
name: "Easy Example",
123+
pieces: &[
124+
(0, 5, PieceType::Rook),
125+
(2, 4, PieceType::Bishop),
126+
(3, 2, PieceType::Rook),
127+
(3, 5, PieceType::Queen),
128+
],
129+
},
130+
ExampleSetup {
131+
name: "Hard Example",
132+
pieces: &[
133+
(0, 1, PieceType::Rook),
134+
(0, 3, PieceType::Knight),
135+
(1, 0, PieceType::King),
136+
(1, 1, PieceType::Rook),
137+
(2, 2, PieceType::Knight),
138+
(2, 3, PieceType::Knight),
139+
(3, 0, PieceType::Bishop),
140+
(4, 0, PieceType::Knight),
141+
(1, 6, PieceType::Queen),
142+
(7, 1, PieceType::Rook),
143+
(3, 2, PieceType::Knight),
144+
],
145+
},
146+
];
147+
148+
fn apply_preset(
149+
mut board_state: Signal<Board>,
150+
mut selected_step: Signal<Option<usize>>,
151+
pieces: &[(usize, usize, PieceType)],
152+
) {
153+
board_state.with_mut(|b| {
154+
for r in 0..8 {
155+
for c in 0..8 {
156+
b.clear_cell(r, c);
157+
}
158+
}
159+
for &(r, c, piece_type) in pieces {
160+
b.set_cell(r, c, Piece::new(piece_type));
161+
}
162+
});
163+
selected_step.set(None);
164+
}

src/solver.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ pub fn solo_chess_solver(board: &mut Board) -> Vec<Step> {
4242
let p = board.count_pieces();
4343

4444
if p <= 1 {
45-
return if king_required { board.single_is_king() } else { p == 1 };
45+
return if king_required {
46+
board.single_is_king()
47+
} else {
48+
p == 1
49+
};
4650
}
4751

4852
if board.sum_move_left() < p - 1 {
@@ -127,7 +131,7 @@ fn get_capturable_cells_into(
127131
c: usize,
128132
piece_type: PieceType,
129133
move_rules: &[(i32, i32)],
130-
out: &mut Vec<Step>
134+
out: &mut Vec<Step>,
131135
) {
132136
let is_king = piece_type == PieceType::King;
133137
let is_pawn = piece_type == PieceType::Pawn;
@@ -310,7 +314,7 @@ mod test_utilities {
310314
list_capture_pairs_into(&board, &mut capture_pairs);
311315
assert!(capture_pairs.is_empty());
312316

313-
// The check on that direction should stop if met a King.
317+
// The check on that direction should stop if met a King.
314318
board.set_cell(5, 0, Piece::new(PieceType::Pawn));
315319
board.set_cell(5, 2, Piece::new(PieceType::King));
316320
list_capture_pairs_into(&board, &mut capture_pairs);
@@ -401,4 +405,4 @@ mod test_utilities {
401405
}
402406
assert_eq!(ca, cb);
403407
}
404-
}
408+
}

src/ui/solution.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,21 @@ pub fn Solution(steps: Vec<Step>, selected_step: Signal<Option<usize>>) -> Eleme
2828
fn to_sans(steps: &[Step]) -> Vec<String> {
2929
let mut sans_step = vec![];
3030

31-
let numeric_to_sans = |r: usize, c: usize| -> String {
32-
format!("{}{}", (c as u8 + b'a') as char, 8 - r)
33-
};
31+
let numeric_to_sans =
32+
|r: usize, c: usize| -> String { format!("{}{}", (c as u8 + b'a') as char, 8 - r) };
3433

35-
for Step { from: (fr, fc), to: (tr, tc), piece_type } in steps {
36-
let tmp = format!("{}{}x{}", piece_type.to_string(), numeric_to_sans(*fr, *fc), numeric_to_sans(*tr, *tc));
34+
for Step {
35+
from: (fr, fc),
36+
to: (tr, tc),
37+
piece_type,
38+
} in steps
39+
{
40+
let tmp = format!(
41+
"{}{}x{}",
42+
piece_type.to_string(),
43+
numeric_to_sans(*fr, *fc),
44+
numeric_to_sans(*tr, *tc)
45+
);
3746
sans_step.push(tmp);
3847
}
3948
sans_step

src/ui/step_colors.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
11
pub const STEP_COLORS: [&str; 10] = [
2-
"#e6194b",
3-
"#f58231",
4-
"#ffe119",
5-
"#3cb44b",
6-
"#42d4f4",
7-
"#4363d8",
8-
"#911eb4",
9-
"#f032e6",
10-
"#bfef45",
11-
"#fabebe",
2+
"#e6194b", "#f58231", "#ffe119", "#3cb44b", "#42d4f4", "#4363d8", "#911eb4", "#f032e6",
3+
"#bfef45", "#fabebe",
124
];

0 commit comments

Comments
 (0)