Determining Which Invader Can Return Fire
2 replies to this topic
Posted 19 May 2014 - 05:17 AM
Still on 2nd Edition , finally reached the game of invaders. Stuck badly on one thing - how to determine which invader returns fire.
In the book it says :
If it gets past both tests, it can use LINQ to group the invaders by
their Location.X and sort them descending. Once it’s
got those groups, it can choose a group at random, and use
its Last() method to find the invader at the bottom of the
column. All right, now you’ve got the shooter....
That's my problem right there - how on earth do I chose group at random in LINQ ?
Posted 21 May 2014 - 07:01 AM
I bet you're overthinking it! Use two separate LINQ queries, the first to get the groups, then choose one from the result, and then second to get the bottom invader of that group.
BTW, for the third edition we published the Windows Store version of Invaders as an open source project: http://invaders.codeplex.com/ -- if you want spoilers, you can see the ReturnFire() method in Model\InvadersModel.cs. But it's more satisfying to do it all on your own!
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
Posted 22 May 2014 - 03:30 AM
Finally solved it on my own !!! Luckily saw your post after I soled my problem else would have been tempted to cheat.
But I see you have done things much more differently than I have - you code is a lot more professional ( for lack of a better word).
Can learn a lot from your code.
Thanks for answering.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users