Team sorting using Hashmaps and Arrays

Discussion in 'Plugin Development' started by i3ick, Apr 1, 2014.

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

    i3ick

    Hey, I sort players into teams by first sorting them into arrays and then according tho them I put them into hashmaps. However all players end up being in the red team. Any ideas why?

    Code:java
    1. for (String p: arena.getPlayers()) {
    2. if(reda.size() > greena.size()){
    3. greena.add(p);
    4. }
    5. else{
    6. reda.add(p);
    7. }
    8.  
    9. }
    10.  
    11. for(String p: arena.getPlayers()){
    12. if(reda.contains(p)){
    13. arena.GetHash().put(p, Team.RED);
    14. }
    15. else{
    16. arena.GetHash().put(p, Team.GREEN);
    17. }
    18. }



    edit: Sorry, I wanted to link GitHub but it's not working for me atm
     
  2. Offline

    Wizehh

    In your primary for loop, you're testing if the red size is more than the green size; if is, you add the string to the red team. Are you sure this what you want? Are you trying to randomize the teams?
     
  3. Offline

    i3ick

    Omg, thanks!
    I didn't want to add the string but the player.
     
  4. Offline

    Wizehh

    Has the issue been resolved? Or do you still need help?
     
  5. Offline

    i3ick

    Wizehh
    Hmm, it's still not working

    Code:
    for (String p: arena.getPlayers()) {
            Player pl = Bukkit.getPlayer(p);
                if(reda.size() > greena.size()){
                    greena.add(pl.getName());
                }
                else{
                    reda.add(pl.getName());
                }
             
        }
     
        for(String p: arena.getPlayers()){
            Player pl = Bukkit.getPlayer(p);
                if(reda.contains(p)){
                    arena.GetHash().put(pl.getName(), Team.RED);
                }
                else{
                    arena.GetHash().put(pl.getName(), Team.GREEN);
                }
            }
     
  6. Offline

    Wizehh

    Can you further explicate your problem? Are you trying to randomize the teams? If so, you could use a Random#nextBoolean(), and then add them to the red team if the value is true, or green team if the value is false.
     
  7. Offline

    i3ick

    Wizehh
    Mistake in code that I fixed 'if(reda.contains(p))' changed to 'if(reda.contains(pl.getName()))'
    Now players are all green team.

    I want to sort the players in two teams and I don't want to randomize it, I want them to be roughly the same size. That's why I use the for loop with arrays. Once all players are put into red and green arrays I use another loop to put them into a hashmap and assign them to a team according to the array they are already because of the previous loop
     
  8. Offline

    Wizehh

    Ah, I see. Can you post the rest of the code (that's pertinent to this)?
     
  9. Offline

    i3ick

    Wizehh
    For arrays I just created two arraylists in the same class:
    private ArrayList<String> greena = new ArrayList<String>();
    private ArrayList<String> redaa = new ArrayList<String>();

    and 'areana' gets an instance of another class so I get a hashmap from there using:
    private HashMap<String, Team> players = new HashMap<String, Team>();


    public HashMap<String, Team> GetHash() {
    return players;
    }


    GitHub (from line 142): https://github.com/i3ick/Winter-Sla...lash/src/me/i3ick/com/WinterSlashManager.java
     
Thread Status:
Not open for further replies.

Share This Page