Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Ereptor · Jun 21, 2013 at 02:18 AM · c#listshuffle

List woes. (C#)

Hi everyone,

I'm having some issues with lists I'm running. My game features instantiated avatars which are randomly populated with a series of demographic values such as: age, gender, income, work ethic, etc. Based on an applied societal standard, these demographics are assigned point values. The ultimate point of the game is to see which randomly instantiated avatars will be in the top 20% of their society based on their scores.

Speaking more clearly:

  • New avatars are instantiated on each game;

  • Avatars will always receive a random set of demographics;

  • Number of avatars will always = 30;

  • Avatars will always have a score (based on their demographics);

  • Number of avatars in the top 20% will always = 6;

The issue I'm having is related to my code. I'm trying to give hats to the people who are in the top six. Here is what brute force has done for me so far:

             foreach(GameObject avatar in Avatars){
                 bool avatarAdded = false;                                                        //This avatar hasn't been added yet. DWL
                 if(topSix < 6 && !avatarAdded){                                                    //If there is room in the winner's circle and this hasn't been added yet. DWL
                     int thisScore = avatar.GetComponent<AvatarDemographicScript>().myScore;        //Get the avatar's score. DWL
                     if(thisScore >= highScores[0] && topSix < 6){                                //If the score beats the top score and there is room. DWL
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatarAdded = true;                                                        //We've evaluated this avatar. DWL
                     }
                     else if(thisScore >= highScores[1] && topSix < 6){
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatarAdded = true;
                     }
                     else if(thisScore >= highScores[2] && topSix < 6){
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatarAdded = true;
                     }
                     else if(thisScore >= highScores[3] && topSix < 6){
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatarAdded = true;
                     }
                     else if(thisScore >= highScores[4] && topSix < 6){
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatarAdded = true;
                     }
                     else if(thisScore >= highScores[5] && topSix < 6){
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatarAdded = true;
                     }
                     else{avatarAdded = true;}
                     graduation = true;
                 }
             }

The main issue I'm having with this code:

  • The top six avatars are not always the avatars with the highest scores. In fact, as long as the first six avatars have scores equal to the top 6 highest scores, the first six will always be chosen.

Here is why this is a problem:

  • Highest scores: 24, 22, 16, 16, 16, 16

  • Scores of the first six avatars: 16, 16, 16, 16, 16, 16

  • These six avatars will make up the 'top six' because they are the first six to be equal to or higher than a high-scoring value.

What I would rather do:

  • Check to see if any of the avatars are better than or equal to the HIGHEST score and add them (until I run out of space, or until I run out of room in the top six).

  • Then, check to see if any of the avatars are better than or equal to the second-highest score and add them (until I run out of space, or until I run out of room in the top six).

  • Continue this until I have six winning avatars.

Problem with that is I don't know how to separate the different checks. Every time I initiate a new foreach() I run through ALL the avatars again.

Can someone suggest a way to clean this mess up?

[By the way, in the off chance that someone knows how to call avatars.Shuffle(); before this loop, please let me know!]

Thank you a billion for reading.

Comment
Add comment
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

1 Reply

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

Answer by Ereptor · Jun 21, 2013 at 03:44 AM

I did find a brute force solution. God forgive me for butchering this, but it works!

Take a look:

 foreach(GameObject avatar in Avatars){
                 if(topSix < 6 && avatar.GetComponent<AvatarScript>().keepTallying){                //If there is room in the winner's circle and this hasn't been added yet. DWL
                     int thisScore = avatar.GetComponent<AvatarDemographicScript>().myScore;        //Get the avatar's score. DWL
                     if(thisScore >= highScores[0] && topSix < 6){                                //If the score beats the top score and there is room. DWL
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatar.GetComponent<AvatarScript>().keepTallying = false;                //Don't score this avatar anymore. DWL
                     }
                 }
             }
             
             foreach(GameObject avatar in Avatars){
                 if(topSix < 6 && avatar.GetComponent<AvatarScript>().keepTallying){                //If there is room in the winner's circle and this hasn't been added yet. DWL
                     int thisScore = avatar.GetComponent<AvatarDemographicScript>().myScore;        //Get the avatar's score. DWL
                     if(thisScore >= highScores[1] && topSix < 6){                                //If the score beats the second-to-top score and there is room. DWL
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatar.GetComponent<AvatarScript>().keepTallying = false;                //Don't score this avatar anymore. DWL
                     }
                 }
             }
             
             foreach(GameObject avatar in Avatars){
                 if(topSix < 6 && avatar.GetComponent<AvatarScript>().keepTallying){                //If there is room in the winner's circle and this hasn't been added yet. DWL
                     int thisScore = avatar.GetComponent<AvatarDemographicScript>().myScore;        //Get the avatar's score. DWL
                     if(thisScore >= highScores[2] && topSix < 6){                                //If the score beats the third-to-top score and there is room. DWL
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatar.GetComponent<AvatarScript>().keepTallying = false;                //Don't score this avatar anymore. DWL
                     }
                 }
             }
             
             foreach(GameObject avatar in Avatars){
                 if(topSix < 6 && avatar.GetComponent<AvatarScript>().keepTallying){                //If there is room in the winner's circle and this hasn't been added yet. DWL
                     int thisScore = avatar.GetComponent<AvatarDemographicScript>().myScore;        //Get the avatar's score. DWL
                     if(thisScore >= highScores[3] && topSix < 6){                                //If the score beats the fourth-to-top score and there is room. DWL
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatar.GetComponent<AvatarScript>().keepTallying = false;                //Don't score this avatar anymore. DWL
                     }
                 }
             }
             
             foreach(GameObject avatar in Avatars){
                 if(topSix < 6 && avatar.GetComponent<AvatarScript>().keepTallying){                //If there is room in the winner's circle and this hasn't been added yet. DWL
                     int thisScore = avatar.GetComponent<AvatarDemographicScript>().myScore;        //Get the avatar's score. DWL
                     if(thisScore >= highScores[3] && topSix < 6){                                //If the score beats the fourth-to-top score and there is room. DWL
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatar.GetComponent<AvatarScript>().keepTallying = false;                //Don't score this avatar anymore. DWL
                     }
                 }
             }
             
             foreach(GameObject avatar in Avatars){
                 if(topSix < 6 && avatar.GetComponent<AvatarScript>().keepTallying){                //If there is room in the winner's circle and this hasn't been added yet. DWL
                     int thisScore = avatar.GetComponent<AvatarDemographicScript>().myScore;        //Get the avatar's score. DWL
                     if(thisScore >= highScores[4] && topSix < 6){                                //If the score beats the fifth-to-top score and there is room. DWL
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatar.GetComponent<AvatarScript>().keepTallying = false;                //Don't score this avatar anymore. DWL
                     }
                 }
             }
             
             foreach(GameObject avatar in Avatars){
                 if(topSix < 6 && avatar.GetComponent<AvatarScript>().keepTallying){                //If there is room in the winner's circle and this hasn't been added yet. DWL
                     int thisScore = avatar.GetComponent<AvatarDemographicScript>().myScore;        //Get the avatar's score. DWL
                     if(thisScore >= highScores[5] && topSix < 6){                                //If the score beats the sixth-to-top score and there is room. DWL
                         topSix++;                                                                //Move over! DWL
                         avatar.GetComponent<AvatarScript>().graduationCapActive = true;            //Give this guy a hat! DWL
                         avatar.GetComponent<AvatarScript>().keepTallying = false;                //Don't score this avatar anymore. DWL
                     }
                 }
             }
             
             graduation = true;

In the meantime, I still don't have a clear way of shuffling the GameObject list before running this. I do have a few manual swaps that simulate randomness, but multiple plays will definitely give it away.

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Random Shuffle Listing 2 Answers

How do you get a certain index of a List 2 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