\n";
}
function processMatch ($player1, $player2, $winsNeeded, $trial) {
global $printPairings;
if ($trial < $printPairings) {
echo "(".$player1['num'].", ".$player2['num']."), ";
}
$p1wins = 0;
$p2wins = 0;
// keep going until one player has enough wins
while ($p1wins < $winsNeeded && $p2wins < $winsNeeded) {
// roll for the winner
$result = mt_rand(1, $player1['str'] + $player2['str']);
// the strengths are simply linear, not ratings
if ($result <= $player1['str']) {
$p1wins++;
}
else {
$p2wins++;
}
}
// return the winner
if ($p1wins >= $winsNeeded) {
return $player1;
}
else {
return $player2;
}
}
// search for the proper power of 2 and then give byes
$cutoff = 1;
while ($numPlayers > $cutoff) {
$cutoff *= 2;
}
$numByes = $cutoff - $numPlayers;
for ($g = 0; $g < $numTrials; $g++) {
// make a copy of the round1 bracket so we can rerun the tournament many times
// in PHP, arrays are assigned by copy
$currentRound = $round1;
$round = 1;
if ($g < $printPairings) {
echo "
Round ".$round.": ";
}
// process byes
for ($i = 0; $i < $numByes; $i++) {
// take players and move them immediately to the next round, starting
// from the top
$byePlayer = array_splice($currentRound, 0, 1);
// array_splice returns an array
$nextRound[] = $byePlayer[0];
}
// Keep processing until current round has 1 player (the winner)
while (count($currentRound) > 1) {
// get player 1, the top remaining player
$p1 = array_splice($currentRound, 0, 1);
// array_splice returns an array
$p1 = $p1[0];
// get player 2, the bottom remaining player
$p2 = array_pop($currentRound);
// process one match and assign the winner to the next round
$nextRound[] = processMatch($p1, $p2, $winsNeeded, $g);
// if the current round is now empty, move to the next round
if (count($currentRound) == 0) {
$currentRound = $nextRound;
$nextRound = array();
$round++;
if ($g < $printPairings) {
echo " ";
// if the tournament isn't over
if (count($currentRound) > 1) {
echo "Round ".$round.": ";
}
}
}
}
// record the winner of the tournament
$winner = $currentRound[0]['num'];
// to avoid a notice, set it to 1 for the first win
// otherwise, it's fine to simply increment wins
if (array_key_exists($winner, $results)) {
$results[$winner]++;
}
else {
$results[$winner] = 1;
}
} // for each trial
// Calculate the overall average
?>
A Tournament Simulation with trials
Playing as Shadow or playing as Free doesn't change the strength values.
If player A has strength 10 and player B has strength 20, then player B has a
20/(10+20) = 20/30 = 2/3 chance to win each game. This is different than
comparing ELO ratings.
A proper bracket is used, so player 1 must be strongest, then player 2, then
player 3, etc. Ties are OK, so player 1 and player 2 could both have the same
strength.