samedi 4 mars 2017

Dungeons and Dragons - Ability scores generation

I'm trying to create an array variable containing 6 numbers from 3 to 18, generated the way we generate the ability scores for D&D 5th edition. For those who are not familiar with the game, the concept is to roll 6 die, and add up the 3 highest results, in order to get an ability score between 3 and 18.

My problem is that even tho it is supposed to have a range between 3-18, it sometimes generates a score of 0, and never really seems to go higher than 14... Also, if I try to do a do/while loop until the result is higher than 17, just to check out if it is possible, I get some scores of 24 and even 34... So actually, what am I doing wrong? I tried a lot of things, and still it doesn't work... if anyone could help me out, I'd be so grateful!

Here is my code for the moment:

function generation() {

    var abilities = [0,0,0,0,0,0];
    var i, k, c, n, p;

    //for each ability (there is 6)
    for (c = 0; c <= 5; c++){

        var scores = [0,0,0,0,0,0]
        var nb = [-1,-2,-3] 
        var diffMax = 0;
        var iDiff = 0;
        var diff = 0;

            //roll 6 die
            for (i = 0; i <= 5; i++){

                scores[i] = Math.ceil(Math.random() * 6);

            }

            //for each die rolled
            for (k = 0; k <= 5; k++){

                //compare to each score slot (there is 3)
                for (n = 0; n <= 2; n++){

                    //determine difference
                    diff = scores[k] - nb[n];

                    //if the difference is higher than the previously determined difference
                    if (diff >= diffMax){

                        //note the difference and the position
                        iDiff = n;
                        diffMax = diff;

                    }

                }

                //replace the lowest score slot by the result if this result is higher than the score slot
                if (diffMax >= 0){

                    nb[iDiff] = scores[k];

                }

            }

            //add up each score slot
            for (p = 0; p <= 2; p++){

                abilities[c] = abilities[c] + nb[p];

            }

    }

    return abilities;

}




Aucun commentaire:

Enregistrer un commentaire