1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using System;
- using System.Collections.Generic;
- /// <summary>
- /// Shuffle bag based off http://gamedev.tutsplus.com/tutorials/implementation/shuffle-bags-making-random-feel-more-random/
- /// </summary>
- public class ShuffleBag<T>
- {
- private Random random = new Random();
- private List<T> data;
- private T currentItem;
- private int currentPosition = -1;
-
- private int Capacity { get { return data.Capacity; } }
- public int Size { get { return data.Count; } }
-
- public ShuffleBag(int initCapacity)
- {
- data = new List<T>(initCapacity);
- }
- public void Add(T item, int amount = 1)
- {
- for (int i = 0; i < amount; i++)
- {
- data.Add(item);
- }
-
- currentPosition = Size - 1;
- }
- public T Next()
- {
- if (currentPosition < 1)
- {
- currentPosition = Size - 1;
- currentItem = data[0];
-
- return currentItem;
- }
-
- var pos = random.Next(currentPosition);
-
- currentItem = data[pos];
- data[pos] = data[currentPosition];
- data[currentPosition] = currentItem;
- currentPosition--;
-
- return currentItem;
- }
- }
|