single elimination php code

Solutions on MaxInterview for single elimination php code by the best coders in the world

showing results for - "single elimination php code"
Barrett
22 Oct 2016
1<?php
2
3function is_player($round, $row, $team) {
4    return $row == pow(2, $round-1) + 1 + pow(2, $round)*($team - 1);
5}
6
7$num_teams = 16;
8$total_rounds = floor(log($num_teams, 2)) + 1;
9$max_rows = $num_teams*2;
10$team_array = array();
11$unpaired_array = array();
12$score_array = array();
13
14for ($round = 1; $round <= $total_rounds; $round++) {
15    $team_array[$round] = 1;
16    $unpaired_array[$round] = False;
17    $score_array[$round] = False;
18}
19
20
21echo "<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n";
22echo "\t<tr>\n";
23
24for ($round = 1; $round <= $total_rounds; $round++) {
25
26    echo "\t\t<td colspan=\"2\"><strong>Round $round</strong></td>\n";
27
28}
29
30echo "\t</tr>\n";
31
32for ($row = 1; $row <= $max_rows; $row++) {
33
34    echo "\t<tr>\n";
35
36    for ($round = 1; $round <= $total_rounds; $round++) {
37        $score_size = pow(2, $round)-1;
38        if (is_player($round, $row, $team_array[$round])) {
39            $unpaired_array[$round] = !$unpaired_array[$round];
40            echo "\t\t<td>Team</td>\n";
41            echo "\t\t<td width=\"20\"> </td>\n";
42            $team_array[$round]++;
43            $score_array[$round] = False;
44        } else {
45            if ($unpaired_array[$round] && $round != $total_rounds) {
46                if (!$score_array[$round]) {
47                    echo "\t\t<td rowspan=\"$score_size\">Score</td>\n";
48                    echo "\t\t<td rowspan=\"$score_size\" width=\"20\">$round</td>\n";
49                    $score_array[$round] = !$score_array[$round];
50                }
51            } else {
52                echo "\t\t<td colspan=\"2\"> </td>\n";
53            }
54        }
55
56    }
57
58    echo "\t</tr>\n";
59
60}
61
62echo "</table>\n";
63
64?>
65