i'm trying to shuffle or randomize a LinkedList of images in this case but the way i've set it up it seems to go on indefinetely
the shuffling is fairly simple, you have the list, note the last entry, then take the first entry and put it at a random spot in the list, and then the next first entry, etc. until the top entry is the entry you noted as the last, you put that entry in a random place in the list and the list is shuffled.
here's my code:
class ShuffleClass
{
private LinkedList<Image> library;
private Image lastCard;
private Image topCard;
private Random rng;
private int place;
private LinkedListNode<Image> node;
public LinkedList<Image> shuffle(LinkedList<Image> library)
{
this.library = library;
lastCard = library.Last.Value;
rng = new Random();
while (library.First.Value != lastCard)
{
topCard = library.First.Value;
library.RemoveFirst();
place = rng.Next(1,library.Count+1);
if (place == library.Count)
{
library.AddBefore(library.Last, topCard);
}
else
{
node = library.Find(library.ElementAt(place));
library.AddBefore(node, topCard);
}
}
topCard = library.First.Value;
library.RemoveFirst();
place = rng.Next(0,library.Count+1);
if(place == library.Count)
{
library.AddBefore(library.Last, topCard);
}
else
{
node = library.Find(library.ElementAt(place));
library.AddBefore(node, topCard);
}
return library;
}
}
Aucun commentaire:
Enregistrer un commentaire