ラウンド ロビン スケジュール アルゴリズムを使用してトーナメントを生成するプロジェクトに取り組んでいます。アルゴリズムを実装したクラスは次のとおりです。
public class Matchh {
public Team teamHome;
public Team teamAway;
public int teamHomeGoals;
public int teamAwayGoals;
public String matchDay;
public int noOfTeams;
public String[][] rounds;
public String[][] round;
Team teamList = new Team();
// no-arg constructor
Matchh() {
}
Matchh(String matchDay, Team teamHome, Team teamAway, int teamHomeGoals, int teamAwayGoals) {
this.matchDay = matchDay;
this.teamHome = teamHome;
this.teamAway = teamAway;
this.teamHomeGoals = teamHomeGoals;
this.teamAwayGoals = teamAwayGoals;
}
// round robin schedule method
public String[][] schedule() {
this.rounds = new String[(teamList.getSize() - 1) * 2][(teamList.getSize() / 2)];
for (int round = 0; round < (teamList.getSize() - 1) * 2; round++) {
for (int match = 0; match < (teamList.getSize() / 2); match++) {
this.teamHome = teamList.getIndex((round + match) % (teamList.getSize() - 1));
this.teamAway = teamList.getIndex((teamList.getSize() - 1 - match + round) % (teamList.getSize() - 1));
// Last team stays in the same place while the others rotate around it.
if (match == 0) {
teamAway = teamList.getIndex(teamList.getSize() - 1);
}
// from rounds half interchange the position of teams in rounds, to get both home and away matches
String mixedRounds;
if (round < (teamList.getSize() - 1)) {
mixedRounds = (teamHome + " vs " + teamAway + " " + teamHome.getGoal() + " - " + teamAway.getGoal());
} else {
mixedRounds = (teamAway + " vs " + teamHome + " " + teamAway.getGoal() + " - " + teamHome.getGoal());
}
rounds[round][match] = mixedRounds;
}
}
return rounds;
}
}
このメソッドは、 12 個の文字列 (12 個のチーム名) を含むschedule()
のトーナメント スケジュールを表示するために正常に機能しています。以下は 、理解を深めるためのクラスです。しかし、上記のクラスが定義されている方法を考えると、別のクラスのさまざまなプロパティを呼び出す可能性はありません。たとえば、特定のチームの合計ゴール数が必要な場合は、 のようなメソッドを呼び出したいと思います。Team teamlist
Arraylist
Team
getTeamHomeGoals()
私がやろうとしてきたことは、schedule()
メソッドをバラバラに「分割」することです: メソッドを定義setTeamHome()
しsetTeamAway()
、それぞれにランダムな目標を生成し、getMatchDay()
メソッドを作成し、各ラウンドを、、、をMatchh
含むオブジェクトとして構築します。teamHome
teamAway
teamHomeGoals
teamAwayGoals
matchDay
これまでのところ、次のメソッドがあります(意図したものを返していません):
//get match day, matches ar held each week Wednesday and Sunday - we start with a Wednesday
public String getMatchDay() {
for (int round = 0; round < (teamList.getSize() - 1) * 2; round++) {
if (round % 2 == 0) {
this.matchDay = ("Wednesday" + (round + 2) / 2);
} else {
this.matchDay = ("Sunday" + (round + 1) / 2);
}
}
return matchDay;
}
//teamHome
public Team getTeamHome() {
for (int round = 0; round < (teamList.getSize() - 1) * 2; round++) {
for (int match = 0; match < (teamList.getSize() / 2); match++) {
this.teamHome = teamList.getIndex((round + match) % (teamList.getSize() - 1));
}
}
return teamHome;
}
必要なものを取得するためにクラスをどのように構築する必要があるかについて、アドバイスをお願いしますMatchh
。これは、一致のさまざまなプロパティをリンクし、メソッドを「分割」するschedule()
方法です。
ここにもTeam
クラスがあります。
//team class
import java.util.ArrayList;
import java.util.Random;
public class Team {
// the name of the team object
private String name;
public ArrayList<Team> teamList;
//no-arg constructor, creates the array list of default teams
Team() {
this.teamList = new ArrayList<Team>();
teamList.add(new Team("Brondby IF"));
teamList.add(new Team("AaB"));
teamList.add(new Team("Viborg FF"));
teamList.add(new Team("Esbjerg"));
teamList.add(new Team("FC Copenhagen"));
teamList.add(new Team("Randers FC"));
teamList.add(new Team("FC Midtjylland"));
teamList.add(new Team("FC Nordsjaelland"));
teamList.add(new Team("Odense BK"));
teamList.add(new Team("AGF Aarhus"));
teamList.add(new Team("FC Vestsjaelland"));
teamList.add(new Team("Sonderjyske"));
}
//constructor using name
Team(String name) {
this.name = name;
}
//get name of team
public String getName() {
return name;
}
//get the size of the arrayList
public int getSize() {
return teamList.size();
}
//get an element at a specific index i
public Team getIndex(int i) {
return teamList.get(i);
}
}