Skip to content

Commit 3b22566

Browse files
committed
wormhole move gen
1 parent 03e7233 commit 3b22566

2 files changed

Lines changed: 72 additions & 16 deletions

File tree

gui/src/wormhole/board.rs

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ The numbering has been chosen such that the following symmetries are easier to c
6060
- Flip along the xy-diagonal i.e. flip along the Top and Bottom along the nw-se diagonal and the flip the Hole left-right on the line connecting `y` to `Y` or equivalently the line connecting `w` to `W`.
6161
*/
6262

63+
use std::collections::{BTreeSet, HashSet};
64+
6365
use glam::Vec3;
6466

6567
use crate::wormhole::board_render::BoardParams;
@@ -73,7 +75,7 @@ pub enum PosType {
7375
HolePent, // A number in the "Hole" part of the diagram between 128 and 143 inclusive.
7476
}
7577

76-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
78+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7779
pub struct Pos {
7880
n: u8,
7981
}
@@ -661,13 +663,15 @@ pub struct MovesLookup {
661663
diagonal_adjacent: [Vec<Pos>; 144],
662664
continuations: [[Vec<Pos>; 144]; 144],
663665
knight_moves: [Vec<Pos>; 144],
666+
white_forward_diagonal: [Vec<Pos>; 144],
667+
black_forward_diagonal: [Vec<Pos>; 144],
664668
cardinal_slides: [Vec<Slides>; 144],
665669
diagonal_slides: [Vec<Slides>; 144],
666670
}
667671

668672
impl MovesLookup {
669673
pub fn new() -> Self {
670-
let white_forward = std::array::from_fn(|i| {
674+
let white_forward: [Vec<Pos>; 144] = std::array::from_fn(|i| {
671675
let mut pos = Pos::new(i as u8);
672676
let flip_z = pos.full_symmetry_and_orbit().0.flip_z;
673677
if flip_z {
@@ -724,7 +728,7 @@ impl MovesLookup {
724728
forward
725729
});
726730

727-
let black_forward = std::array::from_fn(|i| {
731+
let black_forward: [Vec<Pos>; 144] = std::array::from_fn(|i| {
728732
white_forward[Pos::new(i as u8).flip_x().idx()]
729733
.iter()
730734
.map(|p| p.flip_x())
@@ -1072,13 +1076,67 @@ impl MovesLookup {
10721076
})
10731077
});
10741078

1079+
let perp_cardinal_continuations: [[Vec<Pos>; 144]; 144] = std::array::from_fn(|a_idx| {
1080+
std::array::from_fn(|b_idx| {
1081+
let mut perp = cardinal_adjacent[b_idx]
1082+
.iter()
1083+
.cloned()
1084+
.collect::<HashSet<Pos>>();
1085+
perp.remove(&Pos::new(a_idx as u8));
1086+
for c_pos in &continuations[a_idx][b_idx] {
1087+
perp.remove(c_pos);
1088+
}
1089+
perp.into_iter().collect::<Vec<_>>()
1090+
})
1091+
});
1092+
1093+
let knight_moves = std::array::from_fn(|idx| {
1094+
let a = Pos::new(idx as u8);
1095+
let mut mvs = HashSet::new();
1096+
for b in &cardinal_adjacent[idx] {
1097+
for c in &continuations[a.idx()][b.idx()] {
1098+
for d in &perp_cardinal_continuations[b.idx()][c.idx()] {
1099+
mvs.insert(*d);
1100+
}
1101+
}
1102+
for c in &perp_cardinal_continuations[a.idx()][b.idx()] {
1103+
for d in &continuations[b.idx()][c.idx()] {
1104+
mvs.insert(*d);
1105+
}
1106+
}
1107+
}
1108+
mvs.into_iter().collect()
1109+
});
1110+
1111+
let white_forward_diagonal = std::array::from_fn(|a_idx| {
1112+
let mut mvs = HashSet::new();
1113+
for b_pos in white_forward[a_idx].iter() {
1114+
for c_pos in &perp_cardinal_continuations[a_idx][b_pos.idx()] {
1115+
mvs.insert(*c_pos);
1116+
}
1117+
}
1118+
mvs.into_iter().collect::<Vec<_>>()
1119+
});
1120+
1121+
let black_forward_diagonal = std::array::from_fn(|a_idx| {
1122+
let mut mvs = HashSet::new();
1123+
for b_pos in black_forward[a_idx].iter() {
1124+
for c_pos in &perp_cardinal_continuations[a_idx][b_pos.idx()] {
1125+
mvs.insert(*c_pos);
1126+
}
1127+
}
1128+
mvs.into_iter().collect::<Vec<_>>()
1129+
});
1130+
10751131
Self {
10761132
white_forward,
10771133
black_forward,
10781134
cardinal_adjacent,
10791135
diagonal_adjacent,
10801136
continuations,
1081-
knight_moves: std::array::from_fn(|_| Vec::new()),
1137+
knight_moves,
1138+
white_forward_diagonal,
1139+
black_forward_diagonal,
10821140
cardinal_slides: std::array::from_fn(|_| Vec::new()),
10831141
diagonal_slides: std::array::from_fn(|_| Vec::new()),
10841142
}
@@ -1088,6 +1146,14 @@ impl MovesLookup {
10881146
&self.white_forward[pos.idx()]
10891147
}
10901148

1149+
pub fn white_forward_diagonal(&self, pos: Pos) -> &[Pos] {
1150+
&self.white_forward_diagonal[pos.idx()]
1151+
}
1152+
1153+
pub fn black_forward_diagonal(&self, pos: Pos) -> &[Pos] {
1154+
&self.black_forward_diagonal[pos.idx()]
1155+
}
1156+
10911157
pub fn black_forward(&self, pos: Pos) -> &[Pos] {
10921158
&self.black_forward[pos.idx()]
10931159
}

gui/src/wormhole/board_to_egui/mod.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,18 +286,8 @@ impl Pipeline {
286286
if let Some(pos) = pos {
287287
colours[pos.idx()] = [0.0, 0.5, 1.0, 0.8];
288288
let moves = MovesLookup::new();
289-
for p in moves
290-
.cardinal_adjacent(pos)
291-
.iter()
292-
.chain(moves.diagonal_adjacent(pos))
293-
{
294-
for c in moves.continuations(*p, pos) {
295-
if colours[c.idx()] != [1.0, 0.0, 1.0, 0.8] {
296-
colours[c.idx()] = [1.0, 0.0, 1.0, 0.8];
297-
} else {
298-
colours[c.idx()] = [0.0, 1.0, 1.0, 0.8];
299-
}
300-
}
289+
for p in moves.knight_moves(pos) {
290+
colours[p.idx()] = [1.0, 0.0, 1.0, 0.8];
301291
}
302292
}
303293
println!("{:?}", pos);

0 commit comments

Comments
 (0)