# RE: [PHP] Pairing algorithm?

```I found a pairing algorithm finally via Google in C that I've been trying to
translate over into PHP, with no success (seemingly creates an infinite
loop).  I'm also surprised I haven't seen it in PHP yet.. thought someone
would have done it by now.```
```

And the source for that:

I'm still mucking around with it, but I don't know C very well, so I'm sure
there's a few bugs in my translation of it that's causing the infinite loop
in PHP, I'm just still trying to track it down..

Here's the code I very quickly tried to translate over, surely to be
littered with errors:

<?

\$teams = 10;
\$MaxTeams = 50;

\$combinations = (\$teams/2 * (\$teams-1));

\$m = 1;
for (\$a = 1; \$a < \$teams; \$a++) {
for (\$b = (\$a+1); \$b <= \$teams; \$b++) {
\$clist[\$m][one] = \$a;
\$clist[\$m][two] = \$b;
\$m++;
}
}

\$roundcount = 1;
\$index = 1;

while (\$roundcount <= \$teams-1) {
\$matchcount = 1;
\$round_set = 0;
for (\$i = 0; \$i <= \$MaxTeams/2; \$i++) {
\$mlist[\$i] = 0;
}

\$startC = \$roundcount;
while (\$matchcount <= \$teams/2) {
\$c = (\$combinations + 1);
while (\$c > \$combinations) {
\$c = \$startC;
while ((\$c <= \$combinations) && (\$round_set & (1 <<
\$clist[\$c][one])) ||
(\$round_set & (1 << \$clist[\$c][two])) || (\$cused[\$c]))
{
\$c++;
}

if (\$c > \$combinations) {
do {
\$mlist[\$matchcount] = 0;
\$matchcount--;
\$index--;

\$round_set &= ~(1 <<
\$cList[\$mList[\$matchCount]][one]);
\$round_set &= ~(1 <<
\$cList[\$mList[\$matchCount]][two]);

\$cUsed[\$mList[\$matchCount]] = FALSE;

\$tourn[\$index][one] = 0;
\$tourn[\$index][two] = 0;

} while (\$clist[\$mlist[\$matchcount]][one] !=
\$clist[\$mlist[\$matchcount]+1][one]);

\$startc = \$mlist[\$matchcount] + 1;
}

}
\$tourn[\$index] = \$clist[\$c];
\$totalchecks++;
if ((\$totalchecks % 1000) == 0) {
}

\$cused[\$c] = TRUE;
\$mlist[\$matchcount] = \$c;

\$startc = 1;

\$round_set |= (1 << \$clist[\$c][one]);
\$round_set |= (1 << \$clist[\$c][two]);

\$index++;
\$matchcount++;
}
\$roundcount++;
}

?>

-----Original Message-----
From: Bas Jobsen [mailto:[EMAIL PROTECTED]]
Sent: Monday, August 12, 2002 12:19 PM
Subject: Re: [PHP] Pairing algorithm?

Hi

Never Seen it in php. There are many variants, mostly depending on your
needs.
Do you have the algorithm you want in pseudo code?

Op maandag 12 augustus 2002 16:24, schreef Chad Day:
> Has anyone written any sort of pairing algorithm for a round robin sports
> schedule, or something similar?  I couldn't find anything in the archives
> surprisingly, and I'm looking around google and not turning up much except
> some PDFs and white papers addressing the mathmatical complications of it.
>
> Thanks,