I wasn't really sure how to address this question, so I will start with "pseudo" code:
List<Player> players = new List<Player>();
players.Add(p1);
players.Add(p2);
players.Add(p3);
while(true) //infinite loop
{
Player nextOne = players.Get(); // It will always get next player
}
I'm looking is a collection/list/buffer/array etc. that will have some kind of "Get" method, which return next item, always. And when it reach the end, it will return first item.
Great example would be a card game, like poker: I have list of players.. I always return next player, like in the circle.
My question is: is there already, somewhere in C#/.NET, such collection implemented?
If not, is writing an extension method for List the best solution?
You can us the following method to create a sequence that is the infinite repetition of another (presumably finite) sequence.
//TODO consider coming up with a better name
public static IEnumerable<T> IterateInfinitely<T>(
this IEnumerable<T> sequence)
{
while(true)
foreach(var item in sequence)
yield return item;
}
Using this you can now write:
foreach(var player in players.IterateInfinitely())
{
//TODO do stuff
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With