Tournement, auto generate matches.
Moderator: General Moderators
Tournement, auto generate matches.
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!
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!
-
RobertPaul
- Forum Contributor
- Posts: 122
- Joined: Sun Sep 18, 2005 8:54 pm
- Location: OCNY
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:
So loop through two at a time:
I hope that helps some. If not ... well, sorry.
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',
)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]
}
Last edited by RobertPaul on Sat Nov 19, 2005 8:49 pm, edited 1 time in total.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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 />';
}I tested your script (it must be $opponents[$x]) and I now get this:Jcart wrote:This is untested, because I am uncertain if this guarantees all keys are moved.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 />'; }
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?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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();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();Please check it if you can, coz I can't find the error