Answer the question
In order to leave comments, you need to log in
Why is the tournament grid not generated correctly?
Hello. I'm trying to generate a tournament bracket, below is an example so far only for an odd number of participants. I have 7 participants in the example and for 7 participants the grid is generated perfectly. But if you enter 3,5,9,11 and so on. It gives an error, I tried everything, I can not understand why.
The grid should be generated like this:
7 participants, so 21 rounds. 21 because each team will miss 1 day. There will be 3 games in the round.
I would really appreciate any help! Maybe someone will offer a solution.
const teams = [
{
'id': 1,
'player': 'Alex',
'team': 'Arsenal'
},
{
'id': 2,
'player': 'Bob',
'team': 'Real Madrid'
},
{
'id': 3,
'player': 'Jon',
'team': 'FC Bayern'
},
{
'id': 4,
'player': 'Ron',
'team': 'Atletico Madrid'
},
{
'id': 5,
'player': 'Fill',
'team': 'Roma'
},
{
'id': 6,
'player': 'Bill',
'team': 'Barcelona'
},
{
'id': 7,
'player': 'Zill',
'team': 'Man United'
}
];
createTable() {
let teamsQty = this.state.teams.length;
console.log(teamsQty);
var gamesPlayed = [];
for (let k = 1; k <= teamsQty; k++) {
gamesPlayed[k] = {
teamNum: k,
gamesPlayed: 0
};
}
var gamesMap = {};
for (let roundNum = 0; roundNum < teamsQty; roundNum++) { // round
console.log(roundNum + ' round');
gamesPlayed = gamesPlayed.sort((a, b) => {
return a.gamesPlayed < b.gamesPlayed || a.gamesPlayed > b.gamesPlayed ? -1 : 1;
});
var roundData = [];
var roundGamesPlayed = [];
for (var i = 0; i < teamsQty; i++) {
roundGamesPlayed[i] = false;
}
var gamesPerRound = Math.floor(teamsQty / 2);
for (let game = 0; game < gamesPerRound; game++) {
var teamA = null;
var teamB = null;
for (let ii = 0; ii < teamsQty; ii++) {
for (let jj = 0; jj < teamsQty; jj++) {
if (ii == jj) {
continue;
}
let roundTeamAId = gamesPlayed[ii].teamNum;
let roundTeamBId = gamesPlayed[jj].teamNum;
if (roundGamesPlayed[ii] == true || roundGamesPlayed[jj] == true) {
continue;
}
let roundKey = [roundTeamAId, roundTeamBId].sort().join(':');
if (roundKey in gamesMap) {
continue;
}
teamA = ii;
teamB = jj;
break;
}
}
roundGamesPlayed[teamA] = true;
roundGamesPlayed[teamB] = true;
var teamAId = gamesPlayed[teamA].teamNum;
var teamBId = gamesPlayed[teamB].teamNum;
roundData.push({
home: teamAId,
away: teamBId
});
gamesPlayed[teamA].gamesPlayed++;
gamesPlayed[teamB].gamesPlayed++;
var key = [teamAId, teamBId].sort().join(':');
gamesMap[key] = key in gamesMap ? gamesMap[key] + 1 : 1;
}
var roundGames = [];
roundData.forEach(function (round) {
var teamHome = _.find(teams, teamName => teamName.id === round.home);
var teamAway = _.find(teams, teamName => teamName.id === round.away);
console.log(roundNum + ' | ' + teamHome.team + ' -- ' + teamAway.team);
});
}
}
Answer the question
In order to leave comments, you need to log in
You do not track the situation when the number of games in the round exceeds the number of possible playing teams (if I understand correctly) https://jsbin.com/mezabel/13/edit?js,console
Corrected like this:
if(teamA === null || teamB === null) {
console.error(`[Warning] Round ${roundNum}, Game ${game}, teamA is ${teamA}, teamB is ${teamB}`);
break;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question