-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobin.js
More file actions
30 lines (29 loc) · 918 Bytes
/
robin.js
File metadata and controls
30 lines (29 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const DUMMY = -1;
// https://github.com/clux/roundrobin/blob/master/robin.js
// returns an array of round representations (array of player pairs).
// http://en.wikipedia.org/wiki/Round-robin_tournament#Scheduling_algorithm
let generateLeague = function (n, ps) { // n = num players
var rs = []; // rs = round array
if (!ps) {
ps = [];
for (var k = 1; k <= n; k += 1) {
ps.push(k);
}
} else {
ps = ps.slice();
}
if (n % 2 === 1) {
ps.push(DUMMY); // so we can match algorithm for even numbers
n += 1;
}
for (var j = 0; j < n - 1; j += 1) {
rs[j] = []; // create inner match array for round j
for (var i = 0; i < n / 2; i += 1) {
if (ps[i] !== DUMMY && ps[n - 1 - i] !== DUMMY) {
rs[j].push([ps[i], ps[n - 1 - i]]); // insert pair as a match
}
}
ps.splice(1, 0, ps.pop()); // permutate for next round
}
return rs;
};