Guest User

PHPGangsta Algorithmus-Wettbewerb Teil 2: Spielplan errechnen

a guest
Jun 24th, 2010
319
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $time = microtime(true);
  3.  
  4. // codeteil start
  5. function battle($numPlayers = 20)
  6. {
  7.     for($x = 1; $x <= $numPlayers; $x++)
  8.         $listPlayers[] = $x;
  9.     shuffle($listPlayers);
  10.  
  11.     $listRounds = array();
  12.     $numPlayersHalf = $numPlayers * 0.5;
  13.     $count = 0;
  14.     for($round = 1; $round <= $numPlayersHalf; $round++) {
  15.         for($x = $count; $x < $numPlayersHalf; $x++) {
  16.             $listRounds[$round][] = array($listPlayers[$x], null);
  17.         }
  18.         for($x = 0; $x < $count; $x++) {
  19.             $listRounds[$round][] = array($listPlayers[$x], null);
  20.         }
  21.         $a = 0;
  22.         for($x = $numPlayersHalf + $count; $x < $numPlayers; $x++) {
  23.             $listRounds[$round][$a++][1] = $listPlayers[$x];
  24.         }
  25.         $a = $numPlayersHalf - $count;
  26.         for($x = $numPlayersHalf; $x < $numPlayersHalf + $count; $x++) {
  27.             $listRounds[$round][$a++][1] = $listPlayers[$x];
  28.         }
  29.         $count++;
  30.     }
  31.     return $listRounds;
  32. }
  33.  
  34. $listRounds = battle(20);
  35. // codeteil ende
  36.  
  37. echo '<table border="1">';
  38. echo '<tr>';
  39. echo '<td></td>';
  40. for($x = 0; $x < $numPlayers * 0.5; $x++) {
  41.     echo '<td><strong>Spielbrett ' . $x . '</strong></td>';
  42. }
  43. echo '</tr>';
  44. foreach($listRounds AS $round => $fields) {
  45.     echo '<tr>';
  46.     echo '<td><strong>Runde ' . $round . '</strong></td>';
  47.     foreach($fields AS $players) {
  48.         echo '<td>Spieler ' . $players[0] . ' vs. Spieler ' . $players[1] . '</td>';
  49.     }
  50.     echo '</tr>';
  51. }
  52. echo '</table>';
  53. echo '<p style="text-align: center;">' . (microtime(true) - $time) . ' Sek.</p>';
RAW Paste Data