简体   繁体   中英

More Efficient Way to Accomplish This?

I need to know the level of a player using the amount of exp he has and the exp chart. I want to do it the most efficient way possible. This is what I got. Note: The real expChart has thousands of levels/index. All the values are in increasing order.

var expChart = [1,10,23,54,65,78,233,544,7666,22224,64654,456456,1123442];
/*
lvl 0: //0-1[ exp
lvl 1: //[1-10[ exp
lvl 2: //[10-23[ exp
*/
getLvlViaExp = function(exp){
    for(var i = 0 ; i < expChart.length ; i++){
        if(exp < expChart[i]) break;
    }
    return i;
}

This is a more efficient way to do it. Every x steps, (6 i the example, probably every hundreds with real chart), I do a quick comparation and jump to approximative index, skipping many indexes.

getLvlViaExp = function(exp){
    var start = 0;
    if(exp > 233)   start = 6;
    if(exp > 1123442) start = 12;

    for(var i = start ; i < expChart.length ; i++){
        if(exp < expChart[i]) break;
    }
    return i;
}

Is there an even better way to do this?


SOLUTION:

Array.prototype.binarySearch = function(value){
    var startIndex  = 0,
        stopIndex   = this.length - 1,
        middle      = Math.floor((stopIndex + startIndex)/2);
    if(value < this[0]) return 0;
    while(!(value >= this[middle] && value < this[middle+1]) && startIndex < stopIndex){

        if (value < this[middle]){
            stopIndex = middle - 1;
        } else if (value > this[middle]){
            startIndex = middle + 1;
        }

        middle = Math.floor((stopIndex + startIndex)/2);
    }

    return middle+1;
}

The best algorithm for searching is binary search which is O(lg n) (unless you can do it with a hashing search which is O(c).

http://www.nczonline.net/blog/2009/09/01/computer-science-in-javascript-binary-search/

Basically jump to the middle of your chart ( n / 2). Is you experience higher or lower from that number. If higher jump to the middle higher half. If lower jump to the middle of the lower half: Compare and repeat until you find what you're looking for.

var expChart = [1,10,23,54,65,78,233,544,7666,22224,64654,456456,1123442];
getLvlViaExp = function(exp){
    var min=0;
    var max=expChart.length-1;
    var i;
    while(min <=max){

    i=Math.round((min+max)/2); 
        //document.write("<br />"+i+":"+min+":"+max+"<br />");
        if(exp>=expChart[i] && exp <=expChart[i+1]) {
        break;
        }
    if(exp>=expChart[i]){
            min=i+1;
        }
    if(exp<=expChart[i]){
            max=i-1;

        }

    }
return i;
}

document.write(getLvlViaExp("10"));

I have tested it and it seems to work pretty well. If you want to see how many steps it actually goes through to get to the answer, uncomment the document.write in the while loop. It was kind of fascinating watching it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM