summaryrefslogtreecommitdiff
path: root/DSALib/Auxiliary/Dice.cs
diff options
context:
space:
mode:
authorDennis Kobert <d-kobert@web.de>2019-05-19 17:58:42 +0200
committerDennis Kobert <d-kobert@web.de>2019-05-19 17:58:42 +0200
commit2ab4051c6fe720dc47e99b0c305a0d779ee02d51 (patch)
tree9510ddbb174a54474934adf7991a5ba2aa39f818 /DSALib/Auxiliary/Dice.cs
parentc4d046858e0822b7c2c540ac2368b2c0e88e7a26 (diff)
Moved Gamelogic to DSALib
Diffstat (limited to 'DSALib/Auxiliary/Dice.cs')
-rw-r--r--DSALib/Auxiliary/Dice.cs43
1 files changed, 43 insertions, 0 deletions
diff --git a/DSALib/Auxiliary/Dice.cs b/DSALib/Auxiliary/Dice.cs
new file mode 100644
index 0000000..3dd6562
--- /dev/null
+++ b/DSALib/Auxiliary/Dice.cs
@@ -0,0 +1,43 @@
+using System;
+using System.Linq;
+
+namespace DSACore.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();
+ var count = Convert.ToInt32(strings[0]);
+ var d = Convert.ToInt32(strings[0]);
+
+ if (strings.Count != 2)
+ throw new ArgumentException($"{input}: erfüllt nicht die Formatvogaben( Anzahl d Augenzahl)");
+
+ return Roll(count, d);
+ }
+
+ public static int Roll(int count, int d)
+ {
+ if (d <= 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