Topic was automatically imported from the old Question2Answer platform.

Asked By

strangerturtle

I am making a multiplayer game with two teams competing against each other. How do I randomly sort these players onto the two teams and have the teams remain even?

If you have two teams then the number of players should be an even number, otherwise you get one team with exactly one player more than the other. Now if the number of players is even then you can do the following for each player:

Do you have a set of players that need to be sorted into any team, as long as the two teams are even in numbers? Maybe try putting them in different groups. If you have an array of the players, a simple method is to sort them like this:

# The array that has all of the player objects. Here, we'll first shuffle it up.
array_of_players.shuffle()
# Go through the array, adding the first half of the array to a group.
if array_of_players.size() % 2 == 0:
for n in range(array_of_players.size()):
# The "+1" is added here because iterating over a range starts at 0.
if n + 1 <= array_of_players.size() / 2:
array_of_players[n].add_to_group("Red team")
else:
array_of_players[n].add_to_group("Blue team")