sort list of matches into unique rounds

Viewed 25

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)

enter image description here

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:

  1. each team plays just one match per round
  2. a match planned in a round cannot occur in a following round
  3. 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...)

0 Answers
Related