Advertisement
codeaddict

Match League

Oct 23rd, 2013
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.53 KB | None | 0 0
  1. /*
  2.  * author: _where
  3.  *
  4.  * Simple sexy solution, that shows a lil bit of panties.
  5.  */
  6. import java.util.ArrayList;
  7. import java.util.Random;
  8. import java.util.Arrays;
  9. import java.util.Collections;
  10. class MatchSched
  11. {
  12.     private static ArrayList<int[]> homeTeam = new ArrayList<int[]>(); // arraylist to store match combinations(two-element arrays)
  13.     private static ArrayList<Integer> currentTeam = new ArrayList<Integer>();
  14.     private static Random generate = new Random();
  15.     private static boolean filterComplete = false; // boolean indicator to check if changes need to be made to satisfy preconditions
  16.     private final static int[][] derbyGroup = {{0,7}, {2,3,4}}; // teams from same city
  17.     private static boolean[][] highProfile; // store boolean values indicating if an high profile match has taken place
  18.     private static int[][] highProfilePosition; // store positions where high profile matches take place
  19.     private static int tb = 0;
  20.     private static int[] awayMatch; // array used to note consecutive away matches
  21.     public static void main(String[] args)
  22.     {
  23.         for (int counter = 0; counter < 12; counter++)
  24.         {
  25.             int randomMatch = 0;
  26.             currentTeam.clear();
  27.             currentTeam.add(counter); // to make sure a team is not scheduled to play itself
  28.             while(true)
  29.             {
  30.                 if (currentTeam.size() == 12) // stop match generation if matches have been scheduled with all other teams
  31.                     break;
  32.                 randomMatch = generate.nextInt(12);
  33.                 Collections.sort(currentTeam);
  34.                 int index = Collections.binarySearch(currentTeam, randomMatch); // search arraylist to see if team has already been added
  35.                 if (index >= 0)
  36.                     continue;
  37.                 else
  38.                 {
  39.                     int[] newMatch = {counter, randomMatch};
  40.                     homeTeam.add(newMatch);  // add match combo
  41.                     currentTeam.add(randomMatch); // ensure team is not selected at the next random pick
  42.                 }
  43.             }
  44.         }
  45.         // shuffle schedule arraylist
  46.         Collections.shuffle(homeTeam);
  47.         Collections.reverse(homeTeam);
  48.         Collections.shuffle(homeTeam);
  49.        
  50.         while (!filterComplete) // make sure schedule satisfies all preconditions
  51.             filterMatches();
  52.         System.out.printf("After %d passes, schedule generated:\n", tb);
  53.         printSchedule(); // print match schedule
  54.     }
  55.     private static void printSchedule()
  56.     {
  57.         for (int t = 0; t < homeTeam.size(); t++)
  58.         {
  59.             int firstT = homeTeam.get(t)[0];
  60.             int secondT = homeTeam.get(t)[1];
  61.             System.out.printf("\nTeam %s vs Team %s", teamName(firstT), teamName(secondT));
  62.         }
  63.     }
  64.     private static String teamName(int param)
  65.     {
  66.         String m = "";
  67.         switch(param)
  68.         {
  69.             case 0:
  70.                 m = "A";
  71.                 break;
  72.             case 1:
  73.                 m = "B";
  74.                 break;
  75.             case 2:
  76.                 m = "C";
  77.                 break;
  78.             case 3:
  79.                 m = "D";
  80.                 break;
  81.             case 4:
  82.                 m = "E";
  83.                 break;
  84.             case 5:
  85.                 m = "F";
  86.                 break;
  87.             case 6:
  88.                 m = "G";
  89.                 break;
  90.             case 7:
  91.                 m = "H";
  92.                 break;
  93.             case 8:
  94.                 m = "I";
  95.                 break;
  96.             case 9:
  97.                 m = "J";
  98.                 break;
  99.             case 10:
  100.                 m = "K";
  101.                 break;
  102.             case 11:
  103.                 m = "L";
  104.                 break;
  105.             default:
  106.                 m = ";-)";
  107.                 break;
  108.         }
  109.         return m;
  110.     }
  111.     private static boolean filterMatches()
  112.     {
  113.         tb++;
  114.         filterComplete = true; // boolean indicator to check if changes are made to satisfy preconditions
  115.         awayMatch = new int[12];
  116.         highProfilePosition = new int[3][3];
  117.         highProfile = new boolean[3][3];
  118.         int end = 0; // variable used in swapping
  119.         int currentAway = -999; // arbitrary value to initialize variable
  120.         for (int i = 0; i < homeTeam.size(); i++)
  121.         {
  122.             int firstT = homeTeam.get(i)[0];
  123.             int secondT = homeTeam.get(i)[1];
  124.            
  125.             // away match precondition
  126.             if (currentAway == secondT)
  127.                 awayMatch[secondT]++;
  128.             else
  129.                 awayMatch[secondT] = 0;
  130.             currentAway = secondT;
  131.             if (awayMatch[secondT] == 2)
  132.             {
  133.                 end = generate.nextInt(132);
  134.                 if (i != end)
  135.                 {
  136.                     Collections.swap(homeTeam, i, end); // swap positions in order to satisfy away match precondition
  137.                     filterComplete = false; // indicator set to false to indicate correction
  138.                 }
  139.                 else // re-filter if swap positions clash
  140.                 {
  141.                     filterComplete = false; // indicator set to false to indicate correction
  142.                     break;
  143.                 }
  144.             }
  145.            
  146.             // high profile match precondition
  147.             if ((firstT == 0 | firstT == 1 | firstT ==2) & ( secondT == 0 | secondT == 1 | secondT == 2))
  148.             {
  149.                 highProfile[firstT][secondT] = true;
  150.                 highProfilePosition[firstT][secondT] = i;
  151.                 if (highProfile[secondT][firstT])
  152.                 {
  153.                     if ((highProfilePosition[secondT][firstT] <= 66) &
  154.                         (highProfilePosition[firstT][secondT] <= 66)) // two high profile teams play twice in first half of the season
  155.                     {
  156.                         end = 0;
  157.                         while (end <= 66)
  158.                             end = generate.nextInt(132);
  159.                         Collections.swap(homeTeam, i, end); // swap positions in order to satisfy high profile team-play precondition
  160.                         filterComplete = false; // indicator set to false to indicate correction
  161.                     }
  162.                     if ((highProfilePosition[secondT][firstT] > 66)) // two high profile teams play twice in second half of the season
  163.                     {
  164.                         end = generate.nextInt(66);
  165.                         Collections.swap(homeTeam, i, end); // swap positions in order to satisfy high profile team-play precondition
  166.                         filterComplete = false; // indicator set to false to indicate correction
  167.                     }
  168.                 }
  169.             }
  170.            
  171.             //  derby match precondition
  172.             if (i == 0) // skip first match scheduled
  173.                 continue;
  174.             else
  175.             {
  176.                 int initX = homeTeam.get(i-1)[0]; // previous home team
  177.                 int initY = homeTeam.get(i-1)[1]; // previous away team
  178.                 int a = 0, b = 1, c = 2, d = 3; // initialize elements used in searching. Arbitrary values assigned.
  179.                 // search derby group array and take note of row positions
  180.                 for (int r = 0; r < derbyGroup.length; r++)
  181.                 {
  182.                     for (int s = 0; s < derbyGroup[r].length; s++)
  183.                     {
  184.                         if (derbyGroup[r][s] == initX)
  185.                             a = r;
  186.                         if (derbyGroup[r][s] == initY)
  187.                             b = r;
  188.                         if (derbyGroup[r][s] == homeTeam.get(i)[0])
  189.                             c = r;
  190.                         if (derbyGroup[r][s] == homeTeam.get(i)[1])
  191.                             d = r;
  192.                     }
  193.                 }
  194.                 if (a == b & c == d) // if derby matches follow
  195.                 {
  196.                     end = generate.nextInt(132);
  197.                     if (i != end)
  198.                     {
  199.                         Collections.swap(homeTeam, i, end); // swap positions to satisfy derbymatch precondition
  200.                         filterComplete = false;
  201.                     }
  202.                     else
  203.                     {
  204.                         filterComplete = false; // indicator set to false to indicate correction
  205.                         break;
  206.                     }
  207.                 }
  208.             }
  209.         }
  210.         return filterComplete; // return filter status. if no corrections made, it returns true, else false.
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement