How can I create a round robin tournament in PHP and MySQL?

I need to create game sequences using the round robin algorithm. I have a php page where the user can enter the name of the tournament that will be inserted into the database and it has a drop-down menu for up to 32 teams (select the number of teams).

So, if I select 4 teams on the page, it will be from team 1 to team 4, which will be 6 matches, because each team will play another team once. I know how the algorithm works, but I'm not quite sure how to write a query for this.

I created a table command:

Team_id    01     02     03     etc
Team_name  Team1  Team2  Team3  etc.

What should I do next?

+2
source share
2 answers

roundrobin , , , , , .

, . ..

, , , . , 2- ( ) / . - , .

<?php

/**
 * @author D.D.M. van Zelst
 * @copyright 2012
 */

function scheduler($teams){
    if (count($teams)%2 != 0){
        array_push($teams,"bye");
    }
    $away = array_splice($teams,(count($teams)/2));
    $home = $teams;
    for ($i=0; $i < count($home)+count($away)-1; $i++){
        for ($j=0; $j<count($home); $j++){
            $round[$i][$j]["Home"]=$home[$j];
            $round[$i][$j]["Away"]=$away[$j];
        }
        if(count($home)+count($away)-1 > 2){
            array_unshift($away,array_shift(array_splice($home,1,1)));
            array_push($home,array_pop($away));
        }
    }
    return $round;
}
?>

, , :

<?php $members = array(1,2,3,4); ?>

<?php $members = array("name1","name2","name3","name4"); ?>

, , :

<?php $schedule = scheduler($members); ?>

, , : , .

<?php
foreach($schedule AS $round => $games){
    echo "Round: ".($round+1)."<BR>";
    foreach($games AS $play){
        echo $play["Home"]." - ".$play["Away"]."<BR>";
    }
    echo "<BR>";
}
?>

, , .

+10

, ( ):

  • ,
  • (i = 1; ; ++)
    • print matchups Round #i:
    • . [n] [n + ]. 32 , [0] [16], [1] [17] .. [15] [31].
    • "" , . [1] [2], [2] [3],..., [31] [1], [0].

, .

: 4 :

, , - / . ( , ):

[0] [1]
[2] [3]

1:

1 2
3 4

2:

1 4
2 3

3:

1 3
4 2
+8

Source: https://habr.com/ru/post/1714285/


All Articles