2014-09-01 2 views
-2

UHC (Ultra Hard Core)라는 Minecraft Bukkit 플러그인을 만들고 팀을 추가 할 계획입니다. 나는 당신이 원하는 팀을 선택할 수 있기를 원하지만 그렇게하지 않으면 자동으로 팀에 합류하게됩니다.목록에 항목을 추가하여 각 목록을 균일하게 만듭니다.

팀의 최소 선수는 2 명, 최대는 4 명입니다. 예를 들어, 팀은 Red, Blue 및 Green입니다. 빨간색은 2, 파란색은 1, 녹색은 4, 파란색으로 추가됩니다.

어쨌든이 작업을 수행 하시겠습니까?

+0

간단합니다 :'blue.add (player);'. 끝난. – Tom

+0

플레이어를 최소 크기의 목록에 추가하기 만하면 ... – thkala

답변

3

먼저 플레이어 및 팀 개체를 만듭니다 (이미 이와 비슷한 것들을 가지고있을 것입니다).

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"에만 해당되는 내용은 아닙니다.

+0

Arraylists를 좋아 하시겠습니까? : D ... 호환성을 향상시키기 위해 구체적인 유형 대신 변수에 인터페이스를 사용하는 방법을 배웁니다. 모든 메서드는 LinkedList와 같은 다른 List 유형을 처리 할 수 ​​없으므로 ArrayList 대신 LinkedList를 사용하려는 경우 리팩토링을 더 많이 수행 할 수 있습니다. (http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Tom

+0

이것은 좋은 지적입니다. Tom, 감사합니다. – trooper

+0

또한 'Team' 클래스가 그의'Comparable' 인터페이스를 놓치고 있습니다 :). – Tom