Minecraft: Different Amount of Teams Generator

I am currently working on a mini game that involves two teams. I'm wondering if I can make a random team generator in Minecraft that varies with the amount of players that are in the game. For example if I had 8 players, I could have the two teams have 4 players on each. But if I had 10 players, I could have 5 players on each team. Can this be done with command blocks? I know how to make teams and assign people to them already with the scoreboard command, but I don't know how I can make a player amount varied team generator.


Solution 1:

I did something like this once. I had a command block on a timer that would look for players not on a team and then teleport them to a room. Any player in that room would be assigned to either red or blue team, using a t flip flop to determine what team to put them on. The flip flop would be switched for every person assigned, so it would go red blue red blue and so on. As soon as they were assigned I'd teleport them out to the team base with another command block.

This was a LONG time ago and I can't remember any more details than that, and there might be more efficient ways to do it now. I just wish mojang would include a scripting language to command blocks because trying to tie the programming into redstone and everything is way harder than it should be and variables can't be moved between blocks. That's why I had to teleport players to a separate room to assign stuff to them.