I am working at a project to generate a tournament using the round robin schedule algorithm. Here is the class where I implemented the algorithm:
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;
}
}
The schedule()
method is working fine for displaying a tournament schedule for my Team teamlist
, which is an Arraylist
containing 12 strings (12 team names), bellow is the Team
class for a better understanding. But given the way the above class is defined, I don't have the posibility to call the different properties in another class - for example if I want the total number of goals for a specific team, I would like to call a method like getTeamHomeGoals()
.
What I've been trying to do is to "break" the schedule()
method into pieces: to define setTeamHome()
and setTeamAway()
methods, generate random goals for each, create a getMatchDay()
method and built each round as a Matchh
object containing teamHome
, teamAway
, teamHomeGoals
, teamAwayGoals
, matchDay
.
so far I have the following methods (which are not returning what I intended to):
//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;
}
Please give me some advices on how I should structure my Matchh
class to obtain what I want, which is to link together the different properties of a match and maybe how to "break" the schedule()
method.
Here is also Team
class, I mentioned above
//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);
}
}