Page 1 of 1
Tournement, auto generate matches.
Posted: Sat Nov 19, 2005 4:59 pm
by boonika
I don't know how to script this. I'm making an online soccer manager game. I have a team table and I would like to let the teams sign in to cups. There would be a limit of teams that sign in a cup. When the limit is reached, a script will generate the matches.
But how to script that. If you have a table called teams (with the participating teams), how to add matches to a matches table (where you can find the matches that need to be played)?
Ex.
Teams Table
---------------
Team 1
Team 2
Team 3
Team 4
...
Matches Table
------------------
Team 1 VS Team 2
Team 3 VS Team 4
Can somebody please help? Thanks!
Posted: Sat Nov 19, 2005 8:32 pm
by RobertPaul
Well ... every time you insert a team into the database, you'd have to check whether or not you've reached your limit.
If you
have reached the limit, query all the data you think you'll need.
What
I would do is just grab the team names and dump them in an array.
Shuffle the array then just loop through and match them up. For example, after the shuffle you might wind up with an array that looks like:
Code: Select all
array (
0 => 'Team3',
1 => 'Team1',
2 => 'Team2',
3 => 'Team4',
)
So loop through two at a time:
Code: Select all
shuffle($teams);
$count = count($teams);
for($i=0;$i<$count;$i+=2) {
//do whatever you need to do to match $team[$i] against $team[$i+1]
}
I hope that helps some. If not ... well, sorry.
Posted: Sat Nov 19, 2005 8:43 pm
by John Cartwright
Code: Select all
$teams = array(
'team1',
'team2',
'team3',
'team4',
);
$numTeams = sizeof($teams);
$opponents = $teams;
shuffle($opponents);
for ($x = 0; $x < $numTeams; $x++) {
echo $teams[$x].' will be playing'. $opponent[$x] .' <br />';
}
This is untested, because I am uncertain if this guarantees all keys are moved.
Posted: Sun Nov 20, 2005 3:32 am
by boonika
Thanks, I'm going to have a look and I'll report if it works

.
Posted: Sun Nov 20, 2005 9:42 am
by boonika
Jcart wrote:Code: Select all
$teams = array(
'team1',
'team2',
'team3',
'team4',
);
$numTeams = sizeof($teams);
$opponents = $teams;
shuffle($opponents);
for ($x = 0; $x < $numTeams; $x++) {
echo $teams[$x].' will be playing'. $opponents[$x] .' <br />';
}
This is untested, because I am uncertain if this guarantees all keys are moved.
I tested your script (it must be $opponents[$x]) and I now get this:
team1 will be playing team4
team2 will be playing team2
team3 will be playing team3
team4 will be playing team1
How to make teamx doesn't play more than one match at same time and how to make that the same team doesn't play against himself?
Posted: Sun Nov 20, 2005 11:09 am
by John Cartwright
Code: Select all
class generateOpponents {
function generateOpponents($teams);
$this->sizeOf($teams);
$this->opponents = array();
$this->teams = $teams;
}
function cycleTeams() {
foreach ($this->teams as $team) {
$this->generateOpponentID($team);
}
return $this->opponents;
}
function generateOpponentID($team) {
$id = 'team'.rand(1,$this->sizeOf);
if (in_array($id, $this->opponents) && $team != $id) {
$this->generateOpponentID($team);
}
else {
$this->setOpponents($id);
}
}
function setOpponents($id) {
$this->opponents[] = $id;
}
}
$teams = array(
'team1',
'team2',
'team3',
'team4',
);
$opponents = new generateOpponents($teams);
$opponentArray = $opponents->cycleTeams();
I doubt this will work at first because I cannot test it because I am not on my dev computer... therefor I'll leave all the dirty work for you

Posted: Sun Nov 20, 2005 1:17 pm
by boonika
Code: Select all
class generateOpponents {
function generateOpponents($teams) {
$this->sizeOf($teams);
$this->opponents = array();
$this->teams = $teams;
}
function cycleTeams() {
foreach ($this->teams as $team) {
$this->generateOpponentID($team);
}
return $this->opponents;
}
function generateOpponentID($team) {
$id = 'team'.rand(1,$this->sizeOf);
if (in_array($id, $this->opponents) && $team != $id) {
$this->generateOpponentID($team);
}
else {
$this->setOpponents($id);
}
}
function setOpponents($id) {
$this->opponents[] = $id;
}
}
$teams = array(
'team1',
'team2',
'team3',
'team4',
);
$opponents = new generateOpponents($teams);
$opponentArray = $opponents->cycleTeams();
Fatal error: Call to undefined function: sizeof() in /home/httpd/vhosts/octavian.be/httpdocs/test.php on line 4
Please check it if you can, coz I can't find the error

.
Posted: Mon Nov 21, 2005 12:22 pm
by boonika
Please?
