I'm currently building up a little game, where users need to be randomly assigned to teams.
What I got is a table in the database, where the logged in users are listed. Also, each user has a column for the team.
Now in PHP, I got all users stored in a variable, like the following (output of var_export
)
array ( 0 => array ( 'ID' => '3', 'name' => 'olaf', 'team' => NULL, ), 1 => array ( 'ID' => '4', 'name' => 'Peter', 'team' => NULL, ), 2 => array ( 'ID' => '5', 'name' => 'chris', 'team' => NULL, ), 3 => array ( 'ID' => '6', 'name' => 'günther', 'team' => NULL, ), )
Now I got two teams, Team Red and Team Blue. My goal is, that every person has randomly been assigned to one of the two teams, however, both teams should have a similar amount of team members (or if an odd number of users is logged in, one team can have one more player).
When assigned, I would then write the information to the database. However, I'd like to always be able to shuffle the teams and then have new teams.
The number of logged in users can vary between 2 and 50 persons.
So far, I thought about multiple ways of doing it, like randomly picking a number between 1 and sizeof(array of users) and then assigning him a team, however, I couldn't get it to work...
Also I this Stackoverflow post, however, using this code, my team Red always had player 1 and 3 or 1 and 4 and my team Blue always had player 2 and 3 or 2 and 4. It never happened, that player 1 was in team red. However, as mentioned, the mix should be completely random.
Can anybody help me out with this or has an idea, how I could manage to do that?
Aucun commentaire:
Enregistrer un commentaire