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 Playing the Game

Tony Lawrence
Tony Lawrence
3,056 Points

My game never Tells me who won or who lost.

I'm having trouble executing my TreehouseDefense file. For some reason, it looks like the code keeps running for at least 1 minutes, and I know it should be done in at least a second. I have to manually break out of my program with Ctrl Z.

Here's the snapshot link of my codes for anyone to look over: https://w.trhou.se/djra6hhu94

1 Answer

Steven Parker
Steven Parker
229,732 Points

:point_right: There appears to be a logic problem in Level.cs.

You have a loop that runs while remainingInvaders is greater than zero, but it does not appear that it ever gets decremented. On the other hand, any time an invader moves but does not score, the number of remaining invaders gets increased by 1.

Did you intend to recount the remaining invaders in the invader loop? If so, perhaps you meant to reset it to zero before the loop starts.

Tony Lawrence
Tony Lawrence
3,056 Points

Okay. I checked my Level.cs and noticed I didn't reset 'remainingInvaders' to zero. I added that line in there and now the game gives me the statement saying that I won. Thank you.