[ACCEPTED]-How can I generate a round robin tournament in PHP and MySQL?-mysql
I created a roundrobin function from scratch 22 as i thought it might be easier to get the 21 same results and also allowing me to use 20 arrays filled with strings directly instead 19 of numbers.
Because i pull a list of names 18 from a database and add into an array i 17 can now schedule this directly with below 16 function. No extra step needed to link numbers 15 to names etc.
Please feel free to try it 14 and if it works then leave a comment. I 13 also have a version which allows for 2 way 12 (home & return) schedule and or shuffle 11 option. If somone is interested in that 10 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 9 example create an array like:
<?php $members = array(1,2,3,4); ?>
or
<?php $members = array("name1","name2","name3","name4"); ?>
then call 8 the function to create your schedule based 7 on above array:
<?php $schedule = scheduler($members); ?>
To display the resulted array 6 schedule simply do like below or anyway 5 you like: This little code displays the 4 schedule in a nice format but use it anyway 3 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 2 or if you are interested in the 2-way version 1 with shuffle.
There is a fairly simple algorithm for doing 9 round-robin matchups, my solution would 8 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++)
- print matchups for Round #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]. If you have 32 teams, [0] is matched with [16], [1] with [17], etc up to [15] and [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].
And that's 7 it, that will produce all the matchups you 6 need.
An example, with 4 teams:
First half 5 of the array is on top, second half is on 4 the bottom, match-ups are numbers above/below 3 each other. Array indexes (to illustrate 2 what I mean exactly):
[0] [1]
[2] [3]
Round 1:
1 2
3 4
Round 2:
1 4
2 3
Round 1 3:
1 3
4 2
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.