我正在一个项目中使用循环赛程算法生成锦标赛。这是我实现算法的类:
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;
}
}
该schedule()
方法可以很好地显示 my 的锦标赛时间表Team teamlist
,它Arraylist
包含 12 个字符串(12 个团队名称),下面是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);
}
}