How to get onlineplayers and divide them evenly in 2:1 ratio

Discussion in 'Plugin Development' started by artish1, Jul 9, 2013.

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

    artish1

    So i've already got the onlineplayers thing
    I made this because i want to get the players name into a LIST
    Code:
    public List<String> getPlayers(){
            List<String> list = new ArrayList<String>();
           
            for(Player p : Arrays.asList(Bukkit.getOnlinePlayers())){
                list.add(p.getName());
            }
           
            return list;
        }
    And now i want to create a new method here
    Code:
    public void dividePlayers(List<String> list){
                int size = list.size();
                Random r = new Random();
     
        }
    How would i make it so that it would RANDOMLY divide the players in a 2:1 ratio, for every 2 players there will be one on the other side.
     
  2. Offline

    adam753

    Well, there's already a getPlayers method: Bukkit.getOnlinePlayers();

    As for random list creation, that's a tricky one. I would suggest writing a "shuffling" function, which takes a list and shuffles it into a random order like a deck of cards.

    - Take the list's length, L
    - Iterate L times, with an iteration count, I
    - Generate a random number, R, between 0 and L
    - Swap element R with element L
     
  3. Offline

    artish1

    I've created that one because that method returns a list, while the bukkit one returns an Array.

    And the second line is useless don't you think? i mean if it iterated for example with a list of 3 items/objects then ofcourse it would iterate it 3 times right? so I = L, so I = useless when you can just use L.
    Anyways i was honestly thinking of a for loop.
    int count = list.getSize(); list.getSize() > 0; count--;){
    Random r = new Random();
    int num = r.nextInt(list.getSize());
    newList.add(list.get(num));


    All i really want to know is how to get the players in the list and evenly divide them in a 2:1 ratio, and if 1 player or 2 is leftover than they will just be added to a default list.
     
  4. Offline

    adam753

    Splitting them into two lists is easy, but making the order random requires more thought. It would be possible to do it without a shuffling method, but you will have issues trying to do both at once like this- not least with the size of the list changing while you're trying to use it.

    My suggestion for how to split them (randomly or otherwise) would be to take the length of the list, and use that to work out how many players need to go into each list. For example, if you have 10 players in your list, you have six on team 1, three on team 2, and one left over.
     
  5. Offline

    Remi1115

    How about generating a random number from 0-2. If the number is 0, put the player in team one, and if the number is 1 or 2, put the player in team two.
    This would not guarantee a precise 2:1 ratio though. Well, not as precise as the method from Adam above.
     
  6. Offline

    Sessional

    Could always result to the shuffle the list add index 0 to team 1, index 1 to team 2, index 2 to team 2, repeat. This way you'll have approximately 2 to one. You'll end up near 2:1 ratio at all times if you have enough participants. You could always toss a few checks in to just add the remaining players to team 2 if you don't want team 1 to be as big as team 2 ever.
     
  7. Offline

    nisovin

    You can use Collections.shuffle() to randomize a list. Then just take the list size, divide by three, and add that many to your first list, and the rest to your second list.
     
  8. Offline

    AmShaegar

    Code:java
    1. List<String> l = new ArrayList<String>();
    2. for(Player p : Bukkit.getOnlinePlayers()) {
    3. l.add(p.getName());
    4. }
    5. Collections.shuffle(l);
    6. List<String> team1 = l.subList(0, l.size()/3);
    7. List<String> team2 = l.subList(l.size()/3, l.size());
     
Thread Status:
Not open for further replies.

Share This Page