From b88f69af1719bdc4514a121a426bdf9e90ac101b Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Tue, 23 Jun 2020 16:46:49 +0100 Subject: [PATCH] fix some false assumptions about tile IDs (0 is reserved!) --- src/game.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/game.rs b/src/game.rs index 7f714e3..b5ef240 100644 --- a/src/game.rs +++ b/src/game.rs @@ -360,7 +360,11 @@ impl Game { /// if current tile IDs are [0, 1, 2] the result will be `3` #[inline] pub fn new_tile_id(&self) -> String { - new_unique_id(self.tile_ids()) + // don't allow `0` - this is a reserved ID + // todo new_sprite_id will need to avoid A too + let mut ids = self.tile_ids(); + ids.push("0".to_string()); + new_unique_id(ids) } /// adds a tile safely and returns the new tile ID @@ -456,13 +460,16 @@ mod test { #[test] fn test_new_tile_id() { - // default tile has an id of 10 ("a"), so 0 is available - assert_eq!(crate::mock::game_default().new_tile_id(), "0".to_string()); + // default tile has an id of 10 ("a"), and 0 is reserved + assert_eq!(crate::mock::game_default().new_tile_id(), "1".to_string()); + + // for a game with a gap in the tile IDs, check the gap is used let mut game = crate::mock::game_default(); let mut tiles: Vec = Vec::new(); - for n in 0..9 { + // 0 is reserved; upper bound is non-inclusive + for n in 1..10 { if n != 4 { let mut new_tile = crate::mock::tile_default(); new_tile.id = format!("{}", n).to_string(); @@ -474,7 +481,7 @@ mod test { assert_eq!(game.new_tile_id(), "4".to_string()); - // fill in the space created above, and test that tile IDs get sorted + // fill in the space created above, then test that tile IDs get sorted let mut new_tile = crate::mock::tile_default(); new_tile.id = "4".to_string(); @@ -487,10 +494,10 @@ mod test { fn test_add_tile() { let mut game = crate::mock::game_default(); let new_id = game.add_tile(crate::mock::tile_default()); - assert_eq!(new_id, "0".to_string()); + assert_eq!(new_id, "1".to_string()); assert_eq!(game.tiles.len(), 2); let new_id = game.add_tile(crate::mock::tile_default()); - assert_eq!(new_id, "1".to_string()); + assert_eq!(new_id, "2".to_string()); assert_eq!(game.tiles.len(), 3); }