├── cyclic_algo.js ├── even.png ├── odd.png └── readme.md /cyclic_algo.js: -------------------------------------------------------------------------------- 1 | 2 | function pairingsTeams(numOfTeams, numOfRounds) { 3 | 4 | var pairs = []; 5 | var teamHAObj = {}; 6 | var teamGObj = {}; 7 | var roundObj = {}; 8 | 9 | var isOddTeam = numOfTeams % 2 == 0 ? false : true; 10 | var bye = -1; 11 | var home, away; 12 | var generatePairs = function(){ 13 | var pairNum = Math.floor( (numOfTeams - 1) / 2); 14 | 15 | // Initialize the list of teams 16 | var teams = Array(numOfTeams).fill().map((val,idx) => idx + 1); 17 | // if (isOddTeam) teams.push(bye); 18 | if (isOddTeam) teams.unshift(bye); 19 | 20 | for (var r = 1; r < numOfRounds+1; r++) { 21 | var rndN = `round ${r}`; 22 | roundObj[rndN] = []; 23 | for (var k = 0; k < pairNum; k++) { 24 | if ((pairNum - k) % 2 != 0) { 25 | home = teams[pairNum - k]; 26 | away = teams[pairNum + k + 1]; 27 | } else { 28 | home = teams[pairNum + k + 1]; 29 | away = teams[pairNum - k]; 30 | } 31 | 32 | pairs.push(`${home},${away}`); 33 | roundObj[rndN].push(`${home},${away}`); 34 | } 35 | 36 | if (!isOddTeam) { 37 | if (r % 2 == 0){ 38 | home = teams[0]; 39 | away = teams[teams.length - 1]; 40 | } else { 41 | home = teams[teams.length - 1]; 42 | away = teams[0]; 43 | } 44 | 45 | pairs.push(`${home},${away}`); 46 | roundObj[rndN].push(`${home},${away}`); 47 | } 48 | 49 | 50 | // rotate team array 51 | var last = teams.pop(); 52 | teams.splice(1, 0, last); 53 | } 54 | } 55 | 56 | var spliteHomeAway = function(){ 57 | pairs.forEach(function(i) { 58 | var key = i; 59 | let away = key.split(',')[1]; 60 | let home = key.split(',')[0]; 61 | 62 | if (!teamHAObj[away]) { 63 | teamHAObj[away] = {home:0, away: 0}; 64 | } 65 | if (!teamHAObj[home]) { 66 | teamHAObj[home] = {home:0, away: 0}; 67 | } 68 | 69 | teamHAObj[away]['away'] += 1; 70 | teamHAObj[home]['home'] += 1; 71 | }) 72 | } 73 | 74 | var gamesPerTeam = function(){ 75 | pairs.forEach(function(i) { 76 | var key = i; 77 | let away = key.split(',')[1]; 78 | let home = key.split(',')[0]; 79 | 80 | if (!teamGObj[away]) { 81 | teamGObj[away] = {[home]: 0} 82 | } 83 | 84 | if (!teamGObj[home]) { 85 | teamGObj[home] = {[away]: 0} 86 | } 87 | 88 | if (!teamGObj[away][home]) { 89 | teamGObj[away][home] = 0 90 | } 91 | 92 | if (!teamGObj[home][away]) { 93 | teamGObj[home][away] = 0 94 | } 95 | 96 | teamGObj[away][home] += 1; 97 | teamGObj[home][away] += 1; 98 | }) 99 | } 100 | 101 | generatePairs(); 102 | spliteHomeAway(); 103 | gamesPerTeam(); 104 | 105 | console.log(">>>>>>>>>>>>>>>>>>>>>>>"); 106 | console.log(`teams: ${numOfTeams}, rounds: ${numOfRounds}`); 107 | console.log("Pairs >>>>>>>>>>>>>>>>>"); 108 | console.log(pairs); 109 | console.log("Rounds Breakdown >>>>>>"); 110 | console.log(roundObj); 111 | console.log("Home/Away Breakdown >>>"); 112 | console.log(teamHAObj); 113 | console.log("number of games with other teams per team >>>"); 114 | console.log(JSON.stringify(teamGObj, null, 2)); 115 | } 116 | 117 | // pairingsTeams(11,9); 118 | // pairingsTeams(4,11); 119 | // pairingsTeams(8,4); 120 | pairingsTeams(5,10); -------------------------------------------------------------------------------- /even.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jsguru-git/round_robin_scheduler/76392f3e771c3f5f8da35883a5d4af6b5c3edfcf/even.png -------------------------------------------------------------------------------- /odd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jsguru-git/round_robin_scheduler/76392f3e771c3f5f8da35883a5d4af6b5c3edfcf/odd.png -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Round robin tournament generator 2 | 3 | ## Round robin for **even** number of teams 4 | ![round robin for even number of teams](even.png) 5 | ``` 6 | { 7 | 'round 1': [ '4,3', '2,5', '6,1' ], 8 | 'round 2': [ '3,2', '6,4', '1,5' ], 9 | ... 10 | } 11 | ``` 12 | 13 | ## Round robin for **odd** number of teams 14 | ![round robin for odd number of teams](odd.png) 15 | ``` 16 | { 17 | 'round 1': [ '3,4', '5,2', '1,6' ], 18 | 'round 2': [ '2,3', '4,1', '7,5' ], 19 | ... 20 | } 21 | ``` --------------------------------------------------------------------------------