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# C# Objects Loops and Final Touches Putting It All Together

Brooks G
Brooks G
5,284 Points

Do all of the invaders occupy the same MapLocation at once once the "foreach" loop of invaders is finished (in Play)?

On each iteration of the while loop, we reach the "foreach" loop of invaders, and then we iterate through the "foreach" loop, running the code within for each invader. Each invader has an initial location of (0, 2). The first invader moves once to (1, 2), checks if it's at the end of the path, and then we add to our "invadersRemaining" tally before moving onto the next invader in the loop. The next invader also starts on (0,2) and moves to (1, 2). And same for the rest of the invaders until we're done with the loop.

At the end of the "foreach" loop in the first iteration of the while loop, it seems as though every invader would occupy location (1, 2) -- all stacked on top of each other!

I just want to make sure I'm correct about that & not misinterpreting something. I think that's right, but I suppose it seems sort of odd -- perhaps I was expecting them to form a forward-moving line rather than a blob!

1 Answer

Steven Parker
Steven Parker
231,236 Points

Your understanding seems correct, but you also have an interesting idea for variation on the game.

You could limit each cell to a single occupant, so each invader could move only if the one ahead has gone forward or been destroyed. That would certainly give an advantage to the defense towers!

Brooks G
Brooks G
5,284 Points

Cheers, Steven! I think you've answered just about every question I've posted on here -- I really appreciate all of the help!

Gonna see if I can't figure out a way to make a variation of the game doing just that :)