먼저 플레이어 및 팀 개체를 만듭니다 (이미 이와 비슷한 것들을 가지고있을 것입니다).
public class Player {
private String name;
public Player(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
팀은 선수 목록에 불과합니다. 우리는 적어도 2 명의 플레이어가있을 때는 "완료"로, 4 명이있을 때는 "완료"로 간주 할 것입니다. 우리 팀 클래스는 compareTo를 구현하므로 팀의 크기로 팀 목록을 정렬 할 수 있습니다.
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;
}
}
이제 우리는 어떤 팀을 만들 ...
List<Team> teams = new ArrayList<Team>();
teams.add(new Team("Red Team"));
teams.add(new Team("Blue Team"));
teams.add(new Team("Green Team"));
... 그리고 몇몇 선수 수 있습니다. 모든 선수는 "노트럼"목록에서 시작해야합니다.
List<Player> noteam = new ArrayList<Player>();
for (int i = 0; i < 10; i++) {
noteam.add(new Player("Player " + i)); // create some 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);
}
}
이것은 단지 이것에 관한 한 가지 방법 일뿐입니다. 또한이 답변에 대한 내용은 "Minecraft Bukkit Plugin"에만 해당되는 내용은 아닙니다.
간단합니다 :'blue.add (player);'. 끝난. – Tom
플레이어를 최소 크기의 목록에 추가하기 만하면 ... – thkala