From 4c35daac3e9663c6dd2a11bcd7137276c051971c Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Mon, 20 Oct 2025 22:28:37 -0700 Subject: [PATCH] Tests pass so we are... almost cooking --- src/bin/tetris/main.rs | 57 ++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/src/bin/tetris/main.rs b/src/bin/tetris/main.rs index e13e145..ea893f9 100644 --- a/src/bin/tetris/main.rs +++ b/src/bin/tetris/main.rs @@ -1,8 +1,6 @@ // Bevy basically forces "complex types" with Querys #![allow(clippy::type_complexity)] -use bevy::render::render_resource::encase::matrix::AsMutMatrixParts; - use games::*; // *TODO: Detect when piece is going to go out of bounds and restirct parent from moving there @@ -187,7 +185,7 @@ impl GridPosition { } } -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] enum GameError { #[error("Coordinates are out of bounds: Left")] OutOfBoundsLeft, @@ -455,12 +453,35 @@ impl Shape { } // TODO: return impl Iterator - fn relative_coordinates(&self) -> Vec { - todo!() + fn relative_coordinates(&self) -> impl Iterator { + let mut v: Vec = Vec::new(); + match self { + Self::M4(inner) => { + for (i, y) in (-1..3).rev().enumerate() { + let c = inner.col(i); + for (j, x) in (-1..3).enumerate() { + if c[j] == 1.0 { + v.push(RelativePosition { x, y }); + } + } + } + } + Self::M3(inner) => { + for (i, y) in (-1..2).rev().enumerate() { + let c = inner.col(i); + for (j, x) in (-1..2).enumerate() { + if c[j] == 1.0 { + v.push(RelativePosition { x, y }); + } + } + } + } + }; + v.into_iter() } - fn computed_coordinates(&self, center: &GridPosition) -> Vec { - todo!() + fn computed_coordinates(&self, center: &GridPosition) -> impl Iterator> { + self.relative_coordinates().map(|rp| center.add_relative(&rp)) } fn as_ascii(&self) -> String { @@ -559,14 +580,16 @@ mod test { fn test_relative_coordinates() { let shape = Shape::new_t(); - let expected = vec![ + let expected: Vec = vec![ + (0, 1).into(), (-1, 0).into(), (0, 0).into(), (1, 0).into(), - (0, 1).into(), ]; - assert_eq!(shape.relative_coordinates(), expected); + let actual: Vec = shape.relative_coordinates().collect(); + + assert_eq!(actual, expected); } #[test] @@ -575,14 +598,16 @@ mod test { let center = GridPosition { x: 5, y: 5 }; - let expected = vec![ - (4, 5).into(), - (5, 5).into(), - (6, 5).into(), - (5, 6).into(), + let expected: Vec> = vec![ + Ok((5, 6).into()), + Ok((4, 5).into()), + Ok((5, 5).into()), + Ok((6, 5).into()), ]; - assert_eq!(shape.computed_coordinates(¢er), expected); + let actual: Vec> = shape.computed_coordinates(¢er).collect(); + + assert_eq!(actual, expected); } }