Skip to content

Instantly share code, notes, and snippets.

@jimthedev
Created September 26, 2016 22:02
Show Gist options
  • Save jimthedev/76cf5e853760e5c15df6b59b8cfd6fd0 to your computer and use it in GitHub Desktop.
Save jimthedev/76cf5e853760e5c15df6b59b8cfd6fd0 to your computer and use it in GitHub Desktop.
Given an array, loop and return a winner. Uses a single elimination strategy.
/**
* Find the largest power of two below the given num
*/
function maxPow2Below(num){
return Math.pow(2, Math.floor(Math.log(num)/Math.log(2)));
}
/**
* Create an empty bracket given a number of teams entering the round
* This will prefer acheiving a square bracket by taking remainders
* into account.
*/
function createEmptyRound(numberOfTeams) {
// The remainder is the # of teams that must be eliminated in order
// to conduct a "square" tournament in the next round. Typically this
// is only going to be used in the first round.
var squareTournament = maxPow2Below( numberOfTeams );
var remainder = numberOfTeams - squareTournament;
var participants = remainder === 0 ? numberOfTeams : remainder * 2;
var byes = numberOfTeams - participants;
return {
participants: participants,
byes: byes
};
}
/**
* Apply the teams to the bracket
*/
function bindTeamsToRound(emptyBracket, teams) {
var games = [];
var participants = teams.slice(0, emptyBracket.participants);
var byes = emptyBracket.byes !== 0 ? teams.slice(emptyBracket.byes * -1) : [];
return {
byes: byes,
participants: participants
}
}
/**
* Takes a round which has teams bound to it and creates matchups
* for participants.
*/
function splitPairs(arr) {
var pairs = [];
for (var i=0 ; i<arr.length ; i+=2) {
if (arr[i+1] !== undefined) {
pairs.push ([arr[i], arr[i+1]]);
} else {
pairs.push ([arr[i]]);
}
}
return pairs;
}
function createMatchups(boundRound) {
return {
contenderCount: boundRound.byes.length + boundRound.participants.length,
byes: boundRound.byes,
matchups: splitPairs(boundRound.participants)
}
}
function submitResults(matchupRound, picksMatrix) {
var winners = [];
var losers = [];
matchupRound.matchups.map((matchup, index) => {
winners.push(matchup[picksMatrix[index]]);
losers.push(matchup[1-picksMatrix[index]]);
})
var newRoundChoices = matchupRound.byes.concat(winners);
var newRound = prepare(newRoundChoices);
return {
winners: winners,
losers: losers,
contenderCount: newRoundChoices.length,
matchups: newRound.matchups,
byes: newRound.byes
}
}
function prepare(arrayOfChoices) {
return createMatchups(bindTeamsToRound(createEmptyRound(arrayOfChoices.length), arrayOfChoices));
}
function winnerFound(roundToCheck) {
return roundToCheck.contenderCount === 1;
}
var phrases = ['hi', 'yep', 'weee', 'there', {value: 'everyone'}];
var round = prepare(phrases); // copy the array and reformat as a round
// Round loop until we have a winner!
while(!winnerFound(round)) {
// Normally the user would be giving us these.
var picks = [1, 0];
// Pick loop until the end of round
round.matchups.map((matchup, index) => {
console.log('matchup: ' + matchup[0] + ' vs ' + matchup[1]);
console.log('picking ' + matchup[picks[index]])
});
round = submitResults(round, picks);
}
var winner = round.winners[0];
console.log('winner found!', winner);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment