Advertisement
Pro_Unit

RandomStack

Nov 21st, 2020
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace GameCore.Utilities
  5. {
  6.     using Extentions;
  7.  
  8.     [Serializable]
  9.     public class RandomStack<T>
  10.     {
  11.         private IEnumerable<T> RandCollection;
  12.         private IEnumerable<T> StarCollection;
  13.         private Stack<T> Stack;
  14.         private T LastRandom;
  15.         public RandomStack(params T [] colection)
  16.         {
  17.             RandCollection = new List<T>(colection);
  18.             StarCollection = colection;
  19.         }
  20.         public RandomStack(IEnumerable<T> colection)
  21.         {
  22.             RandCollection = new List<T>(colection);
  23.             StarCollection = colection;
  24.         }
  25.         public T Get()
  26.         {
  27.             if (StarCollection.Count() == 1)
  28.                 return StarCollection.ElementAt(0);
  29.             if (RandCollection.Count() == 0)
  30.                 RandCollection = new List<T>(StarCollection);
  31.             T rand;
  32.             do
  33.             {
  34.                 rand = RandCollection.GetRandom();
  35.             }
  36.             while (rand.Equals(LastRandom));
  37.  
  38.             Remove(rand);
  39.             LastRandom = rand;
  40.             return rand;
  41.         }
  42.  
  43.         private void Remove(T rand)
  44.         {
  45.             List<T> list = RandCollection.ToList();
  46.             list.Remove(rand);
  47.             RandCollection = new List<T>(list);
  48.             list.Clear();
  49.         }
  50.  
  51.         public void Log()
  52.         {
  53.             UnityEngine.Debug.Log("StarCollection");
  54.             StarCollection.Log();
  55.             UnityEngine.Debug.Log("RandCollection");
  56.             RandCollection.Log();
  57.         }
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement