chriscloyd Posted April 9, 2012 Share Posted April 9, 2012 I am working on my admin panel for a client and i am having a problem with creating an automatic scheduler. The problem I am having is that if the teams already played its not redoing the for loop how can i make it do the for loop again? <?php //Automatic Scheduler for leagues and such //Load custom mysql class for this project include("class.mysql.php"); //start MySql instance $my = new MySql; //get the number of teams $num = $my->getNum("SELECT id FROM teams"); //if number of teams are odd add a dummy team for a BYE week if ($num%2 != 0) { //create dummy team called BYE $my->insert('Teams',array("Name" => "BYE")); } //do the necessary calculations $number_of_teams = $num; $number_of_weeks = $number_of_teams * 2 - 2; $number_of_matches_per_week = $number_of_teams/2; $total_number_of_matches = $number_of_weeks * $number_of_matches_per_week; //start the week for statment for ($i = 1; $i <= $number_of_weeks; $i++) { //get all the teams in an array $teams = $my->getArray("SELECT id,name FROM teams"); shuffle($teams); for ($b = 1; $b <= $number_of_matches_per_week; $b++) { //get random teams $aid = array_rand($teams); $hid = array_rand($teams); //check to see if the teams have played each other if they have it will return FALSE if ($my->checkTeams($aid,$hid)) { //create match $my->createMatch($aid,$hid,$i); } else { //do it again $b--; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260583-scheduler/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 9, 2012 Share Posted April 9, 2012 It is not efficient to randomly try to schedule teams, because as more and more pairs of the teams have already played, you have less and less valid combinations and by randomly picking combinations, you will end up leaving a pool of teams that have already played that cannot be paired together. You will end up back-tracking and throwing away the sets of picks and when you get near the end, you will find that you must throw away a huge number of tries before you find a set that is unique. The best way of getting a computer to schedule all possible rotations of teams, is to create an array of the teams and shift the data to form all rotations. You would then pick a different set from the result for each schedule. See the following code and the output it produces - <?php $teams = array( "Team 1", "Team 2", "Team 3", "Team 4", "Team 5", "Team 6", "Team 7", "Team 8", "Team 9", "Team 10", "Team 11", "Team 12", "Team 13", "Team 14", "Team 15", "Team 16", "Team 17", "Team 18", "Team 19", "Team 20" ); $numteams = sizeof($teams); $team_list = array_keys($teams); $tmp_list = array_splice($team_list, 1, sizeof($team_list) - 1); $count = sizeof($tmp_list) + 1; $days = array(); for ($i = 0;$i < $numteams - 1;$i++) { $days[$i] = array(); $day_list = array_merge(array($team_list[0]), $tmp_list); for ($j = 0;$j < $count / 2;$j++) { $days[$i][] = $teams[$day_list[$j]] . ' vs ' . $teams[$day_list[$count - $j - 1]]; } // rotate $tmp_list $elm = array_shift($tmp_list); array_push($tmp_list, $elm); } echo "<pre>"; print_r($days); echo "</pre>"; // $days is an array of arrays - 0 - 18 (weeks) and each has 0-9 (games) Quote Link to comment https://forums.phpfreaks.com/topic/260583-scheduler/#findComment-1335528 Share on other sites More sharing options...
chriscloyd Posted April 9, 2012 Author Share Posted April 9, 2012 Okay, thanks for the help! I really appreciated it. How do I make it to where they only play for ten weeks? Quote Link to comment https://forums.phpfreaks.com/topic/260583-scheduler/#findComment-1335530 Share on other sites More sharing options...
PFMaBiSmAd Posted April 9, 2012 Share Posted April 9, 2012 If you want unique combinations and you want each team to play all the other teams one time, you cannot have 10 weeks. There's an (N-1) term involved in the combinations, so if you have 10 teams (or any combination of teams and bye's that add up to 10), taken two at a time, you have 9 weeks. For 12 teams (or any combination of teams and bye's that add up to 12), taken two at a time, you would have 11 weeks. You should have an even total of teams+bye's so that you don't have a team playing itself (which is essentially sitting out, which is the same as having enough bye's to make an even number.) The only way to have 10 weeks would be to produce more than ten combinations and throw enough away to give you 10 combinations, in which case every team won't play all the other teams one time. Quote Link to comment https://forums.phpfreaks.com/topic/260583-scheduler/#findComment-1335538 Share on other sites More sharing options...
PFMaBiSmAd Posted April 9, 2012 Share Posted April 9, 2012 I guess, here's a way you can have 10 weeks. You need 11 teams. The combinations from the code I posted will have a different team playing itself each week. That single team would sit out that week, leaving the remaining 10 teams to play. Quote Link to comment https://forums.phpfreaks.com/topic/260583-scheduler/#findComment-1335541 Share on other sites More sharing options...
PFMaBiSmAd Posted April 9, 2012 Share Posted April 9, 2012 Edit to the above: You cannot use an odd number of teams. You will end up with teams playing each other more than once and one of the teams won't have an off week. Quote Link to comment https://forums.phpfreaks.com/topic/260583-scheduler/#findComment-1335545 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.