我正在开发一个使用循环赛时间表算法生成锦标赛的项目。这是我实现算法的类:
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()
方法可以很好地显示我的 Team teamlist
的锦标赛日程,这是一个包含 12 个字符串(12 个团队名称)的 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);
}
}
最佳答案
团队类别:
public class Teams {
// the name of the team object
private String teamName;
Teams (String name){
this.teamName =name;
}
public String getName(){
return teamName;
}
@Override
public String toString(){
return teamName;
}
}
游戏类:
public class Game {
public Teams teamHome;
public Teams teamAway;
public String day;
Game(){
}
调度程序类(ScheduleGenerator)
public class Scheduler {
public String[][] rounds;
// round robin schedule method
public String[][] schedule(ArrayList<Teams> list){
this.rounds = new String[(list.size()-1)*2][(list.size() / 2)];
for (int round = 0; round < (list.size()-1)*2; round++) {
for (int match = 0; match < (list.size() / 2); match++) {
Game game = new Game();
game.teamHome = list.get((round + match) % (list.size() - 1));
game.teamAway = list.get((list.size() - 1 - match + round) % (list.size() - 1));
// Last team stays in the same place while the others rotate around it.
if (match == 0) {
game.teamAway = list.get(list.size() - 1);
}
// from rounds half interchange the position of teams in rounds, to get both home and away matches
String mixedRounds;
if (round < (list.size() - 1)) {
mixedRounds = ( game.teamHome + " vs " + game.teamAway );
} else
//interchange the place of teams from half ((teamList.size() - 1)
{
mixedRounds = (game.teamAway + " vs " + game.teamHome);
}
rounds[round][match] = mixedRounds;
}
}
return rounds;
}
}
和冠军级,将所有内容捆绑在一起并显示:
import java.util.ArrayList;
import java.util.Arrays;
public class Championship {
public static ArrayList<Teams> teamList = new ArrayList<Teams>();
public static Scheduler schedule = new Scheduler();
Championship(){
}
//static ArrayList<Teams> teamList = new ArrayList<Teams>();
public void createDefaultList(int noOfTeams){
for(int i=0;i<noOfTeams;i++)
{
Championship.teamList.add(new Teams("Team "+ (i+1)));
}
}
public void createDanishLeagueList(){
teamList.add(new Teams("Brondby IF"));
teamList.add(new Teams("AaB"));
teamList.add(new Teams("Viborg FF"));
teamList.add(new Teams("Esbjerg"));
teamList.add(new Teams("FC Copenhagen"));
teamList.add(new Teams("Randers FC"));
teamList.add(new Teams("FC Midtjylland"));
teamList.add(new Teams("FC Nordsjaelland"));
teamList.add(new Teams("Odense BK"));
teamList.add(new Teams("AGF Aarhus"));
teamList.add(new Teams("FC Vestsjaelland"));
teamList.add(new Teams("Sonderjyske"));
}
public static void main(String[] args) {
Championship championship = new Championship();
//default teams (team1, team2, etc)
championship.createDefaultList(4);
//danish league teams
//championship.createDanishLeagueList();
String[][] fixtures = schedule.schedule(teamList);
// Display the schedule
for (int i = 0; i < (teamList.size() - 1)*2; i++) {
if (i%2 == 0){
System.out.println("Wednesday " + (i + 2)/2);
}
else {
System.out.println("Sunday " + (i+1)/2);
}
for (int j = 0; j < (teamList.size()/2); j++){
System.out.println(Arrays.asList(fixtures[i][j]));
System.out.println();
}
}
}
}
关于java - 如何使用 OOP 构造一个实现循环算法的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20956462/