[SNIPPET] Team Joining Algorithm

Discussion in 'Resources' started by TheKomputerKing, Feb 15, 2014.

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

    TheKomputerKing

    Some people were asking me about even team choosing, so I wrote this small snippet for them:

    Code:java
    1. public void teamMatch(){
    2. if (red.size() > blue.size()){
    3. // Add to blue team
    4. } else if (blue.size() > red.size()){
    5. // Add to red team
    6. } else {
    7. if (new Random().nextBoolean()){
    8. // Add to red team
    9. } else {
    10. // Add to blue team
    11. }
    12. }
    13. }


    It's not much but it could help some people ;)
     
  2. Offline

    TomTheDeveloper

    Don't you want the players be able to choose a team theirselves like Annihilation on Shotbow?
     
  3. Offline

    TheKomputerKing

    TomTheDeveloper

    They can code that themselves, this is just an algorithm for automatic team joining ;)
     
  4. Offline

    macguy8

    TheKomputerKing
    So you're asking someone who needs a tutorial for something like this to then go and make something 2x more advanced all on their own?
     
  5. Offline

    TheKomputerKing

    macguy8

    That's not advanced, it's just team.add(Player)
     
  6. Offline

    macguy8

    TheKomputerKing

    It's advanced if you want to prevent 19 reds vs 1 blues.
     
  7. Offline

    Retherz_

    If you don't want that you use something like this...
     
    TheKomputerKing likes this.
  8. Offline

    TheKomputerKing

    macguy8

    They can use the basic concept of this algorithm to do that.
     
  9. Offline

    macguy8

    Retherz_
    What if someone wants to have random team joining & selection.

    TheKomputerKing
    Although that would work, there'd be no way for 2 players to switch teams otherwise :)
     
  10. Offline

    Retherz_

    That is really simple, check if the teams size are equals, if they are let them select, otherwise you assign them to the smallest team, very simple..
     
    TheKomputerKing likes this.
  11. Offline

    macguy8

    Retherz_
    I am aware that and it's very simple, however for the level of programming someone reading this guide might be on, such a topic could be very advanced to them.
     
  12. Offline

    Retherz_

    If assigning people to teams is your problem, you should not make anything with teams unless you can figure out how to fix it.
     
  13. Offline

    TheKomputerKing

    macguy8

    I didn't call this snippet all-in-one team selection system, I just wrote it as a way for letting people randomly put people in teams. If somebody wishes to do what you're talking about, they can find that somewhere else.
     
  14. Offline

    Retherz_

    Code:java
    1.  
    2. public enum Team {
    3. RED,
    4. BLUE //very creative.
    5. }
    6.  
    7. public void joinTeam(Team, Player) {
    8. if(Team == Team.RED && redTeam.size() < blueTeam.size()) {
    9. redTeam.add(player.getName());
    10. return;
    11. } else if(Team == Team.BLUE && redTeam.size() > blueTeam.size()) {
    12. blueTeam.add(player.getName());
    13. return;
    14. } else {
    15. if(new Random().nextBoolean()) {
    16. redTeam.add(player.getName());
    17. } else {
    18. blueTeam.add(player.getName());
    19. }
    20. }
    21.  
    22. }
    23.  


    You probably want to use methods to assign players to teams instead, less messy if you want to do messages, give items etc
     
  15. Offline

    macguy8

    Retherz_ This is a guide for people who don't know and want to learn, not for those who do.
     
  16. Offline

    Retherz_

    macguy8 ok

    macguy8 Why do you think i posted that?
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  17. Offline

    Scizzr

    Here's some code I posted a few months back. It's still fully functional, and a really simple way to make teams and balance the selection.

    Code:
    public class Team {
        private static List<Team> teams  = new ArrayList<Team>();
       
        public static final Team RED = new Team("Red", "&c");
        public static final Team BLUE = new Team("Blue", "&9");
        public static final Team GREEN = new Team("Green", "&a");
        public static final Team YELLOW = new Team("Yellow", "&e");
        //add more if needed, following the same format
       
        private String name;
        private String prefix;
        private List<String> players = new ArrayList<String>();
       
        public Team(String name, String prefix) {
            this.name = name;
            this.prefix = prefix;
            teams.add(this);
        }
       
        public String getRawName() {
            return this.name;
        }
       
        public String getRawPrefix() {
            return this.prefix;
        }
       
        public String getName() {
            return getPrefix() + this.name;
        }
       
        public String getPrefix() {
            return ChatColor.translateAlternateColorCodes('&', this.prefix);
        }
       
        public void addPlayer(Player player) {
            players.add(player.getName());
        }
       
        public void removePlayer(Player player) {
            players.remove(player.getName());
        }
       
        public List<String> getPlayers() {
            return this.players;
        }
       
        public static Team getSmallestTeam() {
            //smallest team, starting with a default team
            Team teamMin = Team.RED;
           
            //loop through all teams and update the smallest team if needed
            for (Team team : teams) {
                if (team.getPlayers().size() < teamMin.getPlayers().size()) {
                    teamMin = team;
                }
            }
           
            //return the smallest team
            return teamMin;
        }
       
        public static Team getPlayerTeam(Player player) {
            for (Team team : teams) {
                if (team.getPlayers().contains(player.getName())) {
                    return team;
                }
            }
            return null;
        }
    }
    
    Once that's made, you can use this bit of code to equally add them to a team.
    Code:
    Player player = ???; //player we are referencing
    Team.getSmallestTeam().addPlayer(player);
    
    And this code to get the team a player is in
    Code:
    Player player = ???; //player we are referencing
    Team team = Team.getPlayerTeam(player);
    if (team != null) {
        //player is in a team
    } else {
        //player is not in any team
    }
    
    Or this code to remove them from their team
    Code:
    Player player = ???; //player we are referencing
    Team team = Team.getPlayerTeam(player);
    if (team != null) {
        team.removePlayer(player);
    } else {
        //player is not in any team
    }
    
    Enjoy!
     
    TheKomputerKing likes this.
  18. Offline

    SoThatsIt

    You shouldnt create a new Random every time you want to add someone to a team. you should create a

    Code:java
    1. public static final Random r = new Random();


    in your main class and then whenever you need something random do MAINCLASS.r.nextBoolean();
     
Thread Status:
Not open for further replies.

Share This Page