开发者

Can anyone tell me if this is possible to do in Linq?

开发者 https://www.devze.com 2022-12-26 15:36 出处:网络
So I have been working and trying for a couple of hours. I am creating a BubbleBreaker type of game. I Can do it using for loop or while but I am trying to also get practice in using Linq. Here is wha

So I have been working and trying for a couple of hours. I am creating a BubbleBreaker type of game. I Can do it using for loop or while but I am trying to also get practice in using Linq. Here is what I am trying to do in pesudo code.

Each Bubble has a Column and Row Property. If The bubble subtracts one from its Column property and finds the same Color bubble, it should Select it then Subtract -1 and see if two bubbles away there is also a same color bubble. If there is then subtract -2 and开发者_如何转开发 so on. So What I am trying to do is

var test= _theBubbles.TakeWhile((i, s) => i.BubbleColor== bubble.BubbleColor)//Then somehow tell it to do bubble.Column-s

and then Subtract s from bubble.Column So the Idea is to keep looking down the column till the bubble isnt the same


Let's assume that _theBubbles is a list of all of the bubbles on the game board. In that case, givena particular starting bubble you could use the following query to find all bubbles below the given one that are the same color (I assume that the bottom-most row is row zero):

var bubble = ... // some known bubble
var sameColorBubblesBelowTheBubble =
     _theBubbles.Where( b => b.Column == bubble.Column &&
                        b.Row < bubble.Row )
                .OrderByDescending( b => b.Row )
                .TakeWhile( (b,i) =>  b.BubbleColor == bubble.BubbleColor );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号