fix: account for wrapping in grid.get and grid.get_mut

This commit is contained in:
Matej Janezic 2024-12-21 12:47:20 +01:00
parent 000002509e
commit 00000260b8
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
2 changed files with 12 additions and 2 deletions

View File

@ -43,7 +43,7 @@ impl Display for Grid {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (idx, cell) in self.grid.iter().enumerate() {
write!(f, "{}", *cell as char)?;
if idx > 0 && idx % self.width == 0 {
if idx > 0 && (idx + 1) % self.width == 0 {
writeln!(f)?;
}
}
@ -77,10 +77,20 @@ impl Grid {
}
pub fn get(&self, p: &Point) -> Option<&u8> {
if p.i >= self.grid.len() / self.width || p.j >= self.width {
return None;
}
self.grid.get(p.i * self.width + p.j)
}
pub fn get_mut(&mut self, p: &Point) -> Option<&mut u8> {
if p.i >= self.grid.len() / self.width || p.j >= self.width {
return None;
}
self.grid.get_mut(p.i * self.width + p.j)
}
pub fn size(&self) -> (usize, usize) {
(self.grid.len() / self.width, self.width)
}
}

View File

@ -89,7 +89,7 @@ impl Point {
#[macro_export]
macro_rules! pnt {
($i:literal, $j:literal) => {
($i:expr, $j:expr) => {
Point { i: $i, j: $j }
};
}