...

View Full Version : Distribute teams in a Schedule with php



ragy
06-22-2010, 08:23 AM
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 .
In the link below you enter the number of teams and press generate then the it distribute the number of teams you had entered in a schedule and i want to do like this by using php

http://www.teamopolis.com/tools/roun...generator.aspx

thanks alot

Fou-Lu
06-22-2010, 03:41 PM
Never bothered to follow that link, but given that it links exactly to that text, I'd assume that it wouldn't work anyway.
In any case though, what's your actual question?

ragy
06-24-2010, 10:00 AM
hello
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 % 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:\n-----------------------";
//print the rounds
for ($round = 1; $round < $num_players; $round++) {
$ret .= sprintf("\nDay #$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;
}
?>

Fou-Lu
06-24-2010, 04:06 PM
Is your $teams array the names you are referring to?
You'll need to clarify on input and output results.
I type 1, what is my expected response? What about 4? What about 5 (out of bounds potentially)? What about 0?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum