Skip to content

Commit

Permalink
suiro: Change pipe interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jun 23, 2022
1 parent 82210f7 commit 512e378
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 219 deletions.
2 changes: 1 addition & 1 deletion suiro/src/direction.rs
@@ -1,7 +1,7 @@
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Direction {
T,
R,
B,
L,
R,
}
34 changes: 13 additions & 21 deletions suiro/src/main.rs
Expand Up @@ -94,26 +94,18 @@ fn print(stdout: &mut StdoutLock, game: &Game) -> anyhow::Result<()> {
} else {
color_flow.to_string()
},
match p {
Pipe::I(d) => match d {
0 => '┃',
1 => '━',
_ => unreachable!(),
},
Pipe::L(d) => match d {
0 => '┗',
1 => '┏',
2 => '┓',
3 => '┛',
_ => unreachable!(),
},
Pipe::T(d) => match d {
0 => '┳',
1 => '┫',
2 => '┻',
3 => '┣',
_ => unreachable!(),
},
match char::from(p) {
'│' => '┃',
'─' => '━',
'└' => '┗',
'┌' => '┏',
'┐' => '┓',
'┘' => '┛',
'┬' => '┳',
'┤' => '┫',
'┴' => '┻',
'├' => '┣',
_ => unreachable!(),
},
termion::color::Fg(termion::color::Reset),
)
Expand Down Expand Up @@ -253,7 +245,7 @@ fn main() -> anyhow::Result<()> {
size,
(0..u16::from(size.width()) * u16::from(size.height()))
.into_iter()
.map(|_| Pipe::I(1))
.map(|_| Pipe::try_from('─').expect("pipe broken"))
.collect::<Vec<Pipe>>(),
)
})?;
Expand Down
67 changes: 39 additions & 28 deletions suiro/src/map.rs
Expand Up @@ -172,12 +172,15 @@ mod tests {

#[test]
fn from_str_test() -> anyhow::Result<()> {
let pipe_i = Pipe::try_from('│')?;
let pipe_l = Pipe::try_from('└')?;

// │
let s = base32::encode(base32::Alphabet::Crockford, &[0b00000000, 0b00000100]);
assert_eq!(s, "0020");
assert_eq!(
Map::from_str(s.as_str()),
Map::new(Size::new(1, 1)?, vec![Pipe::I(0)])
Map::new(Size::new(1, 1)?, vec![pipe_i])
);

// ││
Expand All @@ -188,7 +191,7 @@ mod tests {
assert_eq!(s, "20208");
assert_eq!(
Map::from_str(s.as_str()),
Map::new(Size::new(2, 1)?, vec![Pipe::I(0), Pipe::I(0)])
Map::new(Size::new(2, 1)?, vec![pipe_i, pipe_i])
);

// │└
Expand All @@ -200,57 +203,63 @@ mod tests {
assert_eq!(s, "2420G108");
assert_eq!(
Map::from_str(s.as_str()),
Map::new(
Size::new(2, 2)?,
vec![Pipe::I(0), Pipe::L(0), Pipe::I(0), Pipe::L(0)]
)
Map::new(Size::new(2, 2)?, vec![pipe_i, pipe_l, pipe_i, pipe_l])
);

Ok(())
}

#[test]
fn new_test() -> anyhow::Result<()> {
let area = Map::new(
Size::new(2, 2)?,
vec![Pipe::I(1), Pipe::L(0), Pipe::T(0), Pipe::L(0)],
)?;
assert_eq!(area.pipe(Point::new(0, 0)), Pipe::I(1));
assert_eq!(area.pipe(Point::new(1, 0)), Pipe::L(0));
assert_eq!(area.pipe(Point::new(0, 1)), Pipe::T(0));
assert_eq!(area.pipe(Point::new(1, 1)), Pipe::L(0));
let pipe_i = Pipe::try_from('│')?;
let pipe_l = Pipe::try_from('└')?;
let pipe_t = Pipe::try_from('┬')?;

let area = Map::new(Size::new(2, 2)?, vec![pipe_i, pipe_l, pipe_t, pipe_l])?;
assert_eq!(area.pipe(Point::new(0, 0)), pipe_i);
assert_eq!(area.pipe(Point::new(1, 0)), pipe_l);
assert_eq!(area.pipe(Point::new(0, 1)), pipe_t);
assert_eq!(area.pipe(Point::new(1, 1)), pipe_l);
Ok(())
}

#[test]
fn height_test() -> anyhow::Result<()> {
let area = Map::new(Size::new(1, 2)?, vec![Pipe::I(1), Pipe::L(0)])?;
let pipe_i = Pipe::try_from('│')?;
let pipe_l = Pipe::try_from('└')?;

let area = Map::new(Size::new(1, 2)?, vec![pipe_i, pipe_l])?;
assert_eq!(area.height(), 2);
Ok(())
}

#[test]
fn rotate_test() -> anyhow::Result<()> {
let mut area = Map::new(
Size::new(2, 2)?,
vec![Pipe::I(1), Pipe::L(0), Pipe::T(0), Pipe::L(0)],
)?;
assert_eq!(area.pipe(Point::new(0, 0)), Pipe::I(1));
assert_eq!(area.pipe(Point::new(1, 0)), Pipe::L(0));
assert_eq!(area.pipe(Point::new(0, 1)), Pipe::T(0));
assert_eq!(area.pipe(Point::new(1, 1)), Pipe::L(0));
let pipe_i = Pipe::try_from('│')?;
let pipe_l = Pipe::try_from('└')?;
let pipe_t = Pipe::try_from('┬')?;

let mut area = Map::new(Size::new(2, 2)?, vec![pipe_i, pipe_l, pipe_t, pipe_l])?;
assert_eq!(area.pipe(Point::new(0, 0)), pipe_i);
assert_eq!(area.pipe(Point::new(1, 0)), pipe_l);
assert_eq!(area.pipe(Point::new(0, 1)), pipe_t);
assert_eq!(area.pipe(Point::new(1, 1)), pipe_l);
area.rotate(Point::new(0, 1));
assert_eq!(area.pipe(Point::new(0, 1)), Pipe::T(1));
assert_eq!(area.pipe(Point::new(0, 1)), pipe_t.rotate());
area.rotate(Point::new(0, 0));
assert_eq!(area.pipe(Point::new(0, 0)), Pipe::I(0));
assert_eq!(area.pipe(Point::new(0, 0)), pipe_i.rotate());
Ok(())
}

#[test]
fn test_test() -> anyhow::Result<()> {
let pipe_i = Pipe::try_from('│')?;
let pipe_l = Pipe::try_from('└')?;
let pipe_t = Pipe::try_from('┬')?;

let area = Map::new(
Size::new(2, 2)?,
vec![Pipe::I(1), Pipe::L(0), Pipe::T(0), Pipe::L(0)],
vec![pipe_i.rotate(), pipe_l, pipe_t, pipe_l],
)?;
let (ok, ng, flow) = area.test();
assert!(!ok);
Expand All @@ -259,7 +268,7 @@ mod tests {

let area = Map::new(
Size::new(2, 2)?,
vec![Pipe::I(1), Pipe::L(2), Pipe::T(0), Pipe::L(0)],
vec![pipe_i.rotate(), pipe_l.rotate().rotate(), pipe_t, pipe_l],
)?;
let (ok, ng, flow) = area.test();
assert!(ok);
Expand All @@ -270,7 +279,9 @@ mod tests {

#[test]
fn width_test() -> anyhow::Result<()> {
let area = Map::new(Size::new(1, 2)?, vec![Pipe::I(1), Pipe::L(0)])?;
let pipe_i = Pipe::try_from('│')?;
let pipe_l = Pipe::try_from('└')?;
let area = Map::new(Size::new(1, 2)?, vec![pipe_i, pipe_l])?;
assert_eq!(area.width(), 1);
Ok(())
}
Expand Down

0 comments on commit 512e378

Please sign in to comment.