- Home /
C# Random Stats Distribuition
Hello, i need a help :( I'm trying to make a code to on click in a button, the game distribute the player stats, with a minimal value of 2 and maximum 6, and the sum need to be 16.
Here's the code:
public void randomStats(){
maxPoints = 16;
currentPoints = 16;
switchStats = 4;
atk = 2;
dex = 2;
def = 2;
intelligence = 2;
Debug.Log ("OLA");
maxPoints -= 8;
currentPoints -= 8;
do{
for(int i = 0; i < 4; i++){
randomPoint = Random.Range (0,4);
switch(switchStats){
case 1:
atk += randomPoint;
switchStats -= 1;
break;
case 2:
dex += randomPoint;
switchStats -= 1;
break;
case 3:
def += randomPoint;
switchStats -= 1;
break;
case 4:
intelligence += randomPoint;
switchStats -= 1;
break;
}
}
}
while(switchStats == 4);
}
The problem is that the sum of the stats need to be 16, but i can't do to always give 16.
Sorry for the bad english.
Where does it fail? In what way? Try using Debug.Log in more places to "probe" the state of the program, and see where it goes wrong.
Also, are you sure you mean Switch(switcStats)
rather than Switch(i)
?
Sorry, i forgot to say the problem exactly, i've updated the post.
Answer by Baste · Dec 02, 2014 at 03:20 PM
Your loop is doing something wrong. The idea is right - distribute stats randomly until the sum is exactly 16. What you need to do is to have a while-loop running until the sum is exactly 16, adding to one random stat each time you go through the loop:
void randomStats() {
atk = 2;
def = 2;
dex = 2;
intel = 2; //stay consistent - either use 'attack' and 'intelligence', or 'atk' and 'intel'
while (atk + def + dex + intel < 16) {
int choice = Random.Range(0, 4); //Will give random between 0 and 3!
switch (choice) {
case 0:
if (atk < 6)
atk++;
break;
case 1:
if (def < 6)
def++;
break;
case 2:
if (dex < 6)
dex++;
break;
default:
if (intel < 6)
intel++;
break;
}
}
}
It would possibly be even easier to use an array, and have each index be a different stat:
public void randomStats() {
int[] stats = {2, 2, 2, 2};
while (Sum(stats) < 16) {
int randomIndex = Random.Range(0, 4);
if (stats[randomIndex] < 6)
stats[randomIndex]++;
}
atk = stats[0];
def = stats[1];
dex = stats[2];
intel = stats[3];
}
private int Sum(int[] stats) {
int sum = 0;
for (int i = 0; i < stats.Length; i++) {
sum += stats[i];
}
return sum;
}
I think the second one is prettier, but it's up to you which one you like the best. Hope this helps!
Thanks a lot Baste! You've saved my life :) (if i can take one more question..., how can i do that every time that i call randomStats();? because i call that every time that i click in a button "generate stats")
What do you mean "how can I do that"? What's your goal - getting a different set of stats? The four variables will be reset every time you do the call.
Answer by BenTristem · Dec 02, 2014 at 03:38 PM
Here are some more suggestions...
Remove maxPoints and currentPoints as you don't seem to use them.
I think you mean
while(switchStats > 0)
near the end?You don't use the incriminator i in your for loop.
Sort-out the indentation, it's very confusing. Section 2 of our course would help.
Be careful with break, are you trying to exit the for loop, or the switch statement?
It's c#. If you don't break at the end of each case in a switch statement, the code doesn't compile.
Answer by Koelho · Dec 03, 2014 at 12:14 AM
But work just when i click first time, so to generate again i need to restart the scene
Your answer
Follow this Question
Related Questions
Stats effecting the max Health Value 2 Answers
Getting XP when dies Help 1 Answer
More elegant way to constantly update player health algorithms. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers