I am trying to create a tournament bracket system in my C# asp.net core application. I found this Tournament bracket placement algorithm post and RWC's answer is what I need because it also includes byes.
The problem I'm having is translating this piece of code to c#:
<?php
define('NUMBER_OF_PARTICIPANTS', 16);
$participants = range(1,NUMBER_OF_PARTICIPANTS);
$bracket = getBracket($participants);
var_dump($bracket);
function getBracket($participants)
{
$participantsCount = count($participants);
$rounds = ceil(log($participantsCount)/log(2));
$bracketSize = pow(2, $rounds);
$requiredByes = $bracketSize - $participantsCount;
echo sprintf('Number of participants: %d<br/>%s', $participantsCount, PHP_EOL);
echo sprintf('Number of rounds: %d<br/>%s', $rounds, PHP_EOL);
echo sprintf('Bracket size: %d<br/>%s', $bracketSize, PHP_EOL);
echo sprintf('Required number of byes: %d<br/>%s', $requiredByes, PHP_EOL);
if($participantsCount < 2)
{
return array();
}
$matches = array(array(1,2));
for($round=1; $round < $rounds; $round++)
{
$roundMatches = array();
$sum = pow(2, $round + 1) + 1;
foreach($matches as $match)
{
$home = changeIntoBye($match[0], $participantsCount);
$away = changeIntoBye($sum - $match[0], $participantsCount);
$roundMatches[] = array($home, $away);
$home = changeIntoBye($sum - $match[1], $participantsCount);
$away = changeIntoBye($match[1], $participantsCount);
$roundMatches[] = array($home, $away);
}
$matches = $roundMatches;
}
return $matches;
}
function changeIntoBye($seed, $participantsCount)
{
//return $seed <= $participantsCount ? $seed : sprintf('%d (= bye)', $seed);
return $seed <= $participantsCount ? $seed : null;
}
?>
I have tried translating each piece of PHP line to C# equivalent. However, this snippet is what stopped me at my tracks:
for($round=1; $round < $rounds; $round++)
{
$roundMatches = array();
$sum = pow(2, $round + 1) + 1;
foreach($matches as $match)
{
$home = changeIntoBye($match[0], $participantsCount);
$away = changeIntoBye($sum - $match[0], $participantsCount);
$roundMatches[] = array($home, $away);
$home = changeIntoBye($sum - $match[1], $participantsCount);
$away = changeIntoBye($match[1], $participantsCount);
$roundMatches[] = array($home, $away);
}
$matches = $roundMatches;
}
I don't understand what $roundMatches[]
is trying to accomplish. Is it recreating the array? Is it setting a pointer? No idea. The C# version I wrote gives me wrong seeding number fo each match.
public Dictionary<int, string> getBracket(Dictionary<int, string> participants)
{
Dictionary<int, string> orderedParticipants = new Dictionary<int, string>();
var count = participants.Count;
var rounds = Math.Ceiling(Math.Log(count) / Math.Log(2));
var bracketSize = Math.Pow(2, rounds);
var requiredByes = bracketSize - count;
string p = $"Number of participants: {count}";
string r = $"Number of rounds: {rounds}";
string b = $"Bracket size: {bracketSize}";
string byes = $"Required number of byes: {requiredByes}";
List<List<int>> matches = new List<List<int>>();
matches.Add(new List<int>() {1, 2});
for (int round = 1; round < rounds; round++)
{
List<int> roundMatches = new List<int>();
var sum = (int)Math.Pow(2, round + 1) + 1;
foreach (var match in matches)
{
var home = changeIntoBye(match[0], count);
var away = changeIntoBye(sum - match[0], count);
roundMatches = new List<int> {home.Value, away.Value};
//roundMatches
home = changeIntoBye(sum - match[1], count);
away = changeIntoBye(match[1], count);
}
matches.Add(roundMatches);
}
return orderedParticipants;
}