简体   繁体   English

如何在 PHP 和 MySQL 中生成循环赛?

[英]How can I generate a round robin tournament in PHP and MySQL?

I need to generate sequences of games using the round robin algorithm.我需要使用循环算法生成游戏序列。 I have the php page where the user can input the tournament name which will be inserted into the database and it has got a drop down menu up to 32 teams (select number of teams).我有 php 页面,用户可以在其中输入将插入数据库的锦标赛名称,它有一个最多 32 支球队的下拉菜单(选择球队数量)。

So if I select 4 teams in the page, so it will be from team 1 to team 4 which would be 6 matches because every team plays the other team once.因此,如果我 select 页面中有 4 支球队,那么从球队 1 到球队 4 将是 6 场比赛,因为每支球队都与另一支球队比赛一次。 I know how the algorithm works but I am not quite sure how to write the query for that.我知道算法是如何工作的,但我不太确定如何为此编写查询。

I created the table team:我创建了表团队:

Team_id    01     02     03     etc
Team_name  Team1  Team2  Team3  etc.

What should I do from here?我应该从这里做什么?

I created a roundrobin function from scratch as i thought it might be easier to get the same results and also allowing me to use arrays filled with strings directly instead of numbers.我从头开始创建了一个循环 function,因为我认为它可能更容易获得相同的结果,并且还允许我使用 arrays 直接填充字符串而不是数字。

Because i pull a list of names from a database and add into an array i can now schedule this directly with below function.因为我从数据库中提取了一个名称列表并添加到一个数组中,所以我现在可以使用下面的 function 直接安排这个。 No extra step needed to link numbers to names etc.无需额外步骤即可将数字链接到名称等。

Please feel free to try it and if it works then leave a comment.请随时尝试,如果有效,请发表评论。 I also have a version which allows for 2 way (home & return) schedule and or shuffle option.我还有一个版本,它允许 2 路(家庭和返回)日程安排和/或随机播放选项。 If somone is interested in that one then leave a coment as well.如果有人对此感兴趣,那么也发表评论。

<?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;
}
?>

How to use, for example create an array like:如何使用,例如创建一个数组,如:

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

or或者

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

then call the function to create your schedule based on above array:然后调用 function 根据上述数组创建您的日程安排:

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

To display the resulted array schedule simply do like below or anyway you like: This little code displays the schedule in a nice format but use it anyway you like.要显示生成的数组时间表,只需执行以下操作或您喜欢的任何方式:这个小代码以一种很好的格式显示时间表,但您可以随意使用它。

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

Leave a note if it worked for you or if you are interested in the 2-way version with shuffle.如果它对您有用,或者您对带有 shuffle 的 2-way 版本感兴趣,请留言。

There is a fairly simple algorithm for doing round-robin matchups, my solution would be as follows (in pseudo-code):循环比赛有一个相当简单的算法,我的解决方案如下(伪代码):

  • fetch all the teams out of the database into an array, in any order以任意顺序将所有团队从数据库中取出到一个数组中
  • for (i = 1; i < number of teams; i++) for (i = 1; i < 团队数; i++)
    • print matchups for Round #i:打印第 #i 轮比赛:
    • the teams in the first half of the array are matched up in the same order with the teams in the last half of the array.前半场的球队与后半场的球队以相同的顺序进行匹配。 That is, the team at any index [n] is matched up with the team at index [n + half the number of teams].也就是说,任何索引 [n] 的团队与索引 [n + 一半团队数量] 的团队匹配。 If you have 32 teams, [0] is matched with [16], [1] with [17], etc up to [15] and [31].如果您有 32 个团队,则 [0] 与 [16] 匹配,[1] 与 [17] 匹配,等等直到 [15] 和 [31]。
    • Now, "rotate" the teams through the array, but leave the first one in the array in place .现在,通过阵列“轮换”团队,但保留阵列中的第一个 That is, [1] becomes [2], [2] becomes [3], ..., up to [31] becomes [1], but do not move the team at index [0].也就是说,[1] 变为 [2],[2] 变为 [3],...,直到 [31] 变为 [1],但不要移动索引 [0] 处的团队。

And that's it, that will produce all the matchups you need.就是这样,它将产生您需要的所有对决。

An example, with 4 teams:一个例子,有 4 个团队:

First half of the array is on top, second half is on the bottom, match-ups are numbers above/below each other.阵列的前半部分在顶部,后半部分在底部,比赛是彼此上方/下方的数字。 Array indexes (to illustrate what I mean exactly):数组索引(为了说明我的意思):

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

Round 1:第1轮:

1 2
3 4

Round 2:第 2 轮:

1 4
2 3

Round 3:第三轮:

1 3
4 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM