Fix my busted implementations

This commit is contained in:
Patrick Gelvin
2025-08-23 12:56:10 -07:00
parent 031a487fb5
commit b0ddc9236b

View File

@@ -160,13 +160,30 @@ impl Card {
pub fn flip(self, flip: FlipDir) -> Self { pub fn flip(self, flip: FlipDir) -> Self {
let mut new_card = Self::default(); let mut new_card = Self::default();
let transform = match flip {
FlipDir::Horizontal => |cell: Cell| match cell {
Cell::NW => Cell::NE,
Cell::NE => Cell::NW,
Cell::SW => Cell::SE,
Cell::SE => Cell::SW,
other => other,
},
FlipDir::Vertical => |cell: Cell| match cell {
Cell::NW => Cell::SW,
Cell::NE => Cell::SE,
Cell::SW => Cell::NW,
Cell::SE => Cell::NE,
other => other,
},
};
// Check for doors // Check for doors
macro_rules! assign_cell { macro_rules! assign_cell {
( ($source_dir:ident, $source_index:literal), ($dest_dir:ident, $dest_index:literal), $repeat:expr ) => { ( ($source_dir:ident, $source_index:literal), ($dest_dir:ident, $dest_index:literal), $repeat:expr ) => {
new_card.cells[$dest_index] = self.cells[$source_index]; new_card.cells[$dest_index] = transform(self.cells[$source_index]);
new_card.$dest_dir = self.$source_dir; new_card.$dest_dir = self.$source_dir;
if $repeat == true { if $repeat == true {
new_card.cells[$source_index] = self.cells[$dest_index]; new_card.cells[$source_index] = transform(self.cells[$dest_index]);
new_card.$source_dir = self.$dest_dir; new_card.$source_dir = self.$dest_dir;
} }
}; };
@@ -189,7 +206,7 @@ impl Card {
} }
} }
new_card.cells[4] = self.cells[4]; new_card.cells[4] = transform(self.cells[4]);
new_card new_card
} }
@@ -251,38 +268,55 @@ impl Card {
pub fn rotate(self, dir: RotationDir) -> Self { pub fn rotate(self, dir: RotationDir) -> Self {
let mut new_card = Self::default(); let mut new_card = Self::default();
let transform = match dir {
RotationDir::Clockwise => |cell: Cell| match cell {
Cell::NW => Cell::NE,
Cell::NE => Cell::SE,
Cell::SE => Cell::SW,
Cell::SW => Cell::NW,
other => other,
},
RotationDir::CounterClockwise => |cell: Cell| match cell {
Cell::NW => Cell::SW,
Cell::SW => Cell::SE,
Cell::SE => Cell::NW,
Cell::NE => Cell::NW,
other => other,
},
};
// Check for doors // Check for doors
macro_rules! assign_cell { macro_rules! assign_cell {
( ($source_dir:ident, $source_index:literal), ($dest_dir:ident, $dest_index:literal) ) => { ( ($source_dir:ident, $source_index:literal), ($dest_dir:ident, $dest_index:literal) ) => {
new_card.cells[$dest_index] = self.cells[$source_index]; new_card.cells[$dest_index] = transform(self.cells[$source_index]);
new_card.$dest_dir = self.$source_dir; new_card.$dest_dir = self.$source_dir;
}; };
} }
match dir { match dir {
RotationDir::Clockwise => { RotationDir::Clockwise => {
assign_cell!((nw, 0), (n, 1)); assign_cell!((nw, 0), (ne, 2));
assign_cell!((n, 1), (ne, 2)); assign_cell!((n, 1), (e, 5));
assign_cell!((ne, 2), (e, 5)); assign_cell!((ne, 2), (se, 8));
assign_cell!((e, 5), (se, 8)); assign_cell!((e, 5), (s, 7));
assign_cell!((se, 8), (s, 7)); assign_cell!((se, 8), (sw, 6));
assign_cell!((s, 7), (sw, 6)); assign_cell!((s, 7), (w, 3));
assign_cell!((sw, 6), (w, 3)); assign_cell!((sw, 6), (nw, 0));
assign_cell!((w, 3), (nw, 0)); assign_cell!((w, 3), (n, 1));
} }
RotationDir::CounterClockwise => { RotationDir::CounterClockwise => {
assign_cell!((nw, 0), (w, 3)); assign_cell!((nw, 0), (sw, 6));
assign_cell!((w, 3), (sw, 6)); assign_cell!((w, 3), (s, 7));
assign_cell!((sw, 6), (s, 7)); assign_cell!((sw, 6), (se, 8));
assign_cell!((s, 7), (se, 8)); assign_cell!((s, 7), (e, 5));
assign_cell!((se, 8), (e, 5)); assign_cell!((se, 8), (ne, 2));
assign_cell!((e, 5), (ne, 2)); assign_cell!((e, 5), (n, 1));
assign_cell!((ne, 2), (n, 1)); assign_cell!((ne, 2), (nw, 0));
assign_cell!((n, 1), (nw, 0)); assign_cell!((n, 1), (w, 3));
} }
} }
new_card.cells[4] = self.cells[4]; new_card.cells[4] = transform(self.cells[4]);
new_card new_card
} }
@@ -704,10 +738,8 @@ mod test {
..Default::default() ..Default::default()
}; };
let (res_left, res_right) = left_shape.transpose( let (res_left, res_right) =
right_shape, left_shape.transpose(right_shape, TransposeSelection::Row(TransposeIndex::Third));
TransposeSelection::Row(TransposeIndex::Third),
);
assert_eq!(res_left, expected_left); assert_eq!(res_left, expected_left);
assert_eq!(res_right, expected_right); assert_eq!(res_right, expected_right);
} }