Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

christine smith
christine smith
5,959 Points

C# Basics - Treehouse Defense Game

I have just completed the C# Basics course. I am reviewing the code I wrote for the game. I just don't get how the game works.???

We create the invaders like so: Invader[] invaders = { new Invader(path), new Invader(path), new Invader(path), new Invader(path) };

So does this mean we would have 4 invaders on the same point of the path? When the game is played, the invaders are moved along the path one step but if they all started on the same step at the same time we would always have 4 invaders on the same step? I just don't get it.

Could someone who has understood this course please clarify?

1 Answer

Roman Fincher
Roman Fincher
18,267 Points

It's been awhile since I did this course, but from what I remember and from the Play() method below, I believe you are correct. The invaders do all start at the same point and would all move forward to the same point on the the map after each "round" (or execution of the while loop). Granted, they do so one at a time because of the foreach loop on the invaders, but it would look similar to if they all moved at once (because they can't be neutralized before the rest of the invaders get a chance to move).

I suppose the variability is introduced because the towers have a probabilistic chance of success, and that's what makes this work differently than if we had just one invader (instead of four). Two may be hit and the other two may not, for instance. The more invaders you have, the more likely one of them will be able to make it to the end, even if they are moving as a block rather than individuals.

You can certainly change this behavior, and it's a great idea to do that! For instance, you could implement it in such a way that:

  • different invaders had different step sizes
  • the Play method would only allow one invader to advance from (0,2) to (1,2) per turn
  • invaders' move method would not allow two invaders to occupy one space (that is, if an invader is already in the next space, don't move)
  • invaders could follow different paths
  • towers had the chance to fire on invaders within the foreach loop moving the invaders (then an invader might be killed before the next one can move)
  • invaders could have a probability of either moving or not as part of the move method

And many more. That's the idea though, to give you a great starting place from which to make the game more and more complex and interesting!

Overall, this course mainly looking to introduce important concepts of objects in C#, so I think it's understandable that the mechanics of the Play() method are relatively simple. I would encourage you to get started on intermediate C#! That course takes these initial concepts to amazing new places. You'll see all sorts of ways you can make this game more fun.

        public bool Play()
        {
            int remainingInvaders = _invaders.Length;
            while(remainingInvaders > 0)
            {
                foreach(Tower tower in Towers)
                {
                    tower.FireOnInvaders(_invaders);
                }

                remainingInvaders = 0;
                foreach(Invader invader in _invaders)
                {
                    if(invader.IsActive)
                    {
                        invader.Move();

                        if(invader.HasScored)
                        {
                            return false;
                        }

                        remainingInvaders++;
                    }
                }
            }
            return true;
christine smith
christine smith
5,959 Points

Thank you so much for clarifying this for me. I get it now. I was getting confused with how the game was supposed to be being played! You have really helped me to understand. Much appreciated.