Ok, after some more fiddling, I'm really close to getting this to work.

The script seems to correctly generate a schedule if an odd number of teams
are specified:

Given an array of 5 teams, A through E, the following pairs are generated:

Round 1 - A/E, B/D
Round 2 - D/E, A/C
Round 3 - C/D, B/E
Round 4 - B/C, A/D
Round 5 - A/B, C/E

Looks good.. each team gets a bye, each team plays each other.  With an even
number of teams however, some pairings are repeated, and I'm sure this is a
bug I created when translating the code from perl to PHP.  The PHP code I
have is below, the link to the perl source is
(http://www.perlmonks.org/index.pl?parent=90132&title=Round%20Robin%20Schedu
ling&lastnode_id=90132&displaytype=display&type=superdoc&node=Comment%20on),
and the set of pairings I get with 6 teams, A through F is below.

6 teams, A/F

Round 1 - A/E, B/D, C/F
Round 2 - F/D, A/C, B/E
Round 3 - E/C, F/B, A/D
Round 4 - D/B, E/A, F/C (dupe of round 1)
Round 5 - C/A, D/F, E/B (dupe of round 2)

PHP code:

<?

$teamarray = array('A', 'B', 'C', 'D', 'E', 'F');

$num_teams = sizeof($teamarray);

$pivot = undef;
$even = 0;
$end_point_a = 0;
$end_point_b = $num_teams-1;

if($num_teams % 2 == 0) {
        # $pivot = array_pop($teamarray);
        #$pivot = $#teams;
        #$pivot = $num_teams-;
        $pivot = ($num_teams - 1);
        $even = 1;
        $end_point_b--;
}


# Create a list of the "stripes" of the polygon, the stripes will be
# pairs of indices into the array

$stripes = array();

# Assume that each element in the array is a vertex of the polygon
# and the vertices are listed in order, stripes are created

for ($x = 1; $x < (sizeof($teamarray) / 2); $x++) {
# for (1..int(($size-1)/2))     {
        array_push($stripes, array("$end_point_a", "$end_point_b"));
    $end_point_a++;
    $end_point_b--;
}

if ($even) {
        array_push($stripes, array("$end_point_a",  "$pivot"));
}

$games = array();
for ($x = 0; $x < ($num_teams - $even); $x++) {
        $this_week = array();
        for ($y = 0; $y < sizeof($stripes); $y++) {
                $num1 = $stripes[$y][0];
                $num2 = $stripes[$y][1];
                array_push($this_week, $teamarray[$num1], $teamarray[$num2]);
        }
        array_push($games, $this_week);

    # Now rotate the @teams array
    # Save the last team as the pivot for the polygon
    $last_team = array_pop($teamarray);
    array_unshift($teamarray, $last_team);
//      echo "<p>Array Order: ";
//      for ($z = 0; $z < sizeof($teamarray); $z++) {
//              echo $teamarray[$z];
//      }
/*  if ($even) {
        array_push($teamarray, $last_team);
    }
*/
}

// echo '<br><br><br>';
print_r($games);

?>

-----Original Message-----
From: Chad Day [mailto:[EMAIL PROTECTED]]
Sent: Monday, December 30, 2002 10:57 AM
To: php general
Subject: [PHP] Round robin script revisited..


I was trying to find a script to do round robin pairings for a sports
schedule generator..  I was able to come across one in Perl, and am in the
process of trying to convert it to PHP, but since my perl knowledge is a bit
(well, a lot) lacking, I was hoping someone could help..

Perl script:

http://www.perlmonks.org/index.pl?parent=90132&title=Round%20Robin%20Schedul
ing&lastnode_id=90132&displaytype=display&type=superdoc&node=Comment%20on


The part I'm having trouble with:

    for(1..($size-$even))
    {
    my @this_week;
    # push the weeks worth of games onto @games
    foreach my $sched_ref (@stripes)
    {
        push (@this_week, [$teams[$sched_ref->[0]], $teams[$sched_ref-
+>[1]]]);
    }
    push(@games, \@this_week);


I'm not sure how to rewrite that in PHP and be pushing the correct array
elements into the games array .. If anyone can help me out or point me in
the right direction, I'd appreciate it.

Thanks,
Chad


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to