2

I need help, example, i got array with 4 for team names there.

array('logiX.L4d22','Lust','Marat and Friends','Pandas of Belgium');

I want to make 3 dimensional array where first is a round, second is a match, and the third is teams who plays with eachother, there will be always 2 teams.

the logic must make that all teams must be played with all others and in one round, any team will play only one match, so if we got 5 teams then in some round one team must just wait to the next round.

it must produce something like this:

array
  0 => 
    array
      0 => 
        array
          0 => string 'logiX.L4D2' (length=10)
          1 => string 'Lust' (length=4)
      1 => 
        array
          0 => string 'Marat and Friends' (length=17)
          1 => string 'Pandas of Belgium' (length=17)
  1 => 
    array
      0 => 
        array
          0 => string 'logiX.L4D2' (length=10)
          1 => string 'Marat and Friends' (length=17)
      1 => 
        array
          0 => string 'Lust' (length=4)
          1 => string 'Pandas of Belgium' (length=17)
  2 => 
    array
      0 => 
        array
          0 => string 'logiX.L4D2' (length=10)
          1 => string 'Pandas of Belgium' (length=17)
      1 => 
        array
          0 => string 'Lust' (length=4)
          1 => string 'Marat and Friends' (length=17)

It must be work with 2,3,5 ...10 ...12 teams.

I hope that you can help me out, i already spent 1 and half days for that.

Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
Kuido
  • 21
  • 1

2 Answers2

2

Some Googling on Round Robin algorithm PHP gives the following:

http://speedtech.it/blog/2009/03/15/round-robin-algorithm-php/

http://www.phpbuilder.com/board/showthread.php?t=10300945

I hope you'll find what you're looking for.

EDIT

Adding my attempt for this, following the round-robin algorithm described on Wikipedia.

If teams number is odd, it adds a team in the array (null value), so you can retrieve the "waiting team" for each round.

<?php

$teams = range('a', 'g');

function make_rounds($teams)
{
  $nb_teams = count($teams);

  if ($nb_teams % 2 != 0)
  {
    $teams[] = null;
    $nb_teams++;
  }

  $nb_rounds = $nb_teams - 1;
  $nb_matches = $nb_teams / 2;

  $rounds = array();

  for($round_index = 0; $round_index < $nb_rounds; $round_index++)
  {
    $matches = array();

    for($match_index = 0; $match_index < $nb_matches; $match_index++)
    {
      if ($match_index == 0)
        $first_team = $teams[0];
      else
        $first_team = $teams[(($nb_teams-2) + $match_index - $round_index) % ($nb_teams-1) + 1];

      $second_team = $teams[(($nb_teams*2) - $match_index - $round_index - 3) % ($nb_teams-1) + 1];

      $matches[] = array($first_team, $second_team);
    }

    $rounds[] = $matches;
  }

  return $rounds;
}

print_r(make_rounds($teams));
Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
1

My version of solution. I would call it brute force. However, it works somehow. (Or it looks like that.)

<?php

$a = array('a','b','c','d','e','f','g');

function do_array($a)
{
    $lim = sizeof($a) - 1;

    # Create an array of all matches to play.
    $cross = array(array());
    foreach (range(0,$lim) as $k_row):
        foreach (range(0,$lim) as $k_col):
            if ($k_row >= $k_col):
                $toput = false; 
            else:
                $toput = array($a[$k_row],$a[$k_col]);
            endif;
            $cross[$k_row][$k_col] = $toput;
        endforeach;
    endforeach;

    $ret = array();
    foreach (range(0,$lim) as $k_round):
        $round = array();
        # $tmp array holds all possible matches
        # to play in current round.
        $tmp = $cross;
        $i = 0;
        foreach (range(0,$lim) as $k_row):
            foreach (range(0,$lim) as $k_col):
                if ($math = $tmp[$k_row][$k_col]):
                    $cross[$k_row][$k_col] = false;
                    # These matches are not possible
                    # in the current round.
                    foreach (range(0,$lim) as $k):
                        $tmp[$k][$k_col] = false;
                        $tmp[$k][$k_row] = false;
                        $tmp[$k_col][$k] = false;
                        $tmp[$k_row][$k] = false;
                    endforeach;
                    $round[] = $math;
                endif;
            endforeach;
        endforeach;
        if ($round):
            $ret[] = $round;
        endif;
    endforeach;

    return $ret;
}

print_r (do_array($a));

?>
Michas
  • 8,534
  • 6
  • 38
  • 62
  • Small detail: I would advise to use `for(...)` instead of `foreach(range(...))` here. See this comment: http://www.php.net/manual/fr/function.range.php#85331 – Maxime Pacary Mar 30 '11 at 19:01
  • I know it is not so fast. However I think it is more readable. – Michas Mar 30 '11 at 19:41