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.

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.