Skip to content

Commit

Permalink
roguelike-tdd: Add Passage::get_outer_perimeter
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Feb 18, 2024
1 parent 76db8e9 commit 4182f5d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions roguelike-tdd/src/dungeon/passage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ impl Passage {
Passage { steps }
}

// 外周順 (0, 1, 2, 5, 8, 7, 6, 3, 0) に並べる
pub fn get_outer_perimeter(rooms: &[Room]) -> Vec<Room> {
vec![
rooms[0].clone(),
rooms[1].clone(),
rooms[2].clone(),
rooms[5].clone(),
rooms[8].clone(),
rooms[7].clone(),
rooms[6].clone(),
rooms[3].clone(),
rooms[0].clone(),
]
}

pub fn write_to_map(&self, map: &mut [Vec<MapChip>]) {
for (x, y) in &self.steps {
map[*y][*x] = MapChip::Passage;
Expand Down Expand Up @@ -95,6 +110,35 @@ WWWWWWWW
assert_eq!(map.map, expected);
}

#[test]
fn test_get_outer_perimeter_外周の部屋を連結順に並べた配列が変えること() {
let rooms = vec![
(0, 0),
(2, 0),
(4, 0),
(0, 2),
(2, 2),
(4, 2),
(0, 4),
(2, 4),
(4, 4),
]
.into_iter()
.map(|(x, y)| Room {
x,
y,
width: 2,
height: 2,
})
.collect::<Vec<Room>>();
let expeted = vec![0, 1, 2, 5, 8, 7, 6, 3, 0]
.into_iter()
.map(|i| rooms[i].clone())
.collect::<Vec<Room>>();
let actual = Passage::get_outer_perimeter(&rooms);
assert_eq!(actual, expeted);
}

fn map_util_parse(s: &str) -> Vec<Vec<MapChip>> {
s.lines()
.map(|line| {
Expand Down
2 changes: 1 addition & 1 deletion roguelike-tdd/src/dungeon/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::dungeon::room;

use super::map_chips::MapChip;

#[derive(Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Room {
pub x: usize,
pub y: usize,
Expand Down

0 comments on commit 4182f5d

Please sign in to comment.