0

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);
    }
}
Community
  • 1
  • 1
Alina Gabriela
  • 121
  • 1
  • 11
  • Try to define your classes properly before spending time on algorithm. Your Team class doesn't make any sense. – Francis Jan 06 '14 at 18:22
  • yes, this is what I asked mainly, how should it be defined? – Alina Gabriela Jan 06 '14 at 18:29
  • Probably, each instance of `Team` should contain only a single team's data. Right now, every time you call the `Team()` constructor, it calls itself several more times, causing an infinite recursion problem! Try having the `Team` class contain the data for only one team and then have your list of `Team` instances somewhere else. – kevinsa5 Jan 07 '14 at 15:28

2 Answers2

1

Teams class:

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

Game class:

public class Game {
public Teams teamHome;
public Teams teamAway;
public String day;

Game(){
}

Scheduler class (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;
    }
}

and Championship class, tying all together and displaying:

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();
            }
        }
}
}
Alina Gabriela
  • 121
  • 1
  • 11
0

Put your logic in another class like ScheduleGenerator that will output a Schedule. A Schedule is a class that contains a list of matches. A match is a composition of homeTeam, visitorTeam and a date. You can also add fields for scoring and match statistics. homeTeam and visitorTeam are instance of Team class as you did. A Team only has a name in your case, but could have a list of players in the team and other properties like the city the team is from. ScheduleGenerator class will all do the logic in a static function that will create all instances of matches, giving a list of team as params.

I feel that if I give you more infos, I will be doing your homework.

Francis
  • 367
  • 1
  • 10
  • Thank you for your guidelines, I tried to follow them - see the above answer. To further implement scoring and statistics - for example goals scored and conceded for every team in every match, and the afferent score, that I could afterwards sum to get statisctics (standings) should I create a new class, let's say ScoreCounter, and put there the methods that I want? or should I do it in the Game class? I'm only asking for guidelines again, not a complete solution – Alina Gabriela Jan 15 '14 at 11:24
  • For scoring, you can put it in Game with something like homeScore and awayScore. – Francis Jan 21 '14 at 17:20
  • For complete statistics, it can be more complicated. You can add the game statistics inside Game class and also compile Team stats by adding the reference to the statistics for the team and also to the player. This way you can get all the statistics about a season for a player, who is performing great in a team and highlight in a match. You will need an object or maybe more to contain statistics. – Francis Jan 21 '14 at 17:27