I'm working on a 'competition manager' application and run into some trouble sorting the matches in a competition.
Each team needs to play against all other teams, once home and once away.
I already created the list with all the matches in a given competition by the following code:
List<Match> matches = new ArrayList<>();
List<Team> competitors = new ArrayList<>(comp.getCompetitors());
for (Iterator<Team> itt = competitors.iterator(); itt.hasNext(); ) {
Team team = itt.next();
List<Team> validCompetitors = competitors.stream().filter(t -> !t.equals(team)).collect(Collectors.toList());
for (Team competitor : validCompetitors) {
matches.add(new Match(team, competitor));
matches.add(new Match(competitor, team));
}
itt.remove();
}
and with that the list with matches looks like this (with a list of 6 teams)
I had some not working solutions but those were scraped, deleted, they're gone so i don't have an example to lead from here. My apologies...
So how would i sort my list of matches with the following constraints:
- each team plays just one match per round
- a match planned in a round cannot occur in a following round
- A round consists (in this example) of 3 matches
(Pseudo) code examples are welcome but descriptive solutions are also very much appreciated (maybe even more...)
