Tuesday, April 4, 2023

Coding Challenge #41 MGSV Sort New Recruits

// Inspired by MGSV. The base has several stats and soldiers also have the same several stats. When the base is full of high-stat soldiers, the base has high stats in reflection.
// The player can recruit soldiers, and those new recruits are automatically sorted by the system as to where they should go -- we will do that sorting.
// When a new recruit is added, their highest stat determines where in the base they will be stationed.
// If a new recruit doesn't increase the base stats at all (e.g. base has all max stat soldiers) then the new recruit is dismissed without being stationed at the base.

// for this test, the stats will be atk (attack/offensive), def (defense), and med (medical knowledge & skill)
function base(atk, def, med){
    this.atk = atk;
    this.def = def;
    this.med = med;
    this.soldiersAtk = []; // soldiers for each station ATK, DEF, and MED
    this.soldiersDef = [];
    this.soldiersMed = [];
    this.soldiersMax = 2; // max soldiers per category
    this.calc = function(){ // calculate total base atk,def,med as the sum of its soldiers'
        var total = 0;
        for(var xint = 0, len = this.soldiersAtk.length;xint < len;xint++) total += this.soldiersAtk[xint].atk;
        this.atk = total;
        total = 0;
        for(var xint = 0, len = this.soldiersDef.length;xint < len;xint++) total += this.soldiersDef[xint].def;
        this.def = total;
        total = 0;
        for(var xint = 0, len = this.soldiersMed.length;xint < len;xint++) total += this.soldiersMed[xint].med;
        this.med = total;
    }
}
function soldier(atk, def, med){
    this.atk = atk;
    this.def = def;
    this.med = med;
}

function sortSoldiers(base, soldiers){ // obj base, ary of objs soldiers
    for(var xint = 0, len = soldiers.length;xint < len;xint++){
        if(Math.max(soldiers[xint].atk,soldiers[xint].def,soldiers[xint].med) == soldiers[xint].atk){ // atk is his highest (or one of highest equally) stats
            //console.log('soldier ' + xint + ' is atk. soldier: ',soldiers[xint]);
            if(base.soldiersAtk.length < base.soldiersMax){ // add the soldier
                base.soldiersAtk.push(soldiers.splice(0,1)[0]);
                xint--;
                len--;
                continue; // added. next soldier
            }else{ // replace weakest soldier in the category
                var rep = 0; // index of base soldier being replaced
                var lowest = base.soldiersAtk[0].atk;
                for(var yint = 0, ylen = base.soldiersAtk.length;yint < ylen;yint++){
                    if(base.soldiersAtk[yint].atk < lowest){
                        rep = yint;
                        lowest = base.soldiersAtk[yint].atk;
                        break;
                    }
                }
                if(soldiers[xint].atk > lowest){
                    soldiers.push(base.soldiersAtk[rep]); // remove old soldier from base
                    base.soldiersAtk[rep] = soldiers.splice(0,1)[0]; // move new soldier to base
                    xint--;
                    continue; // added; next soldier
                }
            }
        }
        // by here, the soldier's best stat wasn't ATK or there wasn't room for another ATK soldier and the soldier didn't improve the overall ATK
        if(Math.max(soldiers[xint].atk,soldiers[xint].def,soldiers[xint].med) == soldiers[xint].def){
            //console.log('soldier ' + xint + ' is def. soldier: ',soldiers[xint]);
            if(base.soldiersDef.length < base.soldiersMax){ // add the soldier
                base.soldiersDef.push(soldiers.splice(0,1)[0]);
                xint--;
                len--;
                continue; // added. next soldier
            }else{ // replace a soldier
                var rep = 0; // index of base soldier being replaced
                var lowest = base.soldiersDef[0].def;
                for(var yint = 0, ylen = base.soldiersDef.length;yint < ylen;yint++){
                    if(base.soldiersDef[yint].def < lowest){
                        rep = yint;
                        lowest = base.soldiersDef[yint].def;
                        break;
                    }
                }
                if(soldiers[xint].def > lowest){
                    soldiers.push(base.soldiersDef[rep]); // remove old soldier from base
                    base.soldiersDef[rep] = soldiers.splice(0,1)[0]; // move new soldier to base
                    xint--;
                    continue; // added; next soldier
                }
            }
        }
        if(Math.max(soldiers[xint].atk,soldiers[xint].def,soldiers[xint].med) == soldiers[xint].med){
            //console.log('soldier ' + xint + ' is med. soldier: ',soldiers[xint]);
            if(base.soldiersMed.length < base.soldiersMax){ // add the soldier
                base.soldiersMed.push(soldiers.splice(0,1)[0]);
                xint--;
                len--;
                continue; // added. next soldier
            }else{ // replace a soldier
                var rep = 0; // index of base soldier being replaced
                var lowest = base.soldiersMed[0].med;
                for(var yint = 0, ylen = base.soldiersMed.length;yint < ylen;yint++){
                    if(base.soldiersMed[yint].med < lowest){
                        rep = yint;
                        lowest = base.soldiersMed[yint].med;
                        break;
                    }
                }
                if(soldiers[xint].med > lowest){
                    soldiers.push(base.soldiersMed[rep]); // remove old soldier from base
                    base.soldiersMed[rep] = soldiers.splice(0,1)[0]; // move new soldier to base
                    xint--;
                    continue; // added; next soldier
                }
            }
        }
    }
    base.calc();
}

function test(base, soldiers){
    
    console.log("Sorting " + soldiers.length + " soldiers into the base.");
    sortSoldiers(base, soldiers);
    console.log("Base stats now: " + base.atk + " ATK, " + base.def + " DEF, " + base.med + " MED.\nBase soldiers: ATK: ",base.soldiersAtk.length,". DEF: ",base.soldiersDef.length,". MED: ",base.soldiersMed.length,".");
}

test(new base(0,0,0), [new soldier(1,1,1), new soldier(1,0,0), new soldier(2,1,1)]);
test(new base(0,0,0), [new soldier(1,0,0), new soldier(0,1,0), new soldier(0,0,1)]);

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...