vendredi 11 décembre 2020

Roll until array contains specific elements only

If anyone plays with games, especially GI, they will know what this is about, but basically I'm making a random stat generator.

The first function works fine, it's just to show how I intend it to work. There are some main and random stats, pick one main, and 4 random subs, while also removing the one picked, to avoid duplicates and that's it. They are what they are.

            let sub_stats=['ATK%','ATK','HP%','HP','DEF%','DEF','EM','CR','CD','ER'];
            const main_stats=['ATK%','HP%','DEF%','EM','CR','CD','ER','Physical','Geo','Electro','Pyro','Cyro','Hydro','Anemo'];
            // let sets=[];
            let artifacts=[];

            let random_main=main_stats[Math.floor(Math.random() * main_stats.length)];
            let random_subs=[];

            for(let i=0; i<4; i++){
                let random_sub=sub_stats[Math.floor(Math.random() * sub_stats.length)];
                random_subs.push(random_sub);

                let index = sub_stats.indexOf(random_sub);
                if (index !== -1) {
                    sub_stats.splice(index, 1);
                }
            }

            let artifact={
                main_stat: random_main,
                sub_stats: random_subs,
                rolls: 1
            };

            artifacts.push(artifact);
            sub_stats=['ATK%','ATK','HP%','HP','DEF%','DEF','EM','CR','CD','ER'];
            
            console.log(artifacts)

And then in this second function, my goal is to repeat that rolling the first function does, until the random_subs array contains nothing but "ATK,ATK%,CD,CR"(basically until's it's a perfect roll). Their order doesn't matter at all. And with each failed roll, increment the roll counter.

Here is the current state of this function, which technically works in a way, but still not perfectly, because there should only be 4 sub stats present, and it just keeps adding sub stats, until the condition stated above is true, so there are always just 2 or 3 rolls, because by the 3rd one, all sub stats have already been added. Obviously this is just my fault, but I can't figure out the correct way.

let sub_stats=['ATK%','ATK','HP%','HP','DEF%','DEF','EM','CR','CD','ER'];
            const main_stats=['ATK%','HP%','DEF%','EM','CR','CD','ER','Physical','Geo','Electro','Pyro','Cyro','Hydro','Anemo'];
            // let sets=[];
            let artifacts=[];

            let random_main=main_stats[Math.floor(Math.random() * main_stats.length)];
            let random_subs=[];
            let rolls=0;
            let perfect=false;

            while(!perfect){
                for(let i=0; i<4; i++){
                    let random_sub=sub_stats[Math.floor(Math.random() * sub_stats.length)];
                    random_subs.push(random_sub);

                    let index = sub_stats.indexOf(random_sub);
                    if (index !== -1) {
                        sub_stats.splice(index, 1);
                    }
                }

                rolls++;

                if(random_subs.includes('ATK') &&
                random_subs.includes('ATK%') &&
                random_subs.includes('CR') &&
                random_subs.includes('CD')){
                    perfect=true;
                }
            }



            let artifact={
                main_stat: random_main,
                sub_stats: random_subs,
                rolls: rolls
            };

            artifacts.push(artifact);
            sub_stats=['ATK%','ATK','HP%','HP','DEF%','DEF','EM','CR','CD','ER'];
            
            console.log(artifacts)



Aucun commentaire:

Enregistrer un commentaire