Finish take_tiles market implementation

Co-authored-by: Daniel Løvbrøtte Olsen <danio@pvv.ntnu.no>
Co-authored-by: Adrian Gunnar Lauterer <adriangl@pvv.ntnu.no>
This commit is contained in:
Eirik Witterso 2024-02-24 21:55:55 +01:00 committed by Daniel Olsen
parent bc0f0444cc
commit a71e5b180d
1 changed files with 9 additions and 8 deletions

View File

@ -316,7 +316,7 @@ impl GameState {
(c, s, _) if self.get_color(s, c) == 0 => {
return Err(MoveErr::Color("Source does not contain that color"))
}
(_, Source::Factory(f), _) if f >= self.factories.len() => {
(_, Source::Factory(f), _) if f > self.factories.len() => {
return Err(MoveErr::Src("Not a valid factory"))
}
(_, _, Destination::PatternLine(l))
@ -400,10 +400,11 @@ impl GameState {
self.market.start -= 1;
player.floor.start += 1;
}
let _ = self.market.add_color(color, -(amount as isize));
}
Source::Factory(f) => {
let factory = &mut self.factories[f];
factory.add_color(color, -(amount as isize)).unwrap();
let _ = factory.add_color(color, -(amount as isize));
self.market += *factory;
factory.clear();
}
@ -538,13 +539,17 @@ impl TileSetWithStart {
Color::Blue => self.blue,
Color::Yellow => self.yellow,
Color::Red => self.red,
Color::Black => self.white,
Color::White => self.black,
Color::Black => self.black,
Color::White => self.white,
}
}
fn add_color(&mut self, color: Color, n: isize) -> Result<(), &'static str> {
match color {
Color::Start if n == 1 && self.start == 0 => {
self.start = usize::checked_add_signed(self.white, n).ok_or("would overflow")?
}
Color::Start => return Err("Tried to add more than 1 start tile to TileSet"),
Color::Blue => {
self.blue = usize::checked_add_signed(self.blue, n).ok_or("would overflow!")?
}
@ -560,10 +565,6 @@ impl TileSetWithStart {
Color::White => {
self.white = usize::checked_add_signed(self.white, n).ok_or("would overflow")?
}
Color::Start if n == 1 && self.start == 0 => {
self.start = usize::checked_add_signed(self.white, n).ok_or("would overflow")?
}
Color::Start => return Err("Tried to add more than 1 start tile to TileSet"),
}
Ok(())