summaryrefslogtreecommitdiff
path: root/DSACore/Auxiliary/Calculator
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 /DSACore/Auxiliary/Calculator
parentc4d046858e0822b7c2c540ac2368b2c0e88e7a26 (diff)
Moved Gamelogic to DSALib
Diffstat (limited to 'DSACore/Auxiliary/Calculator')
-rw-r--r--DSACore/Auxiliary/Calculator/Argument.cs35
-rw-r--r--DSACore/Auxiliary/Calculator/ISolvable.cs10
-rw-r--r--DSACore/Auxiliary/Calculator/Operator.cs51
-rw-r--r--DSACore/Auxiliary/Calculator/Ops.cs13
-rw-r--r--DSACore/Auxiliary/Calculator/StringSolver.cs183
5 files changed, 0 insertions, 292 deletions
diff --git a/DSACore/Auxiliary/Calculator/Argument.cs b/DSACore/Auxiliary/Calculator/Argument.cs
deleted file mode 100644
index 5ed9ee3..0000000
--- a/DSACore/Auxiliary/Calculator/Argument.cs
+++ /dev/null
@@ -1,35 +0,0 @@
-using System;
-
-namespace DSACore.Auxiliary.Calculator
-{
- /// <summary>
- /// Provides an ISolvable class to save numbers. The class handles Argument checking and conversion from string to int.
- /// </summary>
- public class Argument : ISolvable
- {
- private readonly int value;
-
- public Argument(string value)
- {
- // check whether the value given is an empty string
- if (string.IsNullOrEmpty(value))
- throw new ArgumentException("Argument kann nicht mit einem leeren string instanziert werden. ",
- nameof(value));
-
- if (!int.TryParse(value, out var result))
- throw new ArgumentException($"Kann {value} nicht in Integer konvertieren");
-
- this.value = result;
- }
-
- public int Solve()
- {
- return value;
- }
-
- public override string ToString()
- {
- return value.ToString();
- }
- }
-} \ No newline at end of file
diff --git a/DSACore/Auxiliary/Calculator/ISolvable.cs b/DSACore/Auxiliary/Calculator/ISolvable.cs
deleted file mode 100644
index 7be4d19..0000000
--- a/DSACore/Auxiliary/Calculator/ISolvable.cs
+++ /dev/null
@@ -1,10 +0,0 @@
-namespace DSACore.Auxiliary.Calculator
-{
- /// <summary>
- /// Object has to be able to return an integer as it's value
- /// </summary>
- public interface ISolvable
- {
- int Solve();
- }
-} \ No newline at end of file
diff --git a/DSACore/Auxiliary/Calculator/Operator.cs b/DSACore/Auxiliary/Calculator/Operator.cs
deleted file mode 100644
index 31b2a9b..0000000
--- a/DSACore/Auxiliary/Calculator/Operator.cs
+++ /dev/null
@@ -1,51 +0,0 @@
-using System;
-using DSACorev.Auxiliary.Calculator;
-
-namespace DSACore.Auxiliary.Calculator
-{
- /// <summary>
- /// The Operator Class represents a binary operator with tow Arguments and an Operation type
- /// </summary>
- public class Operator : ISolvable
- {
- private readonly ISolvable arg1, arg2;
-
- public Operator(ISolvable arg1, ISolvable arg2, Ops operatorType)
- {
- this.arg1 = arg1;
- this.arg2 = arg2;
- OperatorType = operatorType;
- }
-
- public Ops OperatorType { get; set; }
-
- public int Solve()
- {
- int result;
- switch (OperatorType)
- {
- case Ops.Dice:
- result = Dice.Roll(arg1.Solve(), arg2.Solve());
- break;
- case Ops.Multiply:
- result = arg1.Solve() * arg2.Solve();
- break;
- case Ops.Add:
- result = arg1.Solve() + arg2.Solve();
- break;
- case Ops.Subtract:
- result = arg1.Solve() - arg2.Solve();
- break;
- default:
- throw new ArgumentOutOfRangeException();
- }
-
- return result;
- }
-
- public override string ToString()
- {
- return $"({arg1} {OperatorType} {arg2})";
- }
- }
-} \ No newline at end of file
diff --git a/DSACore/Auxiliary/Calculator/Ops.cs b/DSACore/Auxiliary/Calculator/Ops.cs
deleted file mode 100644
index a5c9a2d..0000000
--- a/DSACore/Auxiliary/Calculator/Ops.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-namespace DSACorev.Auxiliary.Calculator
-{
- /// <summary>
- /// The Different Operations, witch can be performed in execution-order
- /// </summary>
- public enum Ops
- {
- Dice,
- Multiply,
- Subtract,
- Add
- }
-} \ No newline at end of file
diff --git a/DSACore/Auxiliary/Calculator/StringSolver.cs b/DSACore/Auxiliary/Calculator/StringSolver.cs
deleted file mode 100644
index b2a7d83..0000000
--- a/DSACore/Auxiliary/Calculator/StringSolver.cs
+++ /dev/null
@@ -1,183 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using DSACorev.Auxiliary.Calculator;
-
-namespace DSACore.Auxiliary.Calculator
-{
- /// <summary>
- /// The StringSolver divides the calculation string into operations and SubStringSolvers if the string contains
- /// parentheses
- /// </summary>
- public class StringSolver : ISolvable
- {
- private readonly List<object> arguments = new List<object>();
- private readonly string input;
-
- public StringSolver(string input)
- {
- this.input = input;
- }
-
- public int Solve()
- {
- var workInput = "0+" + input.Replace(" ", string.Empty).ToLower();
- workInput = ExpandParentheses(workInput);
-
- // Create a List of the different parts of the calculation, e.g.:{"0", "+", "(5+6)", "d", "3"}.
- AtomizeOperations(workInput);
-
- // traverse the List in order of Operation to Create the binary operation tree .
- NestOperations();
-
- // the List now contains only the top operation node, witch can be solved recursively,
- return ((ISolvable) arguments.First()).Solve();
- }
-
- public override string ToString()
- {
- return "(0+" + input.Replace(" ", string.Empty).ToLower() + ")";
- }
-
- private static string
- GetInner(ref string input) // extract the inner bracket an remove the section from the input string
- {
- var depth = 0;
- for (var index = 1; index < input.Length; index++)
- {
- var c = input[index];
- switch (c)
- {
- case '(':
- depth++;
- break;
- case ')':
- if (depth == 0)
- {
- var split = input.Substring(1, index - 1);
- input = input.Substring(index + 1);
- return split.Equals(string.Empty) ? "0" : split;
- }
- else
- {
- depth--;
- }
-
- break;
- }
- }
-
- return string.Empty;
- }
-
- private static Ops GetOps(char c)
- {
- switch (c)
- {
- case 'd':
- case 'w':
- return Ops.Dice;
- case '+':
- return Ops.Add;
- case '-':
- return Ops.Subtract;
- case '*':
- return Ops.Multiply;
- default:
- return Ops.Multiply;
- }
- }
-
- private static string ExpandParentheses(string input) // insert * between Parentheses and digits
- {
- for (var i = 0; i < input.Length - 1; i++)
- if (input[i + 1] == '(' && char.IsNumber(input[i]))
- input = input.Insert(i + 1, "*");
-
- for (var i = 1; i < input.Length; i++)
- if (input[i - 1] == ')' && char.IsNumber(input[i]))
- input = input.Insert(i, "*");
-
- return input;
- }
-
- private void AtomizeOperations(string workInput)
- {
- for (var index = 0; index < workInput.Length; index++)
- {
- var c = workInput[index];
-
- if (char.IsNumber(c))
- {
- // if char number, check if at end of string, else continue looping
- if (index == workInput.Length - 1)
- // if at end of string; add remaining number to arguments
- arguments.Add(new Argument(workInput.Substring(0, index + 1)));
-
- continue;
- }
-
- switch (c)
- {
- case ')':
- throw new ArgumentException("Unmögliche Anordnung von Klammern");
- case '(':
- arguments.Add(new StringSolver(GetInner(ref workInput)));
- index = -1;
- break;
- default:
- if (index > 0) arguments.Add(new Argument(workInput.Substring(0, index)));
-
- arguments.Add(GetOps(c));
- workInput = workInput.Remove(0, index + 1);
- index = -1;
- break;
- }
- }
- }
-
- private void NestOperations()
- {
- foreach (Ops currentOp in Enum.GetValues(typeof(Ops)))
- // cycle through operators in operational order
- for (var index = 0; index < arguments.Count; index++)
- {
- var arg = arguments[index];
-
- if (arg.GetType() != typeof(Ops)) continue;
-
- // arg is of type Ops
- var op = (Ops) arg;
-
- if (op != currentOp) continue;
-
- // arg describes the current operation
- HandleSpecialFormatting(ref index, op); // Deal with special needs...
-
- // replace the previous current and next Element in the List with one Operation object
- var temp = new Operator((ISolvable) arguments[index - 1], (ISolvable) arguments[index + 1], op);
- arguments[index - 1] = temp;
- arguments.RemoveRange(index, 2);
- index--;
- }
- }
-
- private void HandleSpecialFormatting(ref int index, Ops op)
- {
- var arg1 = arguments[index - 1];
- if (arg1.GetType() == typeof(Ops))
- {
- if (op == Ops.Dice) arguments.Insert(index++, new Argument("1")); // w6 -> 1w6
-
- if (op == Ops.Subtract) arguments.Insert(index++, new Argument("0")); // +-3 -> +0-3
- }
-
- var arg2 = arguments[index + 1]; // 3+-5 -> 3+(0-5)
- if (arg2.GetType() == typeof(Ops))
- {
- arguments[index + 1] = new Operator(new Argument("0"), (ISolvable) arguments[index + 2], (Ops) arg2);
- arguments.RemoveAt(index + 2);
- }
- }
- }
-} \ No newline at end of file