0

I have been looking for this solution and there are few docs but couldn't get them correctly. Please have a look. I tried to implement the round robin algorithm to create a match schedule but stack on home away distribution.

$allTeam=[3,10,8,7];
$leng=sizeof($allTeam);
$lastHalf=$leng-1;
for ($t=0; $t <$leng-1 ; $t++) { 
        for ($i=0; $i < $leng/2; $i++) { 
            if($t % 2== 0){
                \Log::info('Home Team => '.$allTeam[$i].' vs '.  $allTeam[$lastHalf-$i].'<=Away Team');
            }else{
                \Log::info('Away Team =>'.$allTeam[$i].' vs '.  $allTeam[$lastHalf-$i].'<= Home Team');
            }

        }

       /*now rotate the array. For this first insert the last item into postion 1*/
        array_splice( $allTeam, 1, 0, $allTeam[$leng-1]);
        /*now pop up the last element*/
        array_pop($allTeam);
 }

this is the result

Home Team => 3 vs 7<=Away Team  
Home Team => 10 vs 8<=Away Team  

Away Team =>3 vs 8<= Home Team  
Away Team =>7 vs 10<= Home Team  

Home Team => 3 vs 10<=Away Team  
Home Team => 8 vs 7<=Away Team  

You can see it's not equally distributed. How can I make sure that if one team plays as home team in first round then this team should play away in next round? Similarly if away in first round then home in next round?

Thank you.

Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95
  • You subtract one from $leng twice instead of dividing the array in half (with four teams the result is the same, but it won't work for more. Just designate half the teams as A and half as B, A plays at home on odd iterations, B on even ones. Then permutated the array when you have exhausted all the matches. – symcbean Apr 15 '18 at 01:36
  • I tested with many teams and many times, the program works really fine with the scheduling but home-away is the problem. Could you elaborate a bit about your home-away solution you gave? didn't get your solution. – Hkm Sadek Apr 15 '18 at 09:42
  • How can designate half the teams as A and half as B since after one round the array is rotated and half get mixed with one another. – Hkm Sadek Apr 15 '18 at 09:44

2 Answers2

1

It's easier then what you're doing, if you just create every permutation, then each team will play home and away an equal amount of times between teams, so if there are 4 teams you would have 3 home 3 away, generating would be done like:

<?php
$allTeam = [3, 10, 8, 7];

$result = [];
foreach ($allTeam as $home) {
    foreach ($allTeam as $away) {
        if ($home === $away) {
           continue;
        }
        $result[] = 'Home Team => '.$home.' vs '.$away.' <= Away Team';
    } 
}

print_r($result);

https://3v4l.org/EAl8G

Result:

Array
(
    [0] => Home Team => 3 vs 10 <= Away Team
    [1] => Home Team => 3 vs 8 <= Away Team
    [2] => Home Team => 3 vs 7 <= Away Team
    [3] => Home Team => 10 vs 3 <= Away Team
    [4] => Home Team => 10 vs 8 <= Away Team
    [5] => Home Team => 10 vs 7 <= Away Team
    [6] => Home Team => 8 vs 3 <= Away Team
    [7] => Home Team => 8 vs 10 <= Away Team
    [8] => Home Team => 8 vs 7 <= Away Team
    [9] => Home Team => 7 vs 3 <= Away Team
    [10] => Home Team => 7 vs 10 <= Away Team
    [11] => Home Team => 7 vs 8 <= Away Team
)

Then you would have another loop which sorts them in a way which makes it look like a home -> away -> home -> away scenario.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Each team can play n-1 games so if there are 4 team then each team will play 3 times. Your program seems nice but it doesn't work on this objective. – Hkm Sadek Apr 15 '18 at 09:40
1

I solved it with some modification. Here is my final code. It may help others

$allTeam=[1,2,3,4];
    /*now make the draw and insert it in the table*/
    $leng=sizeof($allTeam);
    $firstHalf=0;
    $lastHalf=$leng-1;

    $isHome=true;
    for ($t=0; $t <$leng-1 ; $t++) { 
        for ($i=0; $i < $leng/2; $i++) {
            if($i==0){
                if($isHome){
                    \Log::info('Home team=> '.$allTeam[$i].' vs Away team=> '.$allTeam[$lastHalf-$i]);
                    $isHome=false;
                }else{
                    \Log::info('Home team=> '.$allTeam[$lastHalf-$i].' vs Away team=> '.$allTeam[$i]);
                    $isHome=true;
                }
            }else{
                if($i%2==0){
                /*make first half home team*/
                \Log::info('Home team=> '.$allTeam[$i].' vs Away team=> '.$allTeam[$lastHalf-$i]);

                }else{
                    \Log::info('Home team=> '.$allTeam[$lastHalf-$i].' vs Away team=> '.$allTeam[$i]);  
                }
            }


        }
        /*now rotate the array. For this first insert the last item into postion 1*/
        array_splice( $allTeam, 1, 0, $allTeam[$leng-1]);
        /*now pop up the last element*/
        array_pop($allTeam);

    }
Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95