Exercises
To help Steve apply recursion concepts to his tower defense game, Julia prepared three coding challenges. Let’s see if you can help Steve solve them!
Exercise 1
Steve’s game has a hierarchical structure of enemy waves, where each wave can contain both individual enemies and sub-waves. Implement a recursive function, CountAllEnemies, that navigates through a Wave object (which can contain both Enemy objects and Wave objects) and returns the total count of enemies found within that wave, including all its sub-waves:
public interface IWaveContent {}
public class Enemy : IWaveContent
{
     public string Name { get; set; }
}
public class Wave : IWaveContent
{
     public List<IWaveContent> Contents { get; set; } = new();
}
// Implement this method
int CountAllEnemies(Wave wave)
{
     // Your recursive logic here
}			Test your method with a Wave containing a mix of Enemy objects...
 
                                             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
     
         
                 
                 
                 
                 
                 
                 
                 
                 
                