Advertisement
Guest User

Untitled

a guest
Dec 13th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private Dictionary<string, int> gQualDict = new Dictionary<string, int>();
  2.  
  3. class qualityWithStatus {
  4.     public int quality;
  5.     public bool isAvailable;
  6.     public qualityWithStatus(int ex_quality)
  7.     {
  8.         quality = ex_quality;
  9.         isAvailable = true;
  10.     }
  11. }
  12. ...
  13. if (gQualDict.Count > 0 && gQualDict.Values.Aggregate((a, b) => b + a) >= 40)
  14. {
  15.     string resultMessage = string.Empty;
  16.     List<string> combinationsFound = new List<string>();
  17.     Random rnd = new Random();
  18.     for (int iterator = 0; iterator < 100000; iterator++)
  19.     {
  20.         List<qualityWithStatus> tempQualList = new List<qualityWithStatus>();
  21.         foreach (int qualValue in gQualDict.Values)
  22.             tempQualList.Add(new qualityWithStatus(qualValue));
  23.         int currentSumm = 0;
  24.         //prepare list of numbers like 0,1,2,3,4,5,6,7,8,9,...    
  25.         List<int> numberList = Enumerable.Range(0, tempQualList.Count).ToList();
  26.         do
  27.         {                                      
  28.             //get random number from 0 to count of prepared list numbers                  
  29.             int randomValue = numberList[rnd.Next(0, numberList.Count)];
  30.             //remove this number from prepared list so it will not appear again
  31.             numberList.Remove(randomValue);
  32.             if (tempQualList[randomValue].isAvailable)
  33.             {
  34.                 currentSumm += tempQualList[randomValue].quality;
  35.                 tempQualList[randomValue].isAvailable = false;
  36.             }
  37.         }
  38.         while (currentSumm < 40);
  39.         if (currentSumm == 40)
  40.         {
  41.             gQualDict.Clear();
  42.             enableQualityCombinationModeToolStripMenuItem_Click(enableQualityCombinationModeToolStripMenuItem, null);
  43.             resultMessage += formQualityAnswer(tempQualList) + Environment.NewLine;
  44.             break;
  45.         }
  46.         else if (currentSumm < 43)
  47.         {
  48.             string answer = formQualityAnswer(tempQualList);
  49.             if (!combinationsFound.Contains(answer))
  50.             {
  51.                 combinationsFound.Add(answer);
  52.                 resultMessage += answer + " (" + currentSumm + ")" + Environment.NewLine;
  53.                 if (combinationsFound.Count > 7)
  54.                     break;
  55.             }
  56.         }
  57.     }
  58.  
  59. private string formQualityAnswer(List<qualityWithStatus> ex_qualList)
  60. {
  61.     List<int> answer = new List<int>();
  62.     foreach (qualityWithStatus qws in ex_qualList)
  63.         if (!qws.isAvailable)
  64.             answer.Add(qws.quality);
  65.     answer.Sort();
  66.     return string.Join(" + ", answer);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement