0

我正在一个项目中使用循环赛程算法生成锦标赛。这是我实现算法的类:

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);
    }
}
4

2 回答 2

1

团队班:

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();
            }
        }
}
}
于 2014-01-15T11:18:53.873 回答
0

将您的逻辑放在另一个类中,例如 ScheduleGenerator,它将输出一个 Schedule。Schedule 是一个包含匹配列表的类。一场比赛是主队、访客队和日期的组合。您还可以为得分和比赛统计添加字段。homeTeam 和 visitorTeam 和您一样是 Team 类的实例。在您的情况下,团队只有一个名称,但可能有团队中的球员列表和其他属性,例如团队所在的城市。ScheduleGenerator 类将在一个静态函数中执行所有逻辑,该函数将创建所有匹配实例,并提供一个团队列表作为参数。

我觉得如果我给你更多的信息,我会做你的功课。

于 2014-01-07T15:24:02.213 回答