Skip to content

Commit

Permalink
factor out of groupstage
Browse files Browse the repository at this point in the history
  • Loading branch information
clux committed Oct 23, 2013
0 parents commit 4c49580
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2013 Eirik Albrigtsen

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Round Robin [![Build Status](https://secure.travis-ci.org/clux/roundrobin.png)](http://travis-ci.org/clux/roundrobin) [![Dependency Status](https://david-dm.org/clux/roundrobin.png)](https://david-dm.org/clux/roundrobin)

A simple round robin match scheduler to aid tournament implementations. The round robin implementation is [translated directly from the idea outlined on wikipedia](http://en.wikipedia.org/wiki/Round-robin_tournament#Scheduling_algorithm).

## Usage
Simply give the number of players (with an optional players array), and it will spit out the array of rounds necessary:

```js
var robin = require('roundrobin');
robin(6);
[ [ [ 1, 6 ], [ 2, 5 ], [ 3, 4 ] ],
[ [ 1, 5 ], [ 6, 4 ], [ 2, 3 ] ],
[ [ 1, 4 ], [ 5, 3 ], [ 6, 2 ] ],
[ [ 1, 3 ], [ 4, 2 ], [ 5, 6 ] ],
[ [ 1, 2 ], [ 3, 6 ], [ 4, 5 ] ] ]

// or with names supplied
robin(6, ['clux', 'lockjaw', 'pibbz', 'xeno', 'e114', 'eclipse']);
[ [ [ 'clux', 'eclipse' ], [ 'lockjaw', 'e114' ], [ 'pibbz', 'xeno' ] ],
[ [ 'clux', 'e114' ], [ 'eclipse', 'xeno' ], [ 'lockjaw', 'pibbz' ] ],
[ [ 'clux', 'xeno' ], [ 'e114', 'pibbz' ], [ 'eclipse', 'lockjaw' ] ],
[ [ 'clux', 'pibbz' ], [ 'xeno', 'lockjaw' ], [ 'e114', 'eclipse' ] ],
[ [ 'clux', 'lockjaw' ], [ 'pibbz', 'eclipse' ], [ 'xeno', 'e114' ] ] ]
```

## Installation
Install from npm:

```bash
$ npm install roundrobin --save
```

## License
MIT-Licensed. See LICENSE file for details.
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "roundrobin",
"description": "A round-robin scheduler used in different tournaments",
"author": "Eirik Albrigtsen <analsandblaster@gmail.com>",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "clux/roundrobin"
},
"keywords": ["round", "robin", "tournament"],
"main": "robin.js",
"scripts": {
"test": "tap test/*.js"
},
"dependencies": {},
"devDependencies": {
"interlude": "~1.0.2",
"tap": "~0.4.4"
},
"bugs": {
"url": "http://github.com/clux/roundrobin/issues"
},
"license": "MIT"
}
27 changes: 27 additions & 0 deletions robin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const DUMMY = -1;
// returns an array of round representations (array of player pairs).
// http://en.wikipedia.org/wiki/Round-robin_tournament#Scheduling_algorithm
module.exports = 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);
}
}

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;
};
40 changes: 40 additions & 0 deletions test/robin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var tap = require('tap')
, test = tap.test
, $ = require('interlude')
, robin = require('../');

test("robin", function (t) {
$.range(20).forEach(function (n) {
var rs = robin(n);
var expected = ($.odd(n)) ? n : n-1;
t.equal(expected, rs.length, "correct number of rounds");

var pMaps = [];
$.range(n).forEach(function (p) {
pMaps[p] = [];
});

rs.forEach(function (rnd, r) {
t.equal(rnd.length, Math.floor(n/2), "number of matches in round "+ (r+1));

var plrs = $.flatten(rnd);
t.deepEqual(plrs, $.nub(plrs), "players listed only once per round");

// keep track of who everyone is playing as well
rnd.forEach(function (p) {
var a = p[0]
, b = p[1];
pMaps[a].push(b);
pMaps[b].push(a);
});
});

Object.keys(pMaps).forEach(function (p) {
var val = pMaps[p].sort($.compare());
var exp = $.delete($.range(n), Number(p));
// if this true, then each play all exactly once by previous test
t.deepEqual(val, exp, "player " + p + " plays every enemy");
});
});
t.end();
});

0 comments on commit 4c49580

Please sign in to comment.