Assign players to 1 of 8 teams

Discussion in 'Plugin Development' started by thecreator153, Oct 12, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    thecreator153

    Hello everyone, I'm making a UHC plugin. I'm almost done except I can't figure out a way to assign players to their teams. Forgive me if this task sounds too easy, but I've been knee deep in code for the past week since I'm also trying to integrate this plugin with a server's Game API. My brain is exhausted, and I just can't fiure out a way. There are 8 teams, and when the game starts, players are randomly assigned to their teams. How would i go about doing this?
     
  2. Offline

    CraftCreeper6

    thecreator153
    How many players in the game?

    I would:
    1) Loop through all the online players.
    2) Generate a random number
    3) Create ints for each team.
    4) If the int goes above online players.size(); / 8 then generate another random number...
    5) Add them to the team.

    Seems confusing :p
     
  3. Offline

    thecreator153

    No, it seems completely un-confusing (if that's a word) to me. Thanks a lot for the solution.
     
  4. Offline

    mine-care

  5. Offline

    teej107

  6. Offline

    CraftCreeper6

    teej107
    Uhm, I am pretty sure it will.
     
  7. Offline

    teej107

    CraftCreeper6
    It's selecting a team randomly and your not saying to do anything about not selecting the same team until all the others are selected.
     
  8. Offline

    xMrPoi

    CraftCreeper6 No it won't. It will create a random number for each player. Technically, there's a chance that every player will be assigned to the same team.

    I would do it like this:
    Code:java
    1. ArrayList<UUID> players = new ArrayList<UUID>();
    2. for(Player p:Bukkit.getOnlinePlayers())
    3. players.add(p.getUniqueId());
    4. Iterator<UUID> it = players.iterator();
    5. for(int ind = 0;ind<(players.size()/8)+1;ind++)
    6. for(int i = 1;i<=8;i++)
    7. if(it.hasNext()){
    8. //Add player to team using
    9. //Bukkit.getPlayer(it.next());
    10. }
     
  9. Offline

    coasterman10

    I would create a new list containing all the players that will be added to teams, shuffling it, then adding players to each team. Collections.shuffle(List<?>) is what you want to use. Then you can either divide up the collection and add each few players to each team, or circle through all the teams and add players one at a time.
     
  10. Offline

    CraftCreeper6

    teej107 xMrPoi
    I think you'll find that I said check if the size of the Integer is lower or equal online players / 8 ;)
     
  11. Offline

    xMrPoi

    This still won't do anything. A random number is random lol. There's no way to make sure that the teams will be equally distributed.

    You also edited your post like 30 minutes ago :p
     
  12. Offline

    CraftCreeper6

    xMrPoi
    I edited what post 30 minutes ago?
     
  13. Offline

    teej107

    That is so you could add a player to a list that the number corresponds to. Using Random.nextInt(8) or even
    ((int)(Math.random() * 10) % 8)) would be better. You didn't specify on how to generate a random number and I just listed 2 good ways, both ways not needing to check if the number is above 8.

    An equal way to distribute players among teams evenly would be:
    I had this in mind if you are storing your teams (a collection of some sort) in an ArrayList.
     
Thread Status:
Not open for further replies.

Share This Page