Reputation: 219
So I have 3 to 8 players, I want each one to play against all the others 1 vs 1. The matches are done sequentially one at a time. The ordering within a pair isn't important (a vs b == b vs a).
I'm creating the matchup pairs in a simple nested loop, in which I loop over the players and pair the current index with all the following indexes.
The problem is that player 1 will play all his matches and then have nothing to do, while player 8 will have to wait.
Is there any way to evenly distribute the pairs, so all players have equal downtime?
I'm currently randomizing the array containing the pairs, but I'm looking for a better way.
Upvotes: 3
Views: 160
Reputation: 9117
You can use a Round-robin tournament scheduling algorithm.
The circle method is the standard algorithm to create a schedule for a round-robin tournament. All competitors are assigned to numbers, and then paired in the first round:
Round 1. (1 plays 14, 2 plays 13, ... )
1 2 3 4 5 6 7 14 13 12 11 10 9 8
then fix one of the contributors in the first or last column of the table (number one in this example) and rotate the others clockwise one position
Round 2. (1 plays 13, 14 plays 12, ... )
1 14 2 3 4 5 6 13 12 11 10 9 8 7
Round 3. (1 plays 12, 13 plays 11, ... )
1 13 14 2 3 4 5 12 11 10 9 8 7 6
until you end up almost back at the initial position
Round 13. (1 plays 2, 3 plays 14, ... )
1 3 4 5 6 7 8 2 14 13 12 11 10 9
Upvotes: 3
Reputation: 165
Save the matchups in order in an array, then shuffle it like so
function shuffle(a) {
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
}
And then use it like this
var myArray = ['1','2','3','4','5','6','7','8','9'];
shuffle(myArray);
Taken from Here
Upvotes: 0