Exercises
Now that Steve has learned about honest functions, null, and Option types, Julia has prepared some challenges to help him apply these concepts to his tower defense game. Let’s see if you can help Steve solve them!
Exercise 1
Steve’s game needs to fetch tower information reliably. Refactor this function to use an honest return type that clearly indicates when a tower might not be found:
public Tower GetTowerByPosition(Vector2 position) { var tower = _gameMap.FindTowerAt(position); return tower; }
Exercise 2
In the game players can apply power-ups to towers. Refactor this function to ensure it handles null inputs gracefully:
public void ApplyPowerUp(Tower tower, PowerUp powerUp) { tower.ApplyPowerUp(powerUp); _gameState.UpdateTower(tower); }
Exercise 3
Steve wants to provide players with detailed information about the enemies...