c# - Using LINQ to isolate a particular object from a list of objects -


I am trying my hand in cloning invaders. There are 30 aliens in the 5 x 6 matrix on the screen. I need to give the ability to shoot the most foreigners below. I am using LINX in 5 groups based on LINQ to location.X and then descending group. Then I have to choose a group (which gives me 5 groups) and choose the first foreigner in the group and use it; Coordinate to fire a shot.

My code works below, okay, but aliens on any line are happily firing shots - not only at the bottom Pl look at my code below and tell me what's wrong (R = an example of Random class, all aliens are in the inventory list).

  {inv.location.X by invgroup orderby Invgroup.Key descending selection invgroup} .Oolist (); Var invfirst = query [r.Next (query.Count)]. first (); InvaderShots.Add (new shot (new point (invfirst.Area.X + invfirst.Area.Width / 2, invfirst.Area.Y + invfirst.Area.Height + 5), display, shot.Directation.Down)); }  

Edit:

Resolve. Now it works like Dawood B. has pushed me in the right direction.

The final code below. Certainly, due to a large number of grouping / sorting, light needs some improvement. If someone has something creative to say on this, then I am all ears (or eyes, in this case). Thanks for all the people.

  list & lt; Siege & gt; First Indder = Invaders. Group (inv = & Inv; inv.Location.X). Select (G => G. Order Bearing (Event => inv.Location.Y)). On Element (R. Next (5)) ToList (); Firstinvader.Sort (comparerByLocation); Invader items = FirstWaidder [First Winvist. Number-1]; If (attacker shots.kount & lt; 2) {invaderShots.Add (new shot (new point (item.Area.X + item.Area.Width / 2, item.Area.Y + item.Area.Height / 2) , Displayed, shot.directation.down)); } And {return; }  

  list & lt; Invaders & gt; FirstInvaders = Invaders .GroupBy (inv = & gt; inv.Location.X). Select (g = & gt; g. Order Wednesdayending (inv = & gt; inv.Location.Y). First ()) .Ollist (); Invader's Shooter = First Invader [R. Next (first inwarder count)];  

Comments