Location: PHPKode > scripts > roundrobin.php > roundrobin-php/roundrobin.php
<?
/* RoundRobin -roundrobin.php-
  
  Determines a schedule for n-players playing n/2 games
  per match for n-1 matches, where the same two players
  never meet more than once.
 
  Statistically speaking find (n/2)*(n-1) unique combinations
  of n items.
 
  PHP implementation of Schedule.c, Richard A. DeVenezia Copyright 1992
   
  copyrigth 2002 Email Communications, http://www.emailcommunications.nl/
  written by Bas Jobsen (hide@address.com)
  
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
*/




class RoundRobin
{
 var $MaxTeams;
 var $MaxCombinations;
 var $tourn;
 var $mList;
 var $cList;
 var $cUsed;
 
 function RoundRobin($max)
 {
  $this->MaxTeams=$max;
  $this->MaxCombinations=($this->MaxTeams/2)*($this->MaxTeams-1);
 }
 
 function ShowSchedule($players,$totalChecks)
 {
   
   
   echo $players.' players'."\n";
 
   for ($r=1; $r <= $players/2; $r++) echo 'Game'.$r; 
  
   echo "\n"; 
   echo"        +-";   
   for ($r=1; $r <= ($players/2)*6-2; $r++) echo '-';
   echo "\n";
 
   $index = 1;
   for ($r=1; $r <= $players-1; $r++) 
   {
     echo 'Week '.$r. '|';
     for ($m=1; $m <= $players/2; $m++) 
     {
       echo $this->tourn[$index]['one'].'&amp;'. $this->tourn[$index]['two'];
       $index++;
     }
     echo "\n";
   }
   echo "\n".$totalChecks,' combinations tried'. "\n\n";
 }


 function array_copy(&$dest,$source)
 {
  if(count($source)>count($dest)) {echo 'fatal'; exit;}
  //for($a=0;$a<count($source);$a++)
  $dest['one']=$source['one'];
  $dest['two']=$source['two'];
 } 
 function ClearArrays()
 {
   for ($i=0; $i <= $this->MaxCombinations; $i++)
   {
    $this->tourn[$i]['one']=0;
    $this->tourn[$i]['two']=0;
    $this->cList[$i]['one']=0;
    $this->cList[$i]['two']=0;
    $this->cUsed[$i]=0;
    if($i<=$this->MaxTeams/2)$this->mList[$i] = 0;
   }

 } 
 
 
 function Scheldule($players)
 { 
    
  while ($players <= $this->MaxTeams) 
  {


     
 $combinations = $players/2 * ($players-1);
    $totalChecks = 0;
 $this->ClearArrays();
    /* set up list of all combinations */
     $m=1;
     for ($a=1; $a<$players; $a++)            
      
      for ($b=$a+1; $b<=$players; $b++)
      {
       $this->cList[$m]['one'] = $a;
       $this->cList[$m]['two'] = $b;
       $m++;                          
      }
   
       
    $roundCount=1;
    $index=1;

    while ($roundCount <= $players-1) 
     {
       $matchCount = 1;
       $round_set = 0;
       for ($i=0; $i<=$this->MaxTeams/2; $i++) $this->mList[$i] = 0;
       
       $startC = $roundCount;
       
         while ($matchCount <= $players/2) 
         {
            $c = $combinations + 1;
            
            while ($c > $combinations) 
            {

              $c = $startC;

              /* find an unused pair that would be legitimate */
              while (
                 ($c <= $combinations) 
                 &&
                      ( //
                      ($round_set & (1 << $this->cList[$c]['one'])) ||
                       ($round_set & (1 << $this->cList[$c]['two'])) ||
                       (!empty($this->cUsed[$c]))
                      )
                    )   $c++;
                    
                if ($c > $combinations) 
                 {
            
                  do {
                    $this->mList[$matchCount] = 0;

                    $matchCount--;
                    $index--;

                    $round_set &= ~(1 << $this->cList[$this->mList[$matchCount]]['one']);
                    $round_set &= ~(1 << $this->cList[$this->mList[$matchCount]]['two']);

                    $this->cUsed[$this->mList[$matchCount]] = false;

                    $this->tourn[$index]['one'] = 0;
                    $this->tourn[$index]['two'] = 0;

                   } 
                
                   while ($this->cList[$this->mList[$matchCount]]['one'] != $this->cList[$this->mList[$matchCount]+1]['one']);

                  $startC = $this->mList[$matchCount]+1;
                }      
            }
            
            
            $this->array_copy(&$this->tourn[$index],$this->cList[$c]);
         
            $totalChecks++;

            if (($totalChecks % 1000) == 0) printf("%d\033A\n", $totalChecks );

            $this->cUsed[$c] = true;
            $this->mList[$matchCount] = $c;

            $startC = 1;

            $round_set |= (1 <<  $this->cList[$c]['one']);
            $round_set |= (1 <<  $this->cList[$c]['two']);

            $index++;
            $matchCount++;
         }
      $roundCount++;   
     }
   /* yahoo!, scheduled all the rounds */

    printf("          " );
    $this->ShowSchedule($players,$totalChecks);
    
    /* try and make a schedule using two more teams */
   $players += 2;
   }           
     } 
}
?>
Return current item: roundrobin.php