-3

I am creating a Minecraft Bukkit Plugin called UHC (Ultra Hard Core) and I am planning on adding teams. I want it so you can choose what team you are on, but if you don't, it would automatically put you on a team.

Min players for a team is 2, max is 4. So for example there is team Red, Blue, and Green. Red has 2, Blue has 1, Green has 4, it would add them to blue.

Anyway of doing this?

Beta Nyan
  • 25
  • 2

1 Answers1

3

First off I'll create some Player and Team objects (you probably have something like these already).

public class Player {
    private String name;

    public Player(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

A team is just a list of players. We will consider it "complete" when there are at least 2 players, and "full" when there are 4. Our Team class will implement compareTo so we can sort a list of teams by their size.

public class Team implements Comparable<Team> {
    private String name;
    private List<Player> players;

    public Team(String name) {
        this.name = name;
        this.players = new ArrayList<Player>();
    }

    public String getName() {
        return name;
    }

    public List<Player> getPlayers() {
        return players;
    }

    public int getSize() {
        return players.size();
    }

    public boolean isFull() {
        return (players.size() >= 4);
    }

    public boolean isComplete() {
        return (players.size() >= 2);
    }

    public void add( Player player )
    {
        players.add(player);
    }

    @Override
    public int compareTo(Team otherTeam) {
        int thisSize = getSize();
        int otherSize = otherTeam.getSize();

        return (thisSize == otherSize) ? 0 :
               (thisSize > otherSize) ? 1 : -1; 
    }
}

Now we can create some teams ...

List<Team> teams = new ArrayList<Team>();
teams.add( new Team("Red Team") );
teams.add( new Team("Blue Team") );
teams.add( new Team("Green Team") );

... and some players. All players should start off on the "noteam" list.

List<Player> noteam = new ArrayList<Player>();
for (int i = 0; i < 10; i++) {
    noteam.add( new Player("Player " + i) );  // create some players
}

To place a player onto a team ... we need to determine the smallest team that isn't full. We'll list all teams that aren't full, and then sort them by number of players.

for (Player player : noteam) {                  // add players to teams                                        
    List<Team> potentialTeams = new ArrayList<Team>();
    for (Team team : teams) {
        if (!team.isFull()) {
            potentialTeams.add(team);
        }
    }

    if (potentialTeams.isEmpty()) {
        // cannot add player because all teams are full - we could do something about it here
        break;
    } else {
        Collections.sort(potentialTeams);
        Team smallestTeam = potentialTeams.get(0);
        smallestTeam.add(player);
    }
}

This is just one way of going about this, and its only an example. Also, nothing about this answer is specific to the "Minecraft Bukkit Plugin".

trooper
  • 4,444
  • 5
  • 32
  • 32
  • You like your Arraylists, right? :D ... Learn to use Interfaces for variables instead of concrete types to enhance the compatibility. All your methods can't handle other List types (like LinkedList) therefore you have a lot more to refactor if you like to use LinkedList instead of ArrayList. (http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Tom Sep 01 '14 at 20:31
  • This is a great point Tom, thanks. – trooper Sep 01 '14 at 20:33
  • Also your `Team` class misses his `Comparable` interface :). – Tom Sep 01 '14 at 20:33
  • You should never use arrays ;). – Tom Sep 01 '14 at 20:34