如何在PHP和MySQL中生成循环锦标赛? [英] How can I generate a round robin tournament in PHP and MySQL?
问题描述
我需要使用循环算法来生成游戏序列.我有一个php页面,用户可以在其中输入比赛名称,该名称将插入数据库中,并且具有一个下拉菜单,最多可容纳32个球队(选择球队数).
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).
因此,如果我在页面中选择4个球队,那么这将是从第1队到第4队的比赛,这将是6场比赛,因为每个队与另一队比赛一次.我知道算法是如何工作的,但是我不太确定如何为此编写查询.
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. I know how the algorithm works but I am not quite sure how to write the query for that.
我创建了餐桌团队:
Team_id 01 02 03 etc
Team_name Team1 Team2 Team3 etc.
我应该从这里做什么?
推荐答案
我从头开始创建了一个roundrobin函数,因为我认为可能更容易获得相同的结果,并且还允许我直接使用用字符串填充而不是数字填充的数组
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.
因为我从数据库中提取了一个名称列表并将其添加到数组中,所以现在可以使用以下函数直接安排它.将数字链接到名称等不需要任何额外的步骤.
Because i pull a list of names from a database and add into an array i can now schedule this directly with below function. No extra step needed to link numbers to names etc.
请随时尝试,如果可以,请发表评论. 我也有一个允许2种方式(返程和返程)和/或随机播放选项的版本.如果某人对此感兴趣,那么也要留下评论.
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. 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); ?>
或
<?php $members = array("name1","name2","name3","name4"); ?>
然后调用该函数以根据上述数组创建时间表:
then call the function to create your schedule based on above array:
<?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>";
}
?>
如果它对您有用,或者您对带混洗的2向版本感兴趣,请留下笔记.
Leave a note if it worked for you or if you are interested in the 2-way version with shuffle.
这篇关于如何在PHP和MySQL中生成循环锦标赛?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!