Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe this code solve your problem, it is not as elegant as it could be but it seems to work. You should know ho arrangeGames works ;)</p> <p>Maybe you need to pay attention on <em>arrangePlayerforDays</em>. It put the various games in a day only if both of the players don't have a game already scheduled on that day. The other function are just for make the code more readable (and cover a pair of glitches in <em>arrangePlayerforDays</em>)</p> <pre><code>function gamePrettyPrint($gamesOnADay, $glue) { $result=array(); foreach ($gamesOnADay as $currentGame) $result[]=join($glue,$currentGame); return $result; } function arrangeGamesOnAday($day) { $result=array(); for ($k=0, $limit=count($day); $k&lt;$limit; $k+=2) $result[]=array($day[$k+1], $day[$k]); return $result; } function arrangeGames($players) { for ($i=0, $limit=count($players); $i &lt; $limit; $i++) for ($j=$i+1; $j&lt;$limit;$j++) $games[]=array($players[$i], $players[$j]); return $games; } function calculateTournamentDuration($players) { return count($players)-1; // (n!)/(2!*(n-2)!) * (1/n) } function arrangePlayerforDays($games, $days) { $mem = array_pad(array(),$tournamentDays,array()); for ($k=0;count($games);$k++) if ((array_search($games[0][0],$mem[$k%$days])=== false) and (array_search($games[0][1],$mem[$k%$days])=== false)) list($mem[$k%$days][], $mem[$k%$days][]) = array_shift($games); return ($mem); } function scatterGamesOnCalendar($games, $tournamentDays) { $days=arrangePlayerforDays($games, $tournamentDays); $calendar=array_map('arrangeGamesOnAday',$days); return $calendar; } // $initialArray = array('a','b','c','d','e','f','g','h'); $initialArray = array(30,31,32,33,34,35); $games= arrangeGames($initialArray); $tournamentSpan = calculateTournamentDuration($initialArray); $calendar = scatterGamesOnCalendar($games, $tournamentSpan); while ($day=array_shift($calendar)) $prettyCalendar[]=gamePrettyPrint($day,' v '); print_r($prettyCalendar); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload