Putting It All Together (2)
Now, we want to loop through all the remainingInvaders. Then tower could firing on the invaders.
public bool Play()
{
// run until all invaders are neutralized or reach the end of map
int remainingInvaders = _invaders.Length;
while (remainingInvaders>0)
{
// each tower has opportunity to fire on invaders
foreach(Tower tower in towers)
{
tower.FireOnInvaders(_invaders);
}
// count and move the invaders that are still active
}
}
After that, we would like to count how much invaders still alive.
public bool Play()
{
// run until all invaders are neutralized or reach the end of map
int remainingInvaders = _invaders.Length;
while (remainingInvaders>0)
{
// each tower has opportunity to fire on invaders
foreach(Tower tower in towers)
{
tower.FireOnInvaders(_invaders);
}
// count and move the invaders that are still active
remainingInvaders = 0;
foreach(Invader invader in _invaders)
{
}
}
}
In each remaing and active invader, we want them to move and check if they have reach the end of map.
public bool Play()
{
// run until all invaders are neutralized or reach the end of map
int remainingInvaders = _invaders.Length;
while (remainingInvaders>0)
{
// each tower has opportunity to fire on invaders
foreach(Tower tower in towers)
{
tower.FireOnInvaders(_invaders);
}
// count and move the invaders that are still active
remainingInvaders = 0;
foreach(Invader invader in _invaders)
{
if (invader.IsActive)
{
invader.Move();
if(invader.HasScored)
{
return false;
}
remainingInvaders++;
}
}
}
return true;
}