Last active
June 13, 2016 19:52
-
-
Save Noofbiz/157dc660f1cbdfcf845ddad730e8b8f7 to your computer and use it in GitHub Desktop.
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 Microsoft.VisualStudio.TestTools.UnitTesting; | |
/// <summary> | |
/// Crit Calculator for https://www.reddit.com/r/dailyprogrammer/comments/4nvrnx/20160613_challenge_271_easy_critical_hit/ | |
/// wrote a caclculator for the crit calculator, and did some unit tests for it as well. | |
/// </summary> | |
namespace CritCalculator | |
{ | |
class TestCritCalculator | |
{ | |
static void Main(string[] args) | |
{ | |
CritCalculator c = new CritCalculator(); | |
c.critCalcTest(); | |
Console.ReadKey(true); | |
} | |
} | |
class CritCalculator | |
{ | |
public int intDiceSides; | |
public int intEnemyHealth; | |
public double probabilityOfDeath() | |
{ | |
int intTimesRoll = intEnemyHealth / intDiceSides; | |
int intLastRoll = intEnemyHealth % intDiceSides; | |
double dblProbablity = 1.0 / intDiceSides; | |
double dblLastRollProb = (intLastRoll == 0) ? 1.0 : ((double)(intDiceSides + 1 - intLastRoll) / intDiceSides); | |
return Math.Pow(dblProbablity, intTimesRoll) * dblLastRollProb; | |
} | |
public void critCalcTest() | |
{ | |
try | |
{ | |
intDiceSides = 4; | |
intEnemyHealth = 1; | |
Assert.AreEqual(1, probabilityOfDeath()); | |
intDiceSides = 4; | |
intEnemyHealth = 4; | |
Assert.AreEqual(0.25, probabilityOfDeath()); | |
intDiceSides = 4; | |
intEnemyHealth = 5; | |
Assert.AreEqual(0.25, probabilityOfDeath()); | |
intDiceSides = 4; | |
intEnemyHealth = 6; | |
Assert.AreEqual(0.1875, probabilityOfDeath()); | |
intDiceSides = 1; | |
intEnemyHealth = 10; | |
Assert.AreEqual(1, probabilityOfDeath()); | |
intDiceSides = 100; | |
intEnemyHealth = 200; | |
Assert.AreEqual(0.0001, probabilityOfDeath()); | |
intDiceSides = 8; | |
intEnemyHealth = 20; | |
Assert.AreEqual(0.009765625, probabilityOfDeath()); | |
Console.WriteLine("All Tests Passed!"); | |
} | |
catch (AssertFailedException) | |
{ | |
Console.WriteLine("Assert Failed!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment