x


calculate terms in runtime

Is there an easy way to get the result of a mathematical term in runtime?

For example I randomly genrate a term like 1+3*4=, the player has to type in the correct result an the game says right or wrong. Is there a function like

result = Calculate(term:string);

Any ideas???

more ▼

asked Jul 19 '12 at 01:37 PM

fschaar gravatar image

fschaar
40 6 9 16

I think there's no predefined function for you, it may need you to code for it. If you generate the terms in your own way, then you should know the result already. If you're reading the term from somewhere else, then you need to parse the string and split the string from * and / first, then + and - and so on.

Jul 19 '12 at 02:17 PM J Lam

Well the problem is, it ist a learning game for children, that shall figure out the right order of the numbers in the term. there can be be more then one solution. so i have to check, if what they put together is right or wrong

Jul 19 '12 at 03:12 PM fschaar
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You can use eval to convert a string into code.

function Start () {
    var a = Random.Range(1, 10);
    var b = Random.Range(1, 10);
    var c = Random.Range(1, 10);
    var operations = "-+*";
    var equation = "" + a + operations[Random.Range(0, operations.Length)]
        + b + operations[Random.Range(0, operations.Length)] + c;
    var x = eval(equation);
    Debug.Log (equation + "=" + x);
}
more ▼

answered Jul 19 '12 at 03:12 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

I want to convert my project later to iphone and android. does eval work ther or do i have to create an own function anyway?

Jul 20 '12 at 08:28 AM fschaar

No, eval doesn't work with #pragma strict, so therefore not iOS.

Jul 20 '12 at 05:39 PM Eric5h5

Ah thank you - now I have an idea, what pragma strict means ;) That means I have to write my own function to do the calculation ..

Jul 23 '12 at 01:21 PM fschaar
(comments are locked)
10|3000 characters needed characters left

Yeah you will have to parse it, build up a tree to apply the calculation order etc.

I would say, if you randomly generate it, do not generate it as string in the first pace, but in your own format you can calculate and just present it as string.

Maybe have custom a function RandomEq(...) and get back the result and the string representation in a struct.

more ▼

answered Jul 19 '12 at 02:53 PM

Nightwatch gravatar image

Nightwatch
31 1

(comments are locked)
10|3000 characters needed characters left

Completely random might be difficult/impossible, but you could easily build a function that churns out many different terms...

First I would use a Switch statement (and a random number) to choose the general structure of the term, without fixed numbers, only variables. Then you could assign the variables random numbers, calculate the solution and test it against the player's input.

more ▼

answered Jul 19 '12 at 03:02 PM

Piflik gravatar image

Piflik
5.4k 15 26 44

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3463
x3339
x420
x212

asked: Jul 19 '12 at 01:37 PM

Seen: 284 times

Last Updated: Jul 23 '12 at 01:21 PM