Tournement, auto generate matches.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Tournement, auto generate matches.

Post 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!
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post 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.
Last edited by RobertPaul on Sat Nov 19, 2005 8:49 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post by boonika »

Thanks, I'm going to have a look and I'll report if it works :).
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 ;)
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post 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 :(.
boonika
Forum Newbie
Posts: 18
Joined: Tue May 24, 2005 1:46 pm

Post by boonika »

Please? :(
Post Reply