I have returned to programming after a year without practice and this is also my first time using Java. I know there is a more efficient way to do this so any suggestions would be appreciated.
I am trying to distribute stats randomly to a character in my game. However, I can't just go through each stat and increase/decrease each one randomly. I need the total increases to be equal to the total decreases. So if one stat goes up 5%, another will need to decrease 5% to compensate.
Egg make_egg(){
float newgr = growth_rate;
float newms = maxSpeed;
int newsf = starve_frames;
int newls = lifespan;
ArrayList<Integer> stats = new ArrayList<Integer>();
stats.add(0);
stats.add(1);
stats.add(2);
stats.add(3);
float percentage = 0;
int roll_stat = 0;
while(stats.size() > 1){
percentage = random(0,1);
//get the value of a random element in the arraylist
roll_stat = stats.get(round(random(0, stats.size()-1)));
//remove(int) is ambiguous for an ArrayList<Integer>
//am I doing this right?
stats.remove(Integer.valueOf(roll_stat));
//--------------LOSSES-------------//
if(roll_stat == 0){ //gr
newgr -= percentage*growth_rate_delta;
if(newgr <= minimum_growth_rate){
newgr = minimum_growth_rate;
}
}
else if(roll_stat == 1){ //ms
newms -= percentage*maxspeed_delta;
if(newms <= minimum_maxspeed){
newms = minimum_maxspeed;
}
}
else if(roll_stat == 2){ //sf
newsf -= round(percentage*starve_frames_delta);
if(newsf <= starve_frames_minimum){
newsf = starve_frames_minimum;
}
}
else if(roll_stat == 3){ //ls
newls -= round(percentage*lifespan_delta);
if(newls <= lifespan_minimum){
newls = lifespan_minimum;
}
}
roll_stat = stats.get(round(random(0, stats.size()-1)));
stats.remove(Integer.valueOf(roll_stat));
//--------------GAINS-------------//
if(roll_stat == 0){ //gr
newgr += percentage*growth_rate_delta;
if(newgr >= maximum_growth_rate){
newgr = maximum_growth_rate;
}
}
else if(roll_stat == 1){ //ms
newms += percentage*maxspeed_delta;
if(newms >= maximum_maxspeed){
newms = maximum_maxspeed;
}
}
else if(roll_stat == 2){ //sf
newsf += round(percentage*starve_frames);
if(newsf >= starve_frames_maximum){
newsf = starve_frames_maximum;
}
}
else if(roll_stat == 3){ //ls
newls += round(percentage*lifespan_delta);
if(newls >= lifespan_maximum){
newls = lifespan_maximum;
}
}
}
return new Egg(x, y, newgr, newms, newsf, newls, generation + 1);
}
I know this will not work if there are an odd number of stats, but I can deal with that later. One other problem this has is that two stats will always decrease and two stats will always increase. However, the ideal effect would be if, for example, one stat could increase by 9%, and the other three stats could decrease by 5, 3, and 1% each.