O'Reilly Forums: Determining Which Invader Can Return Fire - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Determining Which Invader Can Return Fire

#1 User is offline   kevin2014 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 19-May 14

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
0

#2 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 804
  • Joined: 08-October 08
  • 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
0

#3 User is offline   kevin2014 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 19-May 14

Posted 22 May 2014 - 03:30 AM

View PostAndrewStellman, on 21 May 2014 - 07:01 AM, said:

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users