Team distribution in schedule

Hello
i’m going to have teams and distributes it in a schedule which every team play with the other but not to play twice in the day or with the same team twice .

i tried this code and it works ,but i want to show the name of each team not the numbers. this code accepts the number of teams to play and generate the schedule .


<body>
<form id="4" name="4" action="test.php" method="post">
<input type="text" id="teams" name="teams" />
<input type="submit" name="submit" value="submit" />
</form>
<?php
$teams=array ("team1","team2","team3","team4");
$n =$_POST['teams'];
if (!empty($_GET['n']) && ctype_digit($_GET['n'])) {
$n = $_GET['n'];
}
echo '<pre>' . generateRoundRobinPairings($n) . '</pre>';

function generateRoundRobinPairings($num_players) {
//do we have a positive number of players? otherwise default to 4
$num_players = ($num_players > 0) ? (int)$num_players : 0;
//set number of players to even number
$num_players = ($num_players &#37; 2 == 0) ? $num_players : $num_players + 1;
//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:\
-----------------------";
//print the rounds
for ($round = 1; $round < $num_players; $round++) {
$ret .= sprintf("\
Day #$format : ", $round);
$players_done = array();
//print the pairings
for ($player = 1; $player < $num_players; $player++) {
if (!in_array($player, $players_done)) {
//select opponent
//$opponents= array("team")
$opponent = $round - $player;

//echo $opponent.'<br>';
$opponent += ($opponent < 0) ? $num_players : 1;
//ensure opponent is not the current player
if ($opponent != $player) {
//choose colours
if ($player % 2 == $opponent % 2) {
if ($player < $opponent) {
//player plays black
$ret .= sprintf($pairing, $opponent, $player);
} else {
//player plays white
// $ret .= sprintf($pairing, $player, $opponent);
}
} else {
if ($player < $opponent) {
//player plays white
$ret .= sprintf($pairing, $player, $opponent);
} else {
//player plays black
$ret .= sprintf($pairing, $opponent, $player);
}
}
//these players are done for this round
$players_done[] = $player;
$players_done[] = $opponent;
}
}
}
//print the last pairing (i.e. for 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;
}
?>

Okay, lets knock out a couple of things off the bat.

$num_players = ($num_players > 0) ? (int)$num_players : 0;

You were defaulting to 0 instead of 4. You also might want to default-load your argument too…


function generateRoundRobinPairings($num_players = 4) {


$num_players = ($num_players &#37; 2 == 0) ? $num_players : $num_players + 1;

$num_players = ceil($num_players / 2) * 2 works here too. Either way is okay, though i’m assuming your % is a copying error for %.

Now… logic.
I assume by your function name you want a Round Robin system; namely that each team has to play each other team once.
There is a mathematical principle here that will be of use; the number of games required is simply n! (where n is the number of teams involved). As you want only floor(N/2) Games per day (Where every team is playing every day), N!/(N/2) = 2*(N-1)! = number of days you will need to complete the round robin.

Here’s how I would do it: (Note: this function takes in an array of team values (names, ID’s, doesnt matter what) and returns a multidimensional array at the end)


function generateRoundRobinPairings($teams) {
$pairs = array();
$games = array();
for($i = 0; $i < count($teams); $i++) {
 for($j = $i + 1; $j < count($teams); $j++) {
   $pairs[][0] = $teams[$i];
   $pairs[][1] = $teams[$j];
 }
}
shuffle($pairs); //Scramble the game order.
while(count($pairs) > 0) {
  $day++;
  $used = array_fill(0,count($teams),0);
  while(count($games[$day]) < floor(count($teams)/2)) {
    $potential = array_shift($pairs);
    if($used[$potential[0]] == 0 && $used[$potential[1]] == 0) {
      $games[$day][] = $potential;
      $used[$potential[0]] = 1;
      $used[$potential[1]] = 1;
    } else {
      array_push($pairs,$potential);
    }
  }
}
return $games;
}
//returns a multdimensional array; index1 being day, index2 being game within the day, and index 3 being 0 or 1 (home and away, or whatever)

to continue the previous post i get the names of the teams from database