Creating a program that will assign teams without duplication?

1k Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

0
On

Here are some possible combinations. If you don't need the assignments to be random or different every time, you could just always use the last example. Otherwise, I think if you create 6 arrays and then fill one column at a time then you'll get it. I sometimes had to go back and change a previous assignment in the same column if there were no more valid assignments available, so your algorithm may need to do that as well. If this makes no sense yet then I recommend writing out more team assignment combinations yourself to gain a better intuitive sense of the problem.

1 >> 2, 3, 4, 5, 6
2 >> 1, 4, 5, 6, 3
3 >> 5, 1, 6, 4, 2
4 >> 6, 2, 1, 3, 5
5 >> 3, 6, 2, 1, 4
6 >> 4, 5, 3, 2, 1

1 >> 2, 3, 4, 5, 6
2 >> 1, 5, 6, 4, 3
3 >> 4, 1, 5, 6, 2
4 >> 3, 6, 1, 2, 5
5 >> 6, 2, 3, 1, 4
6 >> 5, 4, 2, 3, 1

1 >> 6, 5, 4, 3, 2
2 >> 1, 6, 5, 4, 3
3 >> 2, 1, 6, 5, 4
4 >> 3, 2, 1, 6, 5
5 >> 4, 3, 2, 1, 6
6 >> 5, 4, 3, 2, 1