PDA

View Full Version : Advanced algebra with levelling system

Ludatha
06-23-2010, 04:07 PM
I have a function which returns the experiece required to get to a certain level. I need to write a function which returns the level you are currently on when you supply the experience.

I don't want to have a database with all the levels in there and then get PHP to check that, I just want a function.

Here is the function that returns the experience.

function experience(\$L)
{
// This function returns the experience required to get to a level \$L
\$a=0;
for(\$x=1; \$x<\$L; \$x++) {
\$a += floor(\$x+300*pow(2, (\$x/7)));
}
return floor(\$a/33);
}

Here are the first 20 levels:

Level 1: 0
Level 2: 10
Level 3: 21
Level 4: 33
Level 5: 47
Level 6: 62
Level 7: 78
Level 8: 97
Level 9: 117
Level 10: 139
Level 11: 164
Level 12: 192
Level 13: 222
Level 14: 255
Level 15: 292
Level 16: 332
Level 17: 377
Level 18: 427
Level 19: 481
Level 20: 541

Do you think someone could help me write the function?

Keleth
06-23-2010, 04:31 PM
You can write a similar function that does the math and has an if statement (after supplying it the current experience) checking if the current total is or isn't at the experience level. Of course, on pages where this will be used (and I assume more then once) is to have the function return an array with all level experiences, store that, and refer back to it when needed.

Ludatha
06-23-2010, 04:55 PM
You can write a similar function that does the math and has an if statement (after supplying it the current experience) checking if the current total is or isn't at the experience level. Of course, on pages where this will be used (and I assume more then once) is to have the function return an array with all level experiences, store that, and refer back to it when needed.

I thought about this, but I'm sure there is a much more 'optimized' was to do it with some algebra...

Keleth
06-23-2010, 05:51 PM
Well... assuming you'll call the "at what level" and "how much to level" functions at least once on a page (and likely more then once), you'll then have multiple function calls. Calling a function once and storing the result for use later would be more efficient.