ClarkeRubber

UNSW ProgComp: Problem 4 - 2005

Jun 9th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.08 KB | None | 0 0
  1. <?php
  2.  
  3. $input = <<<END
  4. Koalas
  5. Platypi
  6. Bilbies
  7. Thylacines
  8. Wallaroos
  9. END;
  10.  
  11. //There are n-1 rounds
  12. $input = explode("\n", $input);
  13.  
  14. $count = count($input);
  15.  
  16. $always_first = array_shift($input);
  17.  
  18. //var_dump($input);
  19.  
  20. if($count%2){
  21.    
  22.     //if there is an even amount of teams
  23.     //If there is an odd amount of teams
  24.     $input[] = "bye";
  25.     $count++;
  26.     for($x = 0; $x < $count-1; $x++){
  27.         //This loop is for each round
  28.  
  29.         //there is another for for echoing (n+1)/2 lines of draw
  30.         $out = $x+1;
  31.         echo "Round $out:\n";
  32.         for($y = 0; $y < ($count+1)/2-1; $y++){
  33.  
  34.             //alway first vs input[last]
  35.             //2 vs input[last-1]
  36.             //3 vs input[last-y]
  37.  
  38.             $first = $input[$y-1];
  39.             $second = $input[$count-$y-2];
  40.  
  41.             if($y == 0){
  42.                 if(end($input) == 'bye'){
  43.                     echo str_pad($always_first, 12, ' ', STR_PAD_RIGHT).end($input)."\n";
  44.                 }else{
  45.                     echo str_pad($always_first, 12, ' ', STR_PAD_RIGHT)."vs ".end($input)."\n";
  46.                 }
  47.             }elseif($first == 'bye' || $second == 'bye'){
  48.                 if($first == 'bye'){
  49.                     echo str_pad($second, 12, ' ', STR_PAD_RIGHT).$first."\n";
  50.                 }else{
  51.                     echo str_pad($first, 12, ' ', STR_PAD_RIGHT).$second."\n";
  52.                 }
  53.             }else{
  54.                 echo str_pad($first, 12, ' ', STR_PAD_RIGHT)."vs ".$second."\n";
  55.             }
  56.         }
  57.         //rotate array counter clockwise
  58.         array_unshift($input, array_pop($input));
  59.         //var_dump($input);
  60.     }
  61. }else{
  62.    
  63.     //if there is an even amount of teams
  64.     //If there is an odd amount of teams
  65.     //$input[] = "bye";
  66.     for($x = 0; $x < $count-1; $x++){
  67.         //This loop is for each round
  68.  
  69.         //there is another for for echoing (n+1)/2 lines of draw
  70.         $out = $x+1;
  71.         echo "Round $out:\n";
  72.         for($y = 0; $y < ($count+1)/2-1; $y++){
  73.  
  74.             //alway first vs input[last]
  75.             //2 vs input[last-1]
  76.             //3 vs input[last-y]
  77.  
  78.             $first = $input[$y-1];
  79.             $second = $input[$count-$y-2];
  80.  
  81.             if($y == 0){
  82.                 echo str_pad($always_first, 12, ' ', STR_PAD_RIGHT)."vs ".end($input)."\n";
  83.             }else{
  84.                 echo str_pad($first, 12, ' ', STR_PAD_RIGHT)."vs ".$second."\n";
  85.             }
  86.         }
  87.         //rotate array counter clockwise
  88.         array_unshift($input, array_pop($input));
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment