Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Most common special - help needed

Discussion in 'Scripting' started by rukz, Jun 26, 2014.

  1. rukz

    rukz

    Joined:
    Jun 26, 2014
    Posts:
    1
    I can't get it to calculate the most common special attack used in this card game thing i am building for an assignment.

    What i am trying to do is it checks the counters of each time a special attack was used and whatever is the highest .. change it to the current most common spec.

    At the moment, mostCommonSpec just stays as SpecialAttackTypeCS.none (another script which just holds the 4 attacks none, poison, vampire, regen.)
    Code (csharp):
    1.  
    2. public void PrepareCalc()
    3.     {
    4.         totalAttack = 0;
    5.         cardCount = 0;
    6.         minHealth = 10; // minHealth at a higher value so it could round down with the HP variable for lowest HP.
    7.         maxSurvive = 10; // need maxSurvive higher than actual value so it hits the top card when checking.
    8.         specialCount = 0;
    9.         highestAttack = 0;
    10.         totalHp = 10;
    11.         regenCount = 0;
    12.         vampireCount = 0;
    13.         regenCount = 0;
    14.  
    15.         SpecialAttackTypeCS mostCommonSpec = SpecialAttackTypeCS.None;
    16.          
    17.         bestCardName = "Predator";
    18.     }
    19.  
    Code (csharp):
    1.  
    2.         if (spcType == SpecialAttackTypeCS.Poison)
    3.                 {
    4.                     specialCount++;
    5.                     poisonCount++;
    6.                 }
    7.  
    8.         if (spcType == SpecialAttackTypeCS.Regen)
    9.                 {
    10.                     specialCount++;
    11.                     regenCount++;
    12.                 }
    13.  
    14.         if (spcType == SpecialAttackTypeCS.Vampire)
    15.                 {
    16.                     specialCount++;
    17.                     vampireCount++;
    18.                 }
    19.  
    Code (csharp):
    1.  
    2.  
    3. public void FinishCalc()
    4.         {
    5.  
    6.  
    7.  
    8.             if ((regenCount > poisonCount)&&(regenCount > vampireCount))
    9.                 {
    10.             SpecialAttackTypeCS mostCommonSpec = SpecialAttackTypeCS.Regen;  
    11.                 }
    12.      
    13.             if ((poisonCount > regenCount)&&(poisonCount > vampireCount))
    14.                 {
    15.             SpecialAttackTypeCS mostCommonSpec = SpecialAttackTypeCS.Poison;  
    16.                 }
    17.      
    18.             if ((vampireCount > regenCount)&&(vampireCount > poisonCount))
    19.                 {
    20.             SpecialAttackTypeCS mostCommonSpec = SpecialAttackTypeCS.Vampire;  
    21.                 }
    22.         }
    23.  
    Code (csharp):
    1.  
    2. public SpecialAttackTypeCS GetMostCommonSpecial()
    3.         {
    4.         return mostCommonSpec;
    5.         }
    6.  
     
  2. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    well... there are 3 possible reasons
    1) regenCount = vampireCount
    2) regenCount = poisonCount
    3) poisonCount = vampireCount

    and also I don't see poisonCount retested to zero before you do your calculations.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    mostCommonSpec is scoped to within the function in all those pasted chunks of code... so when the function is over its gone.

    I assume you have a declaration within the class somewhere which hasn't been pasted in here, but then you are declaring a new variable of that name within the functions and using that one within the functions.
     
  4. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Yeh that too, and declared many times in same function. Code is a mess...
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I would handle this differently.
    I'd have list of classes like
    Code (CSharp):
    1. class Thing {
    2.   string name //Poison, Regen, or Vampire
    3.   int count
    4. }
    5.  
    6. List<Thing> useCount = new List<Thing>
    7. useCount.Add(new Thing());
    8. useCount[0].name = vampire
    9. ....
    10.  
    11. // Then to increase the counts, do
    12. useCount[0].count++ //to increase element 0 (vampire)
    Then just sort the list by count, and get the new 0'th elements name
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    wouldn't that involve writing a custom (*struggles for the right term*) enumerator thing to get and compare the counts of each class in the list to perform the sort? that might be outside the scope of the assignment.
     
  7. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Of course. But not so hard if sorting only within one parameter. :)
    Code (CSharp):
    1.         useCount.Sort(delegate(Thing x, Thing y) {
    2.             return - x.count.CompareTo(y.count);
    3.         });
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Or let Thing implement IComparer<T>