(&self, symmetry: Symmetry)
| 266 | } |
| 267 | |
| 268 | fn is_symmetric(&self, symmetry: Symmetry) -> bool { |
| 269 | fn flip(n: usize, max_n: usize) -> usize { |
| 270 | let mid_n = max_n / 2; |
| 271 | let new_n = -(n as i32 - mid_n as i32) + mid_n as i32; |
| 272 | (new_n - (1 - max_n as i32 % 2)) as usize |
| 273 | } |
| 274 | |
| 275 | for y in 0..self.height { |
| 276 | for x in 0..self.width { |
| 277 | let (new_x, new_y) = match symmetry { |
| 278 | Symmetry::Vertical => (flip(x, self.width), y), |
| 279 | Symmetry::Horizontal => (x, flip(y, self.height)), |
| 280 | Symmetry::Rotational => (flip(x, self.width), flip(y, self.height)), |
| 281 | }; |
| 282 | if new_x > self.width { |
| 283 | panic!("x {} -> {} is bad", x, new_x); |
| 284 | } |
| 285 | if new_y > self.height { |
| 286 | panic!("y {} -> {} is bad", y, new_y); |
| 287 | } |
| 288 | if self.is_passable_terrain[y][x] != self.is_passable_terrain[new_y][new_x] { |
| 289 | println!("{:?}: passable terrain at ({}, {}) ({}, {})", |
| 290 | symmetry, x, y, new_x, new_y); |
| 291 | return false; |
| 292 | } |
| 293 | if self.initial_karbonite[y][x] != self.initial_karbonite[new_y][new_x] { |
| 294 | println!("{:?}: initial karbonite at ({}, {}) ({}, {})", |
| 295 | symmetry, x, y, new_x, new_y); |
| 296 | return false; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // Initial units are symmetric |
| 302 | let mut unit_locations: HashSet<MapLocation> = HashSet::new(); |
| 303 | for ref unit in &self.initial_units { |
| 304 | let (x, y, planet) = { |
| 305 | let location = unit.location().map_location().expect( |
| 306 | "unit map location was validated previously"); |
| 307 | (location.x as usize, location.y as usize, location.planet) |
| 308 | }; |
| 309 | |
| 310 | let (new_x, new_y) = match symmetry { |
| 311 | Symmetry::Vertical => (flip(x, self.width), y), |
| 312 | Symmetry::Horizontal => (x, flip(y, self.height)), |
| 313 | Symmetry::Rotational => (flip(x, self.width), flip(y, self.height)), |
| 314 | }; |
| 315 | |
| 316 | unit_locations.insert(MapLocation::new(planet, x as i32, y as i32)); |
| 317 | unit_locations.insert(MapLocation::new(planet, new_x as i32, new_y as i32)); |
| 318 | } |
| 319 | if unit_locations.len() != self.initial_units.len() { |
| 320 | println!("{:?}: initial units {:?}", |
| 321 | symmetry, unit_locations); |
| 322 | return false; |
| 323 | } |
| 324 | true |
| 325 | } |
no test coverage detected