void FindBrackets(CalculationList calculationList) // this method scrolls through the inputted calculation list, finding other calculation lists inside of it and then entering it { List <Inputs> currentCalculationList = calculationList.calculationList; // creates a new variable that holds the list of actual inputs Debug.Log("Finding brackets in " + calculationList.IDcode); // debug logs which list we are performing what stage in for (int i = 0; i < currentCalculationList.Count; i++) // loops through all indexes in the list { if (currentCalculationList[i].GetType() == typeof(CalculationList)) // if the current index is a calculation list, run below { Debug.Log("Brackets found"); // debug logs that a list was found if ((currentCalculationList[i] as CalculationList).calculationList.Count != 0) // if the list actually has something in it, let's look in it it { Debug.Log("Entering list " + (currentCalculationList[i] as CalculationList).IDcode); // debug log that we are entering it FindBrackets(currentCalculationList[i] as CalculationList); // run this method in the new list we found if ((currentCalculationList[i] as CalculationList).calculationList[0].GetType() == typeof(Number)) // at this point, a proper list will be reduceded to one value. if it has, run this code { currentCalculationList[i] = new Number(((currentCalculationList[i] as CalculationList).calculationList[0] as Number).value); // replaces the index holding the list with a number equal to the list's remaing number Debug.Log("Compressing list into type(Num) with a value of " + (currentCalculationList[i] as Number).value); // debug log the new number being created } else { Debug.Log("Math Error. No number available"); // if there isn't one number in the list, something went wrong. Debug log an error failedCalculation = true; // set failed calculation to true (something went wrong to trigger this) currentCalculationList[i] = new Number(0); // replace the index with a 0 (to avoid crashes, I explained this in input types) } } else // if the list is empty, just replace it with a zero { currentCalculationList[i] = new Number(0); } } } CalculateFunctions(calculationList); // if there are no more calculation lists in the current list, start looking for functions in it }
void NavigateToList(List <int> listID) // takes in an ID, and sets the currentCalculationList to this ID { currentCalculationList = mainMemory; // the porcess start at the origin, the main memory for (int i = 0; i < listID.Count; i++) // for the length of the ID { int index = listID[i]; // set the index being looked in to the value in the current index of the ID list Debug.Log("Checking index " + index); // debug log what index is being checked if (currentCalculationList.calculationList[index].GetType() == typeof(CalculationList)) { // if there is a list there, great! debug log this and enter the list Debug.Log("Entering list " + (currentCalculationList.calculationList[index] as CalculationList).IDcode); currentCalculationList = currentCalculationList.calculationList[index] as CalculationList; } else if (currentCalculationList.calculationList[index].GetType() == typeof(Function)) { // if there is a function there, that's still fine! we'll just enter it's embededList Debug.Log("Entering function " + (currentCalculationList.calculationList[index] as Function).embededCalculationList.IDcode); currentCalculationList = (currentCalculationList.calculationList[index] as Function).embededCalculationList; } else { // if neither are present, that means our ID was messed up somewhere. Log this error and stop looking Debug.LogWarning("ID error. No valid target at " + index + " in " + currentCalculationList.IDcode); break; } } // after it is finished navigating, print what list the function ended in Debug.Log("Landed in " + currentCalculationList.IDcode); }
public bool failedCalculation; // this variable stores whether or not an error came up that would cause the calculation to be incorrect public void Calculate(CalculationList mainEquation) // this is the public method accessed by other classes to start calculating the memory { failedCalculation = false; // sets the bool to true (it can't have failed at the beginning) FindBrackets(mainEquation); // calls this custom method using the inputted calculation list // by here the calculations should be complete, if (mainEquation.calculationList.Count != 1) { // if the main list isn't reduced to one value, then set the calculation to failed Debug.Log("Error. Too many Inputs left it list"); failedCalculation = true; } if (mainEquation.calculationList[0].GetType() != typeof(Number)) { // if the remaing value isn't a number, then set the calculation to failed Debug.Log("Error. Final Input isn't a number"); failedCalculation = true; } if (!failedCalculation) { // if the calculation didn't fail, print the remaining number Debug.Log("Final value: " + (mainEquation.calculationList[0] as Number).value); } else { // if it did fail, debug log "Error" Debug.LogWarning("Math Error"); } }
public ICalculation CreateCalculation(List <double> listOfValues, Func <List <double>, double> _operation) { var _calculation = CalculationList.Create(listOfValues, _operation); _listOfCalculations.AddCalculation(_calculation); return(_calculation); }
public void Resolve() { if (CurrentCalculation.CanBeOperated()) { CalculatedNumbers.Add(new CalculatedNumber(CurrentCalculation)); CalculationList.Add(new Calculation(CurrentCalculation)); RemoveValuesFromLists(CurrentCalculation); CurrentCalculation.Restart(); SetMessage(); } }
public void RestartGame() { foreach (var number in Numbers) { number.Enable(); } CalculatedNumbers.Clear(); Message = new Message(Objetive); CurrentCalculation.Restart(); CalculationList.Clear(); }
public void ClearEquation() // this function resets the calculator { Debug.Log("Clearing equation"); // debug log what is happening rangeMode = false; // set range mode to false mainMemory.calculationList.Clear(); // delete everything in the mainMemory currentCalculationList = mainMemory; // set the current calculation list to the main memory mainMemory.ID.Clear(); equationText = ""; equationDisplay.text = ""; // set the equation string and both displays to blank strings resultDisplay.text = ""; }
void CalculateFunctions(CalculationList calculationList) // this method looks for functions in the inputted list, enters them to calculate them into one number, and then applies the function to them { List <Inputs> currentCalculationList = calculationList.calculationList; // creates a new variable to hold the list of inputs rather than the object Debug.Log("Finding functions in " + calculationList.IDcode); // debug logs which method we are performing on what list for (int i = 0; i < currentCalculationList.Count; i++) // index through each value { if (currentCalculationList[i].GetType() == typeof(Function)) // if the index is a Function { Debug.Log("Function found"); if ((currentCalculationList[i] as Function).embededCalculationList.calculationList.Count != 0) { Debug.Log("Entering embeded list"); // debug log what we found FindBrackets((currentCalculationList[i] as Function).embededCalculationList); // enter it's embeded calculation list, and look for brackets in it } else { Debug.Log("Function is empty"); failedCalculation = true; } Debug.Log("Calculating function"); // at this stage, the function should have one numnber in it currentFunc = currentCalculationList[i] as Function; // set currentFunc to the our current function (to avoid repeated casting) if (currentFunc.embededCalculationList.calculationList.Count == 1) { // if the function has one value left in it, continue if (currentFunc.embededCalculationList.calculationList[0].GetType() == typeof(Number)) { // if the function has one NUMBER left in it (which it should be) run as normal { float functionValue = currentFunc.Calculate(); // calculate what the value should be, and store it in this variable if ((currentCalculationList[i] as Function).failedCalculation) { failedCalculation = true; } // if the function's failedCalculation is true, set the bedmas's failedCalculation to true currentCalculationList[i] = new Number(Convert.ToDecimal(functionValue)); // replaces the index containing the function with a number containing the value that was calculated } } else { // if it isn't a number, print what is going wrong Debug.Log("Math Error. Trying to get " + currentFunc.functionType + " of type " + currentFunc.embededCalculationList.calculationList[0].GetType()); failedCalculation = true; // set failedCalulation to true currentCalculationList[i] = new Number(0); // replace the value with a 0 (it needs a number) } } else { // if the embeded list is too long Debug.Log("Math Error. Too many values in " + currentFunc.embededCalculationList.IDcode); failedCalculation = true; // set failedCalulation to true currentCalculationList[i] = new Number(0); // replace the value with a 0 } } } MultiplyDivide(calculationList); // no more functions in here? move on to DM (dividing and multipling) }
bool nextNumberNegative; // is the next number negative? void Awake() // this is a built in Unity function that allows you to write code that runs as soon as the program starts { Screen.SetResolution(550, 400, false); // the ui doesn't work very well, haven't figured that out yet, so i restrict the app window size equationDisplay.text = ""; // sets the equation display text to an empty string resultDisplay.text = ""; // sets the result display text to an empty string equationText = ""; // sets the equation string to blank mainMemory = new CalculationList(new List <int> { }, "mainMemory"); // sets the main memory to a new calculationList with an empty ID and a hard coded name currentCalculationList = mainMemory; // sets the current input list to the main memory if (instance != null) { // if if instance has been assigned to, that means that multiples of this script exist, which will cause problems. Debug.Log("Multiple instances of Calculation found"); } instance = this; // set instance to be this script }
public void EqualsPressed() // this function runs when the equals button is pressed { if (!rangeMode) // if the calculator isn't in range mode, do what you would expect an equal button to do { PrintMemory(mainMemory, true); // print the entire memory bedmasBrain.Calculate(mainMemory); // uses bedmas brains's calculate method on our main memory (AKA what you use a calculator for) if (bedmasBrain.failedCalculation) // if some part of the bedmas calculation failed { resultDisplay.text = "Math Error"; // set our result to Math Error, rather than a number } else { resultDisplay.text = (mainMemory.calculationList[0] as Number).value.ToString("G29"); // set our result text to bedmasBrain's remaining number previousAns = new Number((mainMemory.calculationList[0] as Number).value); // set our previousAns to this number Debug.Log("Previous answer: " + previousAns.value); // debug log this saving feature } currentCalculationList = mainMemory; // set the current input list to main memory } else // if the calculator is in range mode, run something completely different { if (minNotMax) // if the user is entering the min { minNotMax = false; // set this bool to max, not min Debug.Log("Inputing random range max"); // debug log and give the user instructions for inputting max range resultDisplay.text = "Enter range max.\nPress equal for number"; equationText += " - "; // create a visual barrier for the min value and the max value equationDisplay.text = equationText; // update our text display to our equation string mainMemory.calculationList.Add(new Operator(Operator.OperatorType.mul)); // add a place holder operator in the main memory (just in case an operation needs to be done on these numbers, and to seperate the numbers) *** This step isn't really needed, it just makes me feel better *** } else // if they are entering the max number... { float minRange = (float)(mainMemory.calculationList[0] as Number).value; // set the min to the first number entered float maxRange = (float)(mainMemory.calculationList[2] as Number).value; // set the max to the second number entered float randomNumber = UnityEngine.Random.Range(minRange, maxRange); // generate a random number based off of these ranges mainMemory.calculationList.Clear(); // remove all indexs from the main memory mainMemory.calculationList.Add(new Number(Convert.ToDecimal(randomNumber))); // set the first index to our new number resultDisplay.text = (mainMemory.calculationList[0] as Number).value.ToString(); // output the new value in our result box previousAns = new Number((mainMemory.calculationList[0] as Number).value); // save our random number in previousAns Debug.Log("Previous answer: " + previousAns.value); // debug log the value created rangeMode = false; // exit range mode } } }
void PrintMemory(CalculationList calculationList, bool firstIteration = false) // this is a debugging function that steps through the memory, printing what it finds (the firstIteration = false means that unless specified, the function isn't printing in the firstIteration, AKA it's false) { if (firstIteration) { // if this iteration was started by me pressing "P", debug log when the printing started (this will only happen for the first list being printed) Debug.Log("--------------- Pritning time: " + Time.time + " ---------------"); } // debug log what list is being printedß Debug.Log("--------------- Pritning at " + calculationList.IDcode + " ---------------"); for (int i = 0; i < calculationList.calculationList.Count; i++) { // index through every value in the inputted list if (calculationList.calculationList[i].GetType() == typeof(Number)) { // if a number is found, print what value it has and where it is Debug.Log("Number " + (calculationList.calculationList[i] as Number).value + " at index " + i); } else if (calculationList.calculationList[i].GetType() == typeof(Operator)) { // if an operator is found, debug log what type it is and where it is Debug.Log("Operator " + (calculationList.calculationList[i] as Operator).operatorType + " at index " + i); } else if (calculationList.calculationList[i].GetType() == typeof(Function)) { // debug log where the function is and what type it is... Debug.Log("Function " + (calculationList.calculationList[i] as Function).functionType + " at index " + i); PrintMemory((calculationList.calculationList[i] as Function).embededCalculationList); // and start printing it's contents (the bool is set to false because it isn't the first print) } else if (calculationList.calculationList[i].GetType() == typeof(CalculationList)) { // debug log where the brackets are... Debug.Log("CalculationList at index " + i); PrintMemory(calculationList.calculationList[i] as CalculationList); // and start printing it's contents } } if (calculationList.calculationList.Count == 0) { Debug.Log("List is empty"); // if there isn't anything in the list, debug log that } Debug.Log("--------------- End of " + calculationList.IDcode + " ---------------"); // debug log that the end of the inputted list has been reached if (firstIteration) { // if wew have reached here, that means that the function is at the end of the printing function on the main memory Debug.Log("Current calculation list: " + currentCalculationList.IDcode); // as extra info, debug log what list values are being inputted into... Debug.Log("Current equation text: |" + equationText + "|"); // and debug log what our equationText is, so what are display internally looks like } }
void MultiplyDivide(CalculationList calculationList) // this method looks for the * and / operator, and applies that operator to the surronding numbers { List <Inputs> currentCalculationList = calculationList.calculationList; // same drill, set it to the list... Debug.Log("Finding multiplication/division in " + calculationList.IDcode); // and log what we are doing... for (int i = 0; i < currentCalculationList.Count; i++) // and look at each index { if (currentCalculationList[i].GetType() == typeof(Operator)) { // if we find an operator... currentOper = currentCalculationList[i] as Operator; // set currentOper to the operator we found (to avoid casting) if (currentOper.operatorType == Operator.OperatorType.div || currentOper.operatorType == Operator.OperatorType.mul) { // we only care about multiplying and dividing right now, so only do math if the operator is either of those to types if (i != 0 && i != currentCalculationList.Count - 1) { // if the operator isn't at the beginning of the list ('_+3' what are we adding?) or the end ('3/_' dividing by what), do some math if (currentCalculationList[i + 1].GetType() == typeof(Number) && currentCalculationList[i - 1].GetType() == typeof(Number)) { // if the two numbers surronding the operator are numbers currentNum1 = currentCalculationList[i - 1] as Number; // the number before the operator is stored here currentNum2 = currentCalculationList[i + 1] as Number; // the number after the operator is stored here currentCalculationList[i] = new Number(SimpleMath(currentNum1, currentNum2, currentOper)); // we replace the index holding the operator with the operation applied to the 2 stored numbers currentCalculationList.RemoveAt(i + 1); // next we delete the second number currentCalculationList.RemoveAt(i - 1); // and then the first number i--; // subtract our current index by one so we don't miss a number } else { Debug.Log("No numbers found around operator"); // if there weren't enough numbers on each side of the operator, debug log this failedCalculation = true; // set the calculation to failed } } else { Debug.Log("Invalid operator location"); // if the operator wasn't in a possible location to do math, debug log this fact failedCalculation = true; // save that the calculation has failed } } } } AddSubtract(calculationList); // move onto the final step in bedmas, addition and subraction in our calculation list }
void AddSubtract(CalculationList calculationList) // this method is the exact same as above, but with + and - { List <Inputs> currentCalculationList = calculationList.calculationList; Debug.Log("Finding addition/subtraction in " + calculationList.IDcode); // if you still can't understand these 3 lines I can't help you for (int i = 0; i < currentCalculationList.Count; i++) { if (currentCalculationList[i].GetType() == typeof(Operator)) { // check if the current index is equal to type operator if (i != 0 && i != currentCalculationList.Count - 1) { // if the operator isn't on either end of the list currentOper = currentCalculationList[i] as Operator; if (currentOper.operatorType == Operator.OperatorType.add || currentOper.operatorType == Operator.OperatorType.sub) { // now we only care about addition and subtraction if (currentCalculationList[i + 1].GetType() == typeof(Number) && currentCalculationList[i - 1].GetType() == typeof(Number)) { // we can only do the math if the two numbers surronding the operator are numbers currentNum1 = currentCalculationList[i - 1] as Number; // the first number in the equation is stored here currentNum2 = currentCalculationList[i + 1] as Number; // the next number is held here currentCalculationList[i] = new Number(SimpleMath(currentNum1, currentNum2, currentOper)); // we replace the index here with the operator applied to these 2 numbers currentCalculationList.RemoveAt(i + 1); // delete the second number currentCalculationList.RemoveAt(i - 1); // and the first number next i--; // subract our current index by one (we just moved everything down one, so we need to account for that) } else { Debug.Log("No numbers found around operator"); // if there weren't enough numbers on each side of the operator, debug log this failedCalculation = true; // set the calculation to failed } } else { Debug.Log("Invalid operator location"); // if the operator wasn't in a possible location to do math, debug log this fact failedCalculation = true; // saved that the calculation has failed } } } } }
// the above bool stores whether or not a function is possible, such as ASIN(ø) when ø > 1, which throw an error in calculating public Function(FunctionType functionType, List <int> ID) // constructor takes an ID list for it's embededCalculationList, and the enum for it's function type { this.functionType = functionType; // sets the objects functionType to the enum inputted embededCalculationList = new CalculationList(ID); // creates an embededList with an ID equal to the inputted ID }