Assigning Players to teams

Discussion in 'Plugin Development' started by boboman13, Jul 10, 2013.

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

    boboman13

    I've been working with plugins for a while but came across a challenge quite recently. In the problem, I have an ArrayList<String> of player names. I also have a HashSet of Teams. (not the Bukkit teams, a custom team)
    What I need to do is, for each of these players, assign them to a team. However, the players need to be dispersed evenly among the teams (give or take one). Examples:

    We have 10 players.
    We have 3 Team instances in the set.
    4 players go to Team1, 3 players go to Team2, 3 players go to Team3.

    We have 6 players.
    We have 2 Team instances.
    3 to each team.

    It might be a simple solution but I've no idea how to do that - help please? :)

    Bump. please help :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    Sessional

    boboman13
    Hashmaps have a size() method in them - using this you can figure out how many teams you have.

    As for algorithm to solve the problem:

    make a list of players
    have a counter for team number
    while list of players size > 0:
    remove the player from the list and add it to the team represented by the counter
    increment counter by 1
    if counter > number of teams:
    reset counter to 0
    back to beginning of loop
     
  3. Offline

    !Phoenix!

    Pseudocode:
    Code:
    amountPlayers / amountTeams = playersPerTeam ("slots")
    amountPlayers % amountTeams = amountRemainingPlayers (e.g. 1 for 10 / 3)
    Add amountRemainingPlayers as free slots to team1
     
    for each team {
        for each free slot in the team (playersPerTeam) {
            team.assign(allPlayers.get(randomNumber(amountOfUnassignedPlayers)))
        }
    }
    
     
  4. Offline

    boboman13

    Exactly what I needed - thanks!

    @!Phoenix! Thanks for helping out but I understand what Sessional said more :)
     
Thread Status:
Not open for further replies.

Share This Page