Guest User

Untitled

a guest
Aug 9th, 2020
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SpellProbability
  6. {
  7.     public class Program
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             //SpellbookToIndependent();
  12.             IndependentToSpellbook();
  13.         }
  14.  
  15.         /// <summary>
  16.         /// Converts a monster spellbook to a list of independent probabilities
  17.         /// </summary>
  18.         public static void SpellbookToIndependent()
  19.         {
  20.             var chances = new List<float>() { 0.5f, 0.5f };
  21.  
  22.             Console.WriteLine($"Spellbook probabilities: {string.Join(", ", chances.Select(i => PercentFormat(i)))}\n");
  23.  
  24.             var castChance = GetProbabilityAny(chances);
  25.  
  26.             Console.WriteLine($"Total casting chance: {PercentFormat(castChance)}\n");
  27.  
  28.             for (var i = 0; i < chances.Count; i++)
  29.             {
  30.                 var prevChanceNone = i > 0 ? GetProbabilityNone(chances.GetRange(0, i)) : 1.0f;
  31.  
  32.                 Console.WriteLine($"Chance of casting spell #{i + 1}: {PercentFormat(chances[i] * prevChanceNone)}");
  33.             }
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Converts a list of independent probabilities to monster spellbook format
  38.         /// </summary>
  39.         public static void IndependentToSpellbook()
  40.         {
  41.             var chances = new List<float>() { 0.25f, 0.25f, 0.25f, 0.25f };
  42.  
  43.             Console.WriteLine($"Independent probabilities (cannot go above 100% additive): {string.Join(", ", chances.Select(i => PercentFormat(i)))}\n");
  44.  
  45.             var spellbook = new List<float>();
  46.  
  47.             for (var i = 0; i < chances.Count; i++)
  48.             {
  49.                 var prevChanceNone = i > 0 ? GetProbabilityNone(spellbook) : 1.0f;
  50.  
  51.                 spellbook.Add(chances[i] / prevChanceNone);
  52.             }
  53.  
  54.             Console.WriteLine($"Spellbook format: {string.Join(", ", spellbook.Select(i => PercentFormat(i)))}\n");
  55.         }
  56.  
  57.         /// <summary>
  58.         /// Returns the probability of none of the events occurring
  59.         /// from a list of chances
  60.         /// </summary>
  61.         public static float GetProbabilityNone(List<float> chances)
  62.         {
  63.             var probability = 1.0f;
  64.  
  65.             foreach (var chance in chances)
  66.                 probability *= 1.0f - chance;
  67.  
  68.             return probability;
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Returns the probability of any of the events occurring
  73.         /// from a list of chances
  74.         /// </summary>
  75.         public static float GetProbabilityAny(List<float> chances)
  76.         {
  77.             return 1.0f - GetProbabilityNone(chances);
  78.         }
  79.  
  80.         public static string PercentFormat(float percent)
  81.         {
  82.             return $"{Math.Round(percent * 100, 2)}%";
  83.         }
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment