Wow, it has been nearly two years since I posted my code. At a whim, I decided to look again and see if I could make any improvements after two years, and I think I have. The function can be used in the same way as originally described:
/******************************************************************************
* Round Robin Pairing Generator
* Author: Eugene Wee
* Date: 23 May 2005
* Last updated: 13 May 2007
* Based on an algorithm by Tibor Simko.
*
* Copyright (c) 2005, 2007 Eugene Wee
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
function generateRoundRobinPairings($num_players) {
// Do we have a positive number of players? If not, default to 4.
$num_players = ($num_players > 0) ? (int)$num_players : 4;
// If necessary, round up number of players to nearest even number.
$num_players += $num_players % 2;
// Format for pretty alignment of pairings across rounds.
$format = "%0" . ceil(log10($num_players)) . "d";
$pairing = "$format-$format ";
// Set the return value
$ret = $num_players . " Player Round Robin:\n-----------------------";
// Generate the pairings for each round.
for ($round = 1; $round < $num_players; $round++) {
$ret .= sprintf("\nRound #$format : ", $round);
$players_done = array();
// Pair each player except the last.
for ($player = 1; $player < $num_players; $player++) {
if (!in_array($player, $players_done)) {
// Select opponent.
$opponent = $round - $player;
$opponent += ($opponent < 0) ? $num_players : 1;
// Ensure opponent is not the current player.
if ($opponent != $player) {
// Choose colours.
if (($player + $opponent) % 2 == 0 xor $player < $opponent) {
// Player plays white.
$ret .= sprintf($pairing, $player, $opponent);
} else {
// Player plays black.
$ret .= sprintf($pairing, $opponent, $player);
}
// This pair of players are done for this round.
$players_done[] = $player;
$players_done[] = $opponent;
}
}
}
// Pair the last player.
if ($round % 2 == 0) {
$opponent = ($round + $num_players) / 2;
// Last player plays white.
$ret .= sprintf($pairing, $num_players, $opponent);
} else {
$opponent = ($round + 1) / 2;
// Last player plays black.
$ret .= sprintf($pairing, $opponent, $num_players);
}
}
return $ret;
}
Aside from arithmetic simplifications and cosmetic changes to comments and spacing, the main change is the use of PHP's xor operator to simplify the colour selection code. Oh, and I decided to switch to the MIT/Expat license, which unfortunately increased the length of the code.