javaarraysarraylistshufflesports-league-scheduling-problem

Creating a program that will assign teams without duplication?


I'm looking to create a program that will assign six teams to play each other once every three days. But I also want each team to play two games each day. I don't want them to play the same person twice within the same period of three days.

I need each game to be grouped by two and randomized. There will be two games playing at one time.

Here is the desired output:

Day 1:             Day 2:             Day 3:

 Team 1 -> Team 6 | Team 1 -> Team 4 | Team 1 -> Team 2
 Team 2 -> Team 5 | Team 2 -> Team 3 | Team 3 -> Team 4
 ________________ | ________________ | ________________
                  |                  |
 Team 3 -> Team 1 | Team 3 -> Team 6 | Team 5 -> Team 6
 Team 4 -> Team 2 | Team 4 -> Team 5 |
 ________________ | ________________ |
                  |                  |
 Team 5 -> Team 4 | Team 5 -> Team 1 |
 Team 6 -> Team 3 | Team 6 -> Team 2 |

It doesn't need to be formatted exactly like this, but this is just a general idea.

To begin with, I want to make so it loops three times, but I want to make sure it does not assign a number if that number had already been used in this sequence of loops.

I believe the code would be something like this:

Integer[] teams = new Integer[6];

  for (int i = 0; i < 6; i++) {
     teams[i] = i + 1;
  }

  for (int i = 0; i < 3; i++) {
     Collections.shuffle(Arrays.asList(teams)); 

     System.out.printf("Team 1 -> %s%n", teams[0]);
     System.out.printf("Team 2 -> %s%n", teams[1]);
     System.out.printf("Team 3 -> %s%n", teams[2]);
     System.out.printf("Team 4 -> %s%n", teams[3]);
     System.out.printf("Team 5 -> %s%n", teams[4]);
     System.out.printf("Team 6 -> %s%n", teams[teams.length - 1]);

     System.out.println("____________");
     System.out.println();
     System.out.println();
  }

Here's the output I'm getting with this code.

Day 1
Team 1 -> 6
Team 2 -> 2
Team 3 -> 1
Team 4 -> 5
Team 5 -> 4
Team 6 -> 3
____________


Day 2
Team 1 -> 2
Team 2 -> 4
Team 3 -> 1
Team 4 -> 5
Team 5 -> 3
Team 6 -> 6

The problems with this are:

-If the number 3 gets assigned to team 5, the number 5 should not be assigned to number 3 later.

-The number 1 cannot be assigned to team 1.

-If Team 4 gets assigned the number 5 on Day 1, it should not be assigned the same number on Day 2 or 3.

-List of teams needs to be randomized and grouped by two.

I'm not quite sure how to go about fixing these problems. Any help would be appreciated.


Solution

  • Use two loops. One inside the other. Loop to form a triangle in this shape:

    enter image description here

    You only need the white triangle. Each cell is a game. The white triangle is every unique game that could be played. Remember not to include the diagonal or a team will be playing against itself. With 6 teams you should get 15 unique games that can be played. More generally: g = n(n-1)/2

    Write a game class that takes two teams and create one for every cell. Sort that list and you can play as uniquely as is possible.