Jump to content


Photo

Determining Which Invader Can Return Fire


  • Please log in to reply
2 replies to this topic

#1 kevin2014

kevin2014

    New Member

  • Members
  • Pip
  • 2 posts

Posted 19 May 2014 - 05:17 AM

Hello All

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 ?


Thanks
Kevin

#2 AndrewStellman

AndrewStellman

    Andrew Stellman

  • O'Reilly Author
  • PipPipPipPipPipPipPipPipPipPipPip
  • 832 posts
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

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!
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com

#3 kevin2014

kevin2014

    New Member

  • Members
  • Pip
  • 2 posts

Posted 22 May 2014 - 03:30 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!


Dear Andrew

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.
kevin




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users