Skip to content

Instantly share code, notes, and snippets.

@aerickson14
Created March 20, 2015 03:23
Show Gist options
  • Save aerickson14/818b43e6c7d95d323de3 to your computer and use it in GitHub Desktop.
Save aerickson14/818b43e6c7d95d323de3 to your computer and use it in GitHub Desktop.
March Madness Predictor
<?
function createDivision($divSize) {
$matches = array();
for($i=1; $i<=$divSize/2; $i++) {
$j = $divSize-$i + 1;
$matches[] = [$i,$j];
}
return $matches;
}
function displayMatch($i, $j, $w) {
echo "<div class='match'>";
echo "<div>".$i."</div>";
echo "<div><span>vs</span><span class='".($w == max($i,$j) ? "upset":"")."'>".$w."</span></div>";
echo "<div>".$j."</div>";
echo "</div>";
}
function predictRound($matches) {
$nextRoundMatches = array();
while(sizeof($matches) > 0) {
$m1 = array_shift($matches);
$w1 = pickWinner($m1[0], $m1[1]);
displayMatch($m1[0], $m1[1], $w1);
if (sizeof($matches) > 0) {
$m2 = array_pop($matches);
$w2 = pickWinner($m2[0], $m2[1]);
displayMatch($m2[0], $m2[1], $w2);
$nextRoundMatches[] = [$w1, $w2];
}
}
return $nextRoundMatches;
}
function pickWinner($i, $j) {
$pct = $j/($i+$j);
$rand = rand(0,10000) / 10000;
return ($rand > $pct) ? $j:$i;
}
?><html>
<head>
<style type="text/css">
div { width:100px;}
div span { display:inline-block; width:50px;}
div.match { margin:5px 0; padding:5px; border: 1px solid grey; }
span.upset { color:red; }
div.round { float:left; padding:5px; margin-right:10px;}
</style>
</head>
<body>
<?
$matches = createDivision(16);
while(sizeof($matches) > 0) {
?>
<div class="round">
<? $matches = predictRound($matches) ?>
</div>
<?
}
?>
<div style="clear:both"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment