Skip to content

Commit

Permalink
roguelike-tdd: Add MapGenerator::write and MapUtil::parse
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Feb 23, 2024
1 parent b7aa3f1 commit e919c1d
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 39 deletions.
1 change: 1 addition & 0 deletions roguelike-tdd/src/dungeon.rs
@@ -1,6 +1,7 @@
mod door;
mod map_chips;
mod map_generator;
mod map_util;
mod passage;
mod room;
mod stairs;
86 changes: 84 additions & 2 deletions roguelike-tdd/src/dungeon/map_generator.rs
@@ -1,4 +1,4 @@
use super::map_chips::MapChip;
use super::{door::Door, map_chips::MapChip, passage::Passage, room::Room, stairs::Stairs};

pub struct MapGenerator {
pub map: Vec<Vec<MapChip>>,
Expand All @@ -10,11 +10,35 @@ impl MapGenerator {
map: vec![vec![MapChip::Wall; width]; height],
}
}

pub fn write(
&mut self,
rooms: &[Room],
passages: &[Passage],
doors: &[Door],
stairs: &[Stairs],
) {
for passage in passages {
passage.write_to_map(&mut self.map);
}
for room in rooms {
room.write_to_map(&mut self.map);
}
for door in doors {
door.write_to_map(&mut self.map);
}
for stair in stairs {
stair.write_to_map(&mut self.map);
}
}
}

#[cfg(test)]
mod tests {
use crate::dungeon::map_chips::MapChip;
use crate::dungeon::{
door::Door, map_chips::MapChip, map_util::MapUtil, passage::Passage, room::Room,
stairs::Stairs,
};

use super::*;

Expand All @@ -35,4 +59,62 @@ mod tests {

assert_eq!(sut.map, vec![vec![MapChip::Wall; width]; height]);
}

#[test]
fn test_write_マップを構成する要素をマップ配列に書き込めること() {
let mut sut = MapGenerator::new(10, 9);
let rooms = vec![
Room {
x: 1,
y: 1,
width: 3,
height: 3,
},
Room {
x: 5,
y: 5,
width: 4,
height: 3,
},
];
let passages = vec![Passage::new(rooms[0].clone(), rooms[1].clone())];
let mut doors = Door::create_doors(rooms[0].clone(), passages.clone());
doors.extend(Door::create_doors(rooms[1].clone(), passages.clone()));
let stairs = vec![
Stairs::new(
vec![Room {
x: 1,
y: 1,
width: 1,
height: 1,
}],
MapChip::UpStairs,
),
Stairs::new(
vec![Room {
x: 6,
y: 6,
width: 1,
height: 1,
}],
MapChip::DownStairs,
),
];
let expected = MapUtil::parse(
r#"
WWWWWWWWWW
WURRWWWWWW
WRRRDPPPWW
WRRRWWWPWW
WWWWWWWDWW
WWWWWRRRRW
WWWWWRSRRW
WWWWWRRRRW
WWWWWWWWWW
"#
.trim(),
);
sut.write(&rooms, &passages, &doors, &stairs);
assert_eq!(sut.map, expected);
}
}
23 changes: 23 additions & 0 deletions roguelike-tdd/src/dungeon/map_util.rs
@@ -0,0 +1,23 @@
use super::map_chips::MapChip;

pub struct MapUtil;

impl MapUtil {
pub fn parse(s: &str) -> Vec<Vec<MapChip>> {
s.lines()
.map(|line| {
line.chars()
.map(|c| match c {
'D' => MapChip::Door,
'P' => MapChip::Passage,
'R' => MapChip::Room,
'S' => MapChip::DownStairs,
'U' => MapChip::UpStairs,
'W' => MapChip::Wall,
_ => unreachable!(),
})
.collect::<Vec<MapChip>>()
})
.collect::<Vec<Vec<MapChip>>>()
}
}
21 changes: 5 additions & 16 deletions roguelike-tdd/src/dungeon/passage.rs
@@ -1,5 +1,6 @@
use super::{map_chips::MapChip, room::Room};

#[derive(Clone, Debug)]
pub struct Passage {
pub steps: Vec<(usize, usize)>,
}
Expand Down Expand Up @@ -65,7 +66,9 @@ impl Passage {

#[cfg(test)]
mod tests {
use crate::dungeon::{map_chips::MapChip, map_generator::MapGenerator, room::Room};
use crate::dungeon::{
map_chips::MapChip, map_generator::MapGenerator, map_util::MapUtil, room::Room,
};

use super::*;

Expand Down Expand Up @@ -113,7 +116,7 @@ mod tests {
height: 4,
};
let mut map = MapGenerator::new(8, 6);
let expected = map_util_parse(
let expected = MapUtil::parse(
r#"
WWWWWWWW
WPPPPPPW
Expand Down Expand Up @@ -215,18 +218,4 @@ WWWWWWWW
})
.collect::<Vec<Room>>()
}

fn map_util_parse(s: &str) -> Vec<Vec<MapChip>> {
s.lines()
.map(|line| {
line.chars()
.map(|c| match c {
'P' => MapChip::Passage,
'W' => MapChip::Wall,
_ => unreachable!(),
})
.collect::<Vec<MapChip>>()
})
.collect::<Vec<Vec<MapChip>>>()
}
}
2 changes: 1 addition & 1 deletion roguelike-tdd/src/dungeon/room.rs
Expand Up @@ -94,7 +94,7 @@ impl Room {
self.x + self.width - 1
}

fn write_to_map(&self, map: &mut [Vec<MapChip>]) {
pub fn write_to_map(&self, map: &mut [Vec<MapChip>]) {
for y in self.y..=self.bottom() {
for x in self.x..=self.right() {
map[y][x] = crate::dungeon::map_chips::MapChip::Room;
Expand Down
26 changes: 6 additions & 20 deletions roguelike-tdd/src/dungeon/stairs.rs
Expand Up @@ -39,7 +39,9 @@ impl Stairs {
mod tests {
use std::collections::HashSet;

use crate::dungeon::{map_chips::MapChip, map_generator::MapGenerator, room::Room};
use crate::dungeon::{
map_chips::MapChip, map_generator::MapGenerator, map_util::MapUtil, room::Room,
};

use super::*;

Expand Down Expand Up @@ -166,7 +168,7 @@ mod tests {
let stairs_type = MapChip::UpStairs;
let stairs = Stairs::new(vec![room], stairs_type);
let mut map = MapGenerator::new(2, 2);
let expected = map_util_parse(
let expected = MapUtil::parse(
r#"
WW
WU
Expand All @@ -188,30 +190,14 @@ WU
let stairs_type = MapChip::DownStairs;
let stairs = Stairs::new(vec![room], stairs_type);
let mut map = MapGenerator::new(2, 2);
let expected = map_util_parse(
let expected = MapUtil::parse(
r#"
WW
WD
WS
"#
.trim(),
);
stairs.write_to_map(&mut map.map);
assert_eq!(map.map, expected);
}

fn map_util_parse(s: &str) -> Vec<Vec<MapChip>> {
s.lines()
.map(|line| {
line.chars()
.map(|c| match c {
'P' => MapChip::Passage,
'W' => MapChip::Wall,
'U' => MapChip::UpStairs,
'D' => MapChip::DownStairs,
_ => unreachable!("invalid map chip: {}", c),
})
.collect::<Vec<MapChip>>()
})
.collect::<Vec<Vec<MapChip>>>()
}
}

0 comments on commit e919c1d

Please sign in to comment.