-1

I am trying to form a loop in returning the next match number in a tournament tree. I managed to do the upper bracket but the lower bracket I am having trouble.

You can take a look at this pic to know exackly what I am looking for: http://s21.postimg.org/3ryr344br/bracket.png (which is an example if max = 32)

So for example in the image where it shows 32, the next match number should return 40. Match number 44 or 45 should return 50.

Any help is appreciated. Many thanks

If max = 8

8 = 10 = 1st round starts

9 = 11

10 = 12 = 2nd round starts

11 = 12

12 = 13 = to final round

If max = 16

16 = 20 = 1st round starts

17 = 21

18 = 22

19 = 23

20 = 24 = 2nd round starts

21 = 24

22 = 25

23 = 25

24 = 26 = 3rd round starts

25 = 27

26 = 28 = 4th round starts

27 = 28

28 = 29 = to final round

If max = 32

32 = 40 = 1st round starts

33 = 41

34 = 42

35 = 43

36 = 44

37 = 45

38 = 46

39 = 47

40 = 48 = 2nd round starts

41 = 48

42 = 49

43 = 49

44 = 50

45 = 50

46 = 51

47 = 51

48 = 52 = 3rd round starts

49 = 53

50 = 54

51 = 55

52 = 56 = 4th round starts

53 = 56

54 = 57

55 = 57

56 = 58 = 5th round starts

57 = 59

58 = 60 = 6th round starts

59 = 60

60 = 61 = to final round

max can be up to 512

Karim Ali
  • 61
  • 2
  • 10
  • Read the question 2 times and image, but didn't understand a thing. Is it just me? – Amal Murali Sep 29 '13 at 16:24
  • possible duplicate to http://stackoverflow.com/questions/18957114/using-array-fill, http://stackoverflow.com/questions/18449679/using-forloop-to-populate-brackets, http://stackoverflow.com/questions/18343955/working-around-for-loop – djot Sep 29 '13 at 16:40

1 Answers1

0

I've made something similar some time ago, but was using sql table to hold the data. Maybe this code will help you a little bit:

    $participants = 32;
    $games = participantsToGames($participants);
    $rounds = log($participants, 2);

    echo "#Participants: $participants; games: $games; rounds: $rounds <br />";

    echo "TRUNCATE TABLE `participants`;<br />";
    for($i = 1; $i <= $participants; $i++){
        echo "INSERT INTO `participants` (`title`) VALUES ('Driver $i');<br />";
    }

    echo "TRUNCATE TABLE `games`;<br />";
    $participantsLeft = $participants;
    $gameNumber = 1;
    $gamesAfterLastRound = 1;

    for($round = 1; $round <= $rounds; $round++){
        $nrOfGames = $participantsLeft / 2;

        for($gameNrThisRound = 1; $gameNrThisRound <= $nrOfGames; $gameNrThisRound++, $gameNumber++){
            $nextGame = ($participantsLeft == 2) 
                ? 0 
                : $gamesAfterLastRound -1 + ceil($nrOfGames + $gameNrThisRound / 2);

            $nextGamePosition = ($gamesAfterLastRound -1 + ($nrOfGames + $gameNrThisRound / 2) == $nextGame) 
            ? 'b'
            : 'a'; 

            if ($round == 1){
                $a = ($participantsLeft * $gameNrThisRound / $nrOfGames) - 1;
                $b = $a + 1;
                echo $sql = "INSERT INTO `games` (`participant_a`, `participant_b`, `round`, `game_number`, `next_game`, `next_game_position`) "
                        ."VALUES ('$a', '$b', '$round', '$gameNumber', '$nextGame', '$nextGamePosition');<br />";
            }elseif ($round == $rounds){
                echo $sql = "INSERT INTO `games` (`round`, `game_number`, `next_game`, `next_game_position`) "
                        ."VALUES ('$round', '$gameNumber', '0', '0');<br />";
                $gameNumber++;
                echo $sql = "INSERT INTO `games` (`round`, `game_number`, `next_game`, `next_game_position`) "
                        ."VALUES ('$round', '$gameNumber', '0', '0');<br />";
            }else{
                echo $sql = "INSERT INTO `games` (`round`, `game_number`, `next_game`, `next_game_position`) "
                        ."VALUES ('$round', '$gameNumber', '$nextGame', '$nextGamePosition');<br />";
            }


        }
        echo '#<br />';
        $gamesAfterLastRound = $gameNumber;
        $participantsLeft /= 2;

    }
    echo "UPDATE `games` SET `next_game` = 32 WHERE `next_game`=31";


    function participantsToGames($participants){
        if ($participants == 2) return 1;
        return participantsToGames($participants / 2) + ($participants / 2);
    }






    /*
     * 
     CREATE TABLE IF NOT EXISTS `games` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `participant_a` int(10) unsigned NOT NULL,
      `participant_b` int(10) unsigned NOT NULL,
      `winner` int(10) unsigned NOT NULL,
      `round` smallint(5) unsigned NOT NULL,
      `game_number` int(10) unsigned NOT NULL,
      `next_game` int(10) unsigned NOT NULL,
      `next_game_position` enum('a','b') NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;


    CREATE TABLE IF NOT EXISTS `participants` (
      `id` mediumint(9) NOT NULL AUTO_INCREMENT,
      `title` varchar(256) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;
*/

Jus add those two tables to a database and then execute SQL queries returned by this script. You will have one table for participant and other for games slots.

Jaroslav
  • 1,389
  • 14
  • 27