Arraylist player names...?

Discussion in 'Plugin Development' started by Meatiex, Nov 30, 2013.

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

    Meatiex

    I made the code below, i don't know if it will help, but I want to split the arraylist into 2 teams , How would I create a player variable for half of the arraylist or something that would work? Thanks :)
    Code:java
    1. int rand = game.size() / 2;
     
  2. Offline

    Danelli

    First off make the list an ArrayList of Strings (of player names) instead of an ArrayList of Players. If you create an ArrayList of players the players will be removed from the ArrayList if they relog.

    Second, you'll need two arraylists. One for team one the other for team two. Then iterate through the ArrayList like so:
    Code:
    for (String str: game) {
      int temp = ((int) Math.Random() * 2.0);
      if (temp == 1)
          team1.add(str);
      else
          team2.add(str);
    }
     
    //Then you'll need to make sure the teams
    //are the same size
     
    while (Math.abs(team1.size()-team2.size()) > 1) {
      if (team1.size() > team2.size()) {
          String str = team1.get(0);
          team2.add(str);
          team1.remove(str);
      }
      else {
          String str = team2.get(0);
          team1.add(str);
          team2.remove(str);
      }
    }
     
  3. Offline

    Ronbo

    Something like this would work:

    Code:text
    1. ArrayList<Player> allPlayers = new ArrayList<Player>();
    2. allPlayers.addAll(getServer().getPlayers());
    3. ArrayList<Player> team1 = new ArrayList<Player>();
    4. ArrayList<Player> team2 = new ArrayList<Player>();
    5. for(int k = 0; k < allPlayers.size() / 2; k++) {
    6. team1.add(allPlayers.remove(0));
    7. }
    8. team2.addAll(allPlayers); //at this point you've removed half the players from allPlayers


    You should probably use Strings for player name instead of the actual Player objects but you get the idea.
     
  4. Offline

    Meatiex

    @Danelli
    How do I add players to sting so I can convert from arraylist?
    Code:java
    1. static String game = new String();
    2. static String red = new String();
    3. static String blue = new String();

    Code below doesn't work...
    Code:java
    1. game.add(player);

    Thanks tho :)


    @Ronbo
    Thanks, that worked great :D
     
Thread Status:
Not open for further replies.

Share This Page