diff options
author | uzvkl <dennis.kobert@student.kit.edu> | 2019-06-11 23:05:52 +0200 |
---|---|---|
committer | uzvkl <dennis.kobert@student.kit.edu> | 2019-06-11 23:05:52 +0200 |
commit | e6181c24124d97f2fbc932b8a68311e625463156 (patch) | |
tree | c1f097c344ca266b7941c9668590b0fd35c7870a /dsa/DSALib/Auxiliary/Dice.cs | |
parent | 2490ad5d31fe2ac778ff9303776f0e91f47a2862 (diff) |
Move dsa related stuff to subfolder
Diffstat (limited to 'dsa/DSALib/Auxiliary/Dice.cs')
-rw-r--r-- | dsa/DSALib/Auxiliary/Dice.cs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/dsa/DSALib/Auxiliary/Dice.cs b/dsa/DSALib/Auxiliary/Dice.cs new file mode 100644 index 0000000..0bfabeb --- /dev/null +++ b/dsa/DSALib/Auxiliary/Dice.cs @@ -0,0 +1,45 @@ +using System; +using System.Linq; + +namespace DSALib.Auxiliary +{ + public static class Dice // roll it! + { + private static readonly Random Rnd = new Random(); + + public static int Roll(int d = 20) + { + return Rnd.Next(d) + 1; + } + + public static int Roll(string input) + { + var strings = input.ToLower().Split(new[] {'w', 'd'}, 2, StringSplitOptions.RemoveEmptyEntries).ToList(); + + + if (strings.Count != 2) + throw new ArgumentException($"{input}: does not satisfy the format requirements( dice count (d|w) die size)"); + + var count = Convert.ToInt32(strings[0]); + var d = Convert.ToInt32(strings[0]); + + return Roll(count, d); + } + + public static int Roll(int count, int d) + { + if (d <= 0 || count <= 0) return 0; + + var sum = 0; + for (var i = 0; i < Math.Abs(count); i++) + { + var roll = Roll(d); + sum += roll; + } + + sum *= Math.Abs(count) / count; + + return sum; + } + } +}
\ No newline at end of file |