Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Koelho · Dec 01, 2014 at 10:36 PM · c#rpgstatsdistribution

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BenTristem · Dec 02, 2014 at 12:17 PM 1
Share

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) ?

avatar image Koelho · Dec 02, 2014 at 01:31 PM 0
Share

Sorry, i forgot to say the problem exactly, i've updated the post.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Koelho · Dec 02, 2014 at 06:00 PM 0
Share

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")

avatar image Baste · Dec 02, 2014 at 11:16 PM 0
Share

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.

avatar image
-1

Answer by BenTristem · Dec 02, 2014 at 03:38 PM

Here are some more suggestions...

  1. Remove maxPoints and currentPoints as you don't seem to use them.

  2. I think you mean while(switchStats > 0) near the end?

  3. You don't use the incriminator i in your for loop.

  4. Sort-out the indentation, it's very confusing. Section 2 of our course would help.

  5. Be careful with break, are you trying to exit the for loop, or the switch statement?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Dec 02, 2014 at 05:49 PM 1
Share

It's c#. If you don't break at the end of each case in a switch statement, the code doesn't compile.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges