Procházet zdrojové kódy

Part testing complete (except rotations)

Christoph Stelz před 1 rokem
rodič
revize
756fbe6ddd
1 změnil soubory, kde provedl 73 přidání a 7 odebrání
  1. 73 7
      src/lib.rs

+ 73 - 7
src/lib.rs

@@ -2,7 +2,7 @@ use std::collections::HashMap;
 
 
 /* 
- * The basic element is a single Triangle.
+ * The basic element is a single triangle.
  * It can be with a wide base (upright) or on its tip:
  *
  *      /\     +--------+
@@ -22,7 +22,7 @@ use std::collections::HashMap;
  *
  */
 
-#[derive(Copy,Clone)]
+#[derive(Debug,PartialEq,Copy,Clone)]
 pub enum Edge {
     BOTTOMTOP, // 1
     RIGHT,     // 2
@@ -48,8 +48,7 @@ pub type Part = Vec<Edge>;
  *      \  /
  *       \/
  *
- *
- *
+ *         (1)
  */
 pub fn generate_parts() -> HashMap<PartID, Part> {
     HashMap::from([
@@ -75,6 +74,7 @@ pub type MapIO = (MapSideIO, MapSideIO);
  * Each cell can be a Barrier (meaning no part may be placed on the cell), Empty (meaning at the
  * current time, it is not occupied by a part) or Occupied.
  */
+#[derive(Copy, Clone)]
 pub enum Cell {
     Barrier,
     Empty,
@@ -187,9 +187,10 @@ pub fn check_part(map: &MapSide, part: &Part, x: u8, y: u8, rotation: Edge) -> b
         return false;
     }
 
-    let mut mx = x;
-    let mut my = y;
+    let mut mx : u8 = x;
+    let mut my : u8 = y;
 
+    println!("Starting at {}, {}", x, y);
     for movement in part {
         let result = move_along_edge(mx, my, *movement);
 
@@ -198,9 +199,15 @@ pub fn check_part(map: &MapSide, part: &Part, x: u8, y: u8, rotation: Edge) -> b
         }
 
         (mx, my) = result.unwrap();
+
+        if !map[my as usize][mx as usize].is_empty() || !in_bound(mx, my) {
+            return false;
+        }
+        println!("Went to {}, {}", mx, my);
     }
+    println!("");
 
-    return false;
+    return true;
 }
 
 
@@ -219,4 +226,63 @@ mod test {
         }
     }
 
+    #[test]
+    fn test_edge_movement() {
+        let (x1, y1) = move_along_edge(0, 0, Edge::RIGHT).unwrap();
+        assert_eq!((1, 0), (x1, y1));
+
+        assert!(move_along_edge(15, 0, Edge::RIGHT).is_none());
+        assert!(move_along_edge(0, 1, Edge::LEFT).is_none());
+
+        let (x2, y2) = move_along_edge(2, 1, Edge::BOTTOMTOP).unwrap();
+        assert_eq!((2, 0), (x2, y2));
+        
+    }
+
+    #[test]
+    fn test_part_placement() {
+        let B = Cell::Barrier;
+        let E = Cell::Empty;
+
+        let left : MapSide = [
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, E, E, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, E, E, E, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, E, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+            [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B],
+        ];
+
+        let parts = generate_parts();
+        let brown_part = parts.get(&1).unwrap();
+
+
+        assert!(check_part(&left, &brown_part, 4, 2, Edge::BOTTOMTOP));
+        let mut called : usize = 0;
+        for x in 0..15 {
+            for y in 0..15 {
+                for rotation in [Edge::LEFT, Edge::RIGHT, Edge::BOTTOMTOP] {
+                    if check_part(&left, &brown_part, x, y, Edge::BOTTOMTOP) {
+                        //assert_eq!(Edge::BOTTOMTOP, rotation);
+                        assert_eq!((4, 2), (x, y));
+                        called += 1;
+                    }
+                }
+            }
+        }
+        assert_eq!(3, called);
+
+    }
+    
+
 }