Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts

    Advanced algebra with levelling system

    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.

    PHP Code:
    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:

    Code:
    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?


    Thanks in advance!

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • #3
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Keleth View Post
    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...

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •