Last active
January 15, 2017 18:42
-
-
Save RobertPaulson90/7723d43e6d7fb8397a739deaed9efa53 to your computer and use it in GitHub Desktop.
Probability Calculations... This can definitely be way simplified
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace StatisticCalculations | |
{ | |
public class Program | |
{ | |
// Challenge is to figure out the average chance of three 3dmg random missiles killing 1 target, | |
// when there are 3 possible targets, 2 of which are "killable" (4hp total) and 1 which isn't (>9hp) | |
// It's pretty simple, but if the first 2 missiles hit both 4hp minions, there | |
// is a 66% chance of 1 dying to the last missile. However the first 2 missiles can also target the same 4hp minion. | |
// So what are the odds? | |
private static int _totalGamesCalculated; | |
private static decimal _avgMinionsKilledPerGame; | |
public static bool RunnningSimulations { get; set; } | |
public static void Main(string[] args) | |
{ | |
const int missiles = 3; | |
const int dmg = 3; | |
var renderThread = new Thread(RenderConsole); // TODO: Use Task indead. Gotta learn it first. | |
renderThread.Start(); | |
RunnningSimulations = true; | |
while (RunnningSimulations) | |
{ | |
var list = InitializeObjects(); | |
var rnd = new Random(); | |
for (var i = 0; i < missiles; i++) | |
{ | |
var currentObject = list[rnd.Next(0, list.Count)]; | |
currentObject.DamageMinion(dmg); | |
if (currentObject.Alive == false) | |
{ | |
list.Remove(currentObject); | |
} | |
} | |
var killsThisAttempt = (list.Count - 3) * -1; | |
if (_totalGamesCalculated != 0) | |
{ | |
_avgMinionsKilledPerGame = | |
_avgMinionsKilledPerGame + | |
(killsThisAttempt - _avgMinionsKilledPerGame) / (_totalGamesCalculated + 1); | |
} | |
else | |
{ | |
_avgMinionsKilledPerGame = killsThisAttempt; | |
} | |
_totalGamesCalculated += 1; | |
} | |
} | |
private static void RenderConsole() | |
{ | |
while (true) | |
{ | |
Console.Clear(); | |
Console.WriteLine($@"Total games calculated: {_totalGamesCalculated}"); | |
Console.WriteLine($@"Aberage minions killed per game: {_avgMinionsKilledPerGame}"); | |
Thread.Sleep(500); | |
} | |
} | |
private static List<Minion> InitializeObjects() | |
{ | |
var minion1 = new Minion(4); | |
var minion2 = new Minion(4); | |
var face = new Minion(30); | |
return new List<Minion> { minion1, minion2, face }; | |
} | |
public class Minion | |
{ | |
public int HP { get; set; } | |
public bool Alive { get; set; } | |
public Minion(int hp) | |
{ | |
this.HP = hp; | |
this.Alive = true; | |
} | |
public void DamageMinion(int dmg) | |
{ | |
this.HP -= dmg; | |
if (this.HP <= 0) | |
{ | |
this.Alive = false; | |
} | |
// Console.WriteLine("Minion damaged. HP is: " + HP + " and status is: " + Alive); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment