Resolved How to pull from a List with specific requirements

smalt

New member
Joined
Aug 20, 2022
Messages
3
Programming Experience
1-3
I'm trying to create a sports simulation, and I currently have a large pool of Players (which is a class) populated into a List. Each Player has an Energy variable (int) and a "isOnTeam" bool.

What I'd like to do is create a function that basically creates two teams by pulling Players randomly out of this List and placing them onto a Team. However, I don't want the Player to be placed on a Team if 1) they have no energy and 2) they are already on a team.

I'm having the hardest time trying to conceptualize how this will work. How can I run through a List, pulling Players randomly that have energy and are not already have a team, and then having that function stop once each Team (2) is full?

Any help would be greatly appreciated! 🙏🙏
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
If you were doing this with team information on little scraps of paper, how would you do it? Consider the elements of your list as the scraps of paper.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,755
Location
Sydney, Australia
Programming Experience
10+
I'm having the hardest time trying to conceptualize how this will work.
The problem is usually that people don't try to conceptualise it. They usually try to write code directly from an idea, without any real idea of what the code has to do. They know the result they want but not the steps to get there. Those steps are logic, not code. You need to think about logic independently of code. We all use logic every day of our lives, so there's no programming experience required. As suggested above, you need to think about how you would perform this task if it were a manual process. I have little doubt that you could perform such a task so the idea that you can't conceptualise it doesn't really hold true. You should be able to explain to someone exactly what the steps are that the code would need to perform before writing any code to perform them. It's because people skip that first step that they can't write the code. If you don't understand a specific step or have trouble implementing a specific step then we can help with that but, if you don't know what the steps are, i.e. you haven't written an algorithm, then there's no specific issue for us to help with.
 

smalt

New member
Joined
Aug 20, 2022
Messages
3
Programming Experience
1-3
This is all actually really helpful -- I never thought to think of information as scraps of paper while coding. That's really interesting and helpful!

Maybe 'conceptualize' was the wrong word. I think I understand how to implement it through code... for instance using a for statement that returns a value if certain conditions are met, in this instance if (player.Energy > 0 && player.isOnTeam = true).

I am relatively new to programming, so I'm not sure how to restart a for loop if certain conditions aren't met.

For instance, I have this so far:

C#:
public Player FindAvailablePlayer ()
    {
        Player player = PlayerListManager.allPlayers[Random.Range(0, PlayerListManager.allPlayers.Count)]
        if (player.energy > 0 && player.isOnTeam = false)
        {
            return player;
        }
        else
        {
            // How do I restart the loop to find an available player, and how do I also return null if there are no more available players so I can work with those conditions? 
        }
    }


I hope this is making sense -- and apologies if not 😟
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
A while loop is better. Some pseudo code that may help:

C#:
flag = 0
while (count of eligible players is not empty AND
       team 1 is not full AND
       team 2 is not full)
{
    take first player from eligible players list
    flag = flag + 1

    if (flag is odd AND team 1 is not full)
    {
        put player into team 1
        mark player as in a team
        remove player from eligible players list
    }
    else if (flag is even AND team 2 is not full)
    {
        put player into team 2
        mark player as in a team
        remove player from eligible players list
    }
}

Now all you need to do is create that eligible players list. Basically go through your original list and only add in the player into the eligible players list if they are not in a team and if their energy is greater than zero. Once you have that list, randomize the contents of the list. When you now have that randomized list of eligible players, you can do the while loop above.

If you are allowed to have lopsided teams, two while loops will be easier to do and not have to deal with that silly odd/even check to determine which team to add to. Simply fill up the first team first, and then fill up the second team after that.
 

smalt

New member
Joined
Aug 20, 2022
Messages
3
Programming Experience
1-3
OK, I think this is very helpful @Skydiver - thank you very much. I'll take a look at your code and try to integrate it! I think I initially wanted to see if it could be done without creating a duplicate list. With your help, I've started playing around with creating bool methods that check information in my lists - which is great. Thank you!
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
Ah, but it is not a duplicate list. It will be a smaller list of eligible players and randomized. Furthermore, recall that in C# all objects are references. So the second list will still be referring to the same objects in first list.
 
Top Bottom