Skip to content

Commit 6fde86d

Browse files
committed
Add DirectedPos struct
1 parent 113ec09 commit 6fde86d

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

src/grid/directed_pos.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use crate::grid::{Grid, Pos};
2+
3+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4+
pub struct DirectedPos {
5+
pos: Pos,
6+
dir: Direction,
7+
}
8+
9+
#[repr(u8)]
10+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11+
pub enum Direction {
12+
North = 0,
13+
NorthEast = 1,
14+
East = 2,
15+
SouthEast = 3,
16+
South = 4,
17+
SouthWest = 5,
18+
West = 6,
19+
NorthWest = 7,
20+
}
21+
22+
impl DirectedPos {
23+
pub fn new(pos: Pos, dir: Direction) -> Self {
24+
Self { pos, dir }
25+
}
26+
27+
pub fn pos(&self) -> Pos {
28+
self.pos
29+
}
30+
31+
pub fn direction(&self) -> Direction {
32+
self.dir
33+
}
34+
35+
pub fn index(&self) -> usize {
36+
self.pos.index()
37+
}
38+
39+
pub fn value(&self) -> char {
40+
self.pos.value()
41+
}
42+
43+
pub fn row(&self) -> usize {
44+
self.pos.row()
45+
}
46+
47+
pub fn col(&self) -> usize {
48+
self.pos.col()
49+
}
50+
51+
pub fn north(&self, grid: &Grid) -> Option<DirectedPos> {
52+
self.pos.north(grid).map(|p| Self::new(p, self.dir))
53+
}
54+
55+
pub fn north_east(&self, grid: &Grid) -> Option<DirectedPos> {
56+
self.pos.north_east(grid).map(|p| Self::new(p, self.dir))
57+
}
58+
59+
pub fn east(&self, grid: &Grid) -> Option<DirectedPos> {
60+
self.pos.east(grid).map(|p| Self::new(p, self.dir))
61+
}
62+
63+
pub fn south_east(&self, grid: &Grid) -> Option<DirectedPos> {
64+
self.pos.south_east(grid).map(|p| Self::new(p, self.dir))
65+
}
66+
67+
pub fn south(&self, grid: &Grid) -> Option<DirectedPos> {
68+
self.pos.south(grid).map(|p| Self::new(p, self.dir))
69+
}
70+
71+
pub fn south_west(&self, grid: &Grid) -> Option<DirectedPos> {
72+
self.pos.south_west(grid).map(|p| Self::new(p, self.dir))
73+
}
74+
75+
pub fn west(&self, grid: &Grid) -> Option<DirectedPos> {
76+
self.pos.west(grid).map(|p| Self::new(p, self.dir))
77+
}
78+
79+
pub fn north_west(&self, grid: &Grid) -> Option<DirectedPos> {
80+
self.pos.north_west(grid).map(|p| Self::new(p, self.dir))
81+
}
82+
}
83+
84+
#[cfg(test)]
85+
mod test {
86+
use super::*;
87+
88+
#[test]
89+
fn test_pos() {
90+
let grid = Grid::new("012\n345\n678".to_string());
91+
let pos = DirectedPos::new(grid.pos(4), Direction::North);
92+
assert_eq!(pos, grid.pos(4).directed(Direction::North));
93+
assert_eq!(pos.direction(), Direction::North);
94+
assert_eq!(pos.index(), 4);
95+
assert_eq!(
96+
pos.north(&grid).unwrap(),
97+
DirectedPos::new(grid.pos(1), Direction::North)
98+
);
99+
}
100+
}

src/grid/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use std::{
44
ops::Deref,
55
};
66

7+
mod directed_pos;
78
mod pos;
9+
pub use directed_pos::DirectedPos;
10+
pub use directed_pos::Direction;
811
pub use pos::Pos;
912

1013
#[derive(Debug)]

src/grid/pos.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::grid::Grid;
1+
use crate::grid::{DirectedPos, Direction, Grid};
22

33
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44
pub struct Pos {
@@ -113,6 +113,10 @@ impl Pos {
113113
self.north_west(grid),
114114
])
115115
}
116+
117+
pub fn directed(self, dir: Direction) -> DirectedPos {
118+
DirectedPos::new(self, dir)
119+
}
116120
}
117121

118122
pub struct NeighborsIter([Option<Pos>; 8]);

0 commit comments

Comments
 (0)