fix some false assumptions about tile IDs (0 is reserved!)

This commit is contained in:
Max Bradbury 2020-06-23 16:46:49 +01:00
parent 62f488c24e
commit b88f69af17
1 changed files with 14 additions and 7 deletions

View File

@ -360,7 +360,11 @@ impl Game {
/// if current tile IDs are [0, 1, 2] the result will be `3` /// if current tile IDs are [0, 1, 2] the result will be `3`
#[inline] #[inline]
pub fn new_tile_id(&self) -> String { 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 /// adds a tile safely and returns the new tile ID
@ -456,13 +460,16 @@ mod test {
#[test] #[test]
fn test_new_tile_id() { fn test_new_tile_id() {
// default tile has an id of 10 ("a"), so 0 is available // default tile has an id of 10 ("a"), and 0 is reserved
assert_eq!(crate::mock::game_default().new_tile_id(), "0".to_string()); 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 game = crate::mock::game_default();
let mut tiles: Vec<Tile> = Vec::new(); let mut tiles: Vec<Tile> = Vec::new();
for n in 0..9 { // 0 is reserved; upper bound is non-inclusive
for n in 1..10 {
if n != 4 { if n != 4 {
let mut new_tile = crate::mock::tile_default(); let mut new_tile = crate::mock::tile_default();
new_tile.id = format!("{}", n).to_string(); new_tile.id = format!("{}", n).to_string();
@ -474,7 +481,7 @@ mod test {
assert_eq!(game.new_tile_id(), "4".to_string()); 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(); let mut new_tile = crate::mock::tile_default();
new_tile.id = "4".to_string(); new_tile.id = "4".to_string();
@ -487,10 +494,10 @@ mod test {
fn test_add_tile() { fn test_add_tile() {
let mut game = crate::mock::game_default(); let mut game = crate::mock::game_default();
let new_id = game.add_tile(crate::mock::tile_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); assert_eq!(game.tiles.len(), 2);
let new_id = game.add_tile(crate::mock::tile_default()); 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); assert_eq!(game.tiles.len(), 3);
} }