Jump to content

Printing random array


mtgriffiths

Recommended Posts

Hey All,

 

i have a list of teams 1 to 6... i need to create some method of arranging them before input to a table so that each team plays each other once only.

 

How would i go about doing this? The need to be inputted into a table in the following format:

 

1    v    2

3    v    4

5    v    6

 

After 5 weeks all teams would have played each other once.

 

Hope some one can help

 

Thanks in advance

 

Matthew

Link to comment
https://forums.phpfreaks.com/topic/93751-printing-random-array/
Share on other sites

Barand, that results in team 1 playing multiple teams in the first week.  I can imagine a solution where you assign team 1 vs someone they haven't played, then continue assigning the other teams vs someone they haven't played.  But I am too busy to implement it.  It just needs you to keep track of who has played who, and then make "random" (or rather systematic) assignments of who plays who.

 

I think that will work, right?  Or is there some situation you can get in where it will fail?

Link to comment
https://forums.phpfreaks.com/topic/93751-printing-random-array/#findComment-480772
Share on other sites

You can just store the played matches in a database. Then you make random matches and for each match, you check whether the two teams have already played against eachother in the database. If they have, start over and make a new random match. That's called a recursive function. It works like this:

 

$result = haveTeamsPlayed( $team1, $team2 );
while( $result == true )
{
      $teams = determineNewMatch();

      $result = haveTeamsPlayed( $teams[ "1" ], $teams[ "2" ] );
}

 

Then put this in a for loop, so you create x new matches every round. I hope this helps you a bit.

 

Link to comment
https://forums.phpfreaks.com/topic/93751-printing-random-array/#findComment-481006
Share on other sites

my 0.02

 

<?php

$teams = array (1,2,3,4,5,6);
$matches = array();

$k = count($teams);

    /**
    * get the matches to be played
    */
for ($i=0; $i<$k-1; $i++)
{
    for ($j=$i+1; $j<$k; $j++)
    {
        $matches[] = array($teams[$i] , $teams[$j]);
    }
}

    /**
    * schedule those matches over 5 weeks
    */
do {
    shuffle($matches);
    $weeks = array(array(),array(),array(),array(),array());
    foreach ($matches as $m)
    {
        schedule($m, $weeks);
    }
} while (countMatches($weeks)<15);      // ensure all are included

    /**
    * output the schedule of matches
    */
echo '<table cellspacing="5">';
echo '<tr>';
for ($i=1; $i<=5; $i++) echo "<td>Week $i</td>";
echo '</tr>';

for ($m=0; $m<3; $m++)
{
    echo '<tr>';
    foreach ($weeks as $wk)
    {
        echo "<td>{$wk[$m][0]} v {$wk[$m][1]}</td>";
    }
    echo '</tr>';
}

echo '</table>';

    /**
    * FUNCTIONS
    */
function countMatches (&$weeks)
{
    $res = 0;
    foreach ($weeks as $w) $res += count($w);
    return $res;
}

function  schedule ($match, &$weeks)
{
    for ($i=0; $i<5; $i++)
    {
        if (count($weeks[$i]) < 3 && !teamHasMatch($match[0], $weeks[$i]) && !teamHasMatch($match[1], $weeks[$i]))
        {
            $weeks[$i][] = $match;
            return;
        }
    }
}

function teamHasMatch ($team, $week)
{
    foreach ($week as $match)
    {
        if (in_array($team, $match)) return true;
    }
    return false;
}
?>

-->
Week 1   Week 2   Week 3   Week 4   Week 5 
3 v 6    2 v 6    2 v 5    2 v 4    4 v 6 
4 v 5    1 v 4    1 v 6    5 v 6    1 v 5 
1 v 2    3 v 5    3 v 4    1 v 3    2 v 3 

Link to comment
https://forums.phpfreaks.com/topic/93751-printing-random-array/#findComment-481161
Share on other sites

try

<?php
function generate($a){
if (!is_array($a)) return false;
$a = array_values($a);
if (count($a)%2) $a[]='';
$rounds = count($a) -1;
$first = array_shift($a);
$cur =0;
while ($cur < $rounds){
	$out[$cur][]=array($first, $a[$rounds-1]);
	for ($i = 0; $i < ($rounds-1) / 2; $i++){
		$out[$cur][] = array($a[$i],$a[$rounds-$i-2]);
	}
	$x = array_shift($a);
	$a[] = $x;
	$cur++;
}
return $out;
}

$test = array(1,2,3,4,5,6,7);
$x = generate($test);
//print_r($x);
foreach ($x as $r => $v){
echo 'round ', $r+1,"<br />\n";
$y = '';
foreach ($v as $p) if ($p[0] and $p[1]) echo $p[0],' v ',$p[1],"<br />\n"; else $y = $p[0].$p[1];
if ($y) echo $y, ' - free';
echo "<hr />\n";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/93751-printing-random-array/#findComment-481417
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.