Having User input math equations by touch

Hey guys,
I just started working with this and it is great. I am running into an issue.
I am trying to make a game where a user can enter in basic math equations, and then once they "submit," it generates the answer.
So for example I will make a button for 0-9, +, -, ÷, x, (, and ). So the user could click, "(6+5)x(7+8)."
I am not sure how to go about this, as the math expressions are not integers. Should I be using several tables?

Also, thinking ahead, creating a "delete," button(i.e. the user makes an input error and needs to delete the last thing they pressed), seems difficult if it is multiple tables.

If anyone has any help, much appreciated!

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited February 2020

    It's certainly possible to make a calculator but I'm curious why you want to. There are excellent calculator apps already... unless this is part of something else you're intending to make.

    The hard part to figure out -- at least for me -- is how to correctly parse* order of operations (e.g. PEMDAS).

    For example, if someone uses your buttons to create this expression...

    5-((3÷6)x2-4)

    ...what is the algorithm you'll use to solve it? If you can answer that below, maybe we can start to build it in GameSalad. But without understanding the algorithm, it's hard to go about constructing a method for this.

    And what if someone enters 5-)3÷6) or 5-÷3 or another invalid expression? You'd need to check for all sorts of errors.

    Also, Is the user going to enter an equation or just an expression?

    *For example: https://medium.com/@alexanderv/parse-and-evaluate-mathematical-expressions-dd6451e51a96

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • adent42adent42 Key Master, Head Chef, Executive Chef, Member, PRO Posts: 3,034

    So if you are submitting to a server to see if their answer is "right", you can just store it as a string in a table and use the network behaviors to send the string to the server to evaluate.

    It really depends on what you're judging. The form of the expression of the final answer. If it's just the form, you just need a string. If you're going to calculate the answer, you should store it in a table with one token per row (a token either being a complete number or an operation).

    Then you could use the loop over table behavior, some rules and some change attribute actions to calculate the value of the expression.

Sign In or Register to comment.