diff --git a/src/game_state/level.rs b/src/game_state/level.rs index 2b6a8f18c04a1f09921efe8bfd058eec0a6f347e..e2426368c8d35414a8180db16f8a2d99774726e7 100644 --- a/src/game_state/level.rs +++ b/src/game_state/level.rs @@ -137,12 +137,26 @@ impl Level { fn add_entity(entity: Tile, board: Board) -> Board { let empty_count = Level::count_empty_tiles(board); - let entity_position = thread_rng().gen_range(0, empty_count); - let entity_x = entity_position / config::BOARD_HEIGHT; - let entity_y = entity_position % config::BOARD_HEIGHT; + let mut entity_position = thread_rng().gen_range(0, empty_count); + let mut board = board.clone(); - board[entity_x][entity_y] = entity; + for x in 0..board.len() { + for y in 0..board[x].len() { + if entity_position > 0 { + match board[x][y] { + Tile::Empty => entity_position -= 1, + _ => () + } + } + if entity_position == 0 { + board[x][y] = entity; + println!("Add {:?} at {:?}, coords: {}, {}", entity, entity_position, x, y); + return board + } + } + } + board }