title: Football league scheduling algorithm (with sync. home/away)
body: <p>I'm making an automatic football league generator and got stuck on schedule. 
  I have an algorithm for generating everything, but not schedule. Take a look:</p>
  
  <pre><code>[tour-id] =&gt; Array
      (
          [playing-day] =&gt; 2013-03-07
          [tour (tour-id+1)] =&gt; 9
          [game-id] =&gt; Array
              (
                  [blue team-id] =&gt; 13
                  [red team-id] =&gt; 10
              )
  </code></pre>
  
  <p>For example:</p>
  
  <pre><code>[8] =&gt; Array
      (
          [playday] =&gt; 2013-03-07
          [tour] =&gt; 9
          [0] =&gt; Array
              (
                  [blue] =&gt; 13
                  [red] =&gt; 10
              )
  
          [1] =&gt; Array
              (
                  [red] =&gt; 15
                  [blue] =&gt; 12
              )
  
          [2] =&gt; Array
              (
                  [blue] =&gt; 6
                  [red] =&gt; 11
              )
  </code></pre>
  
  <p>And now when i have got all that data I can just use few <code>foreach()</code> for taking what i need. But - the thing i want is to put teams into RED &amp; BLUE teams normally.</p>
  
  <p>I mean:</p>
  
  <ul>
  <li>One team in one side (BLUE or RED) can play maximum 2 times;</li>
  <li>One team in one round must play <code>$totalTours/2</code> games in one round. </li>
  </ul>
  
  <p>For example if there are 9 teams, in 1st round a team must play 4 tours in RED (total, not in a row) and 5 tours in BLUE. In 2nd round it must play 5 tours in RED, 4 tours in BLUE.</p>
  
  <p>The algorithm must work even if there are 100 teams.</p>