How do I sort my players into teams?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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?

create two teams

use list (array)
Arrays

var team_a = []
var team_b = []

and
team_a.append(“player_n”)

etc
and
For example, teams of 4 each

if team_a.size < 4:
     team_a.append("player_n")

Just an idea. Maybe it will guide you

ramazan | 2022-09-18 08:20

:bust_in_silhouette: Reply From: lpax

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:

  • toss a coin
  • if heads go to team 1
  • else go to team 2
:bust_in_silhouette: Reply From: Ertain

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

Hope this helps.