Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * author: _where
- *
- * Simple sexy solution, that shows a lil bit of panties.
- */
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.Arrays;
- import java.util.Collections;
- class MatchSched
- {
- private static ArrayList<int[]> homeTeam = new ArrayList<int[]>(); // arraylist to store match combinations(two-element arrays)
- private static ArrayList<Integer> currentTeam = new ArrayList<Integer>();
- private static Random generate = new Random();
- private static boolean filterComplete = false; // boolean indicator to check if changes need to be made to satisfy preconditions
- private final static int[][] derbyGroup = {{0,7}, {2,3,4}}; // teams from same city
- private static boolean[][] highProfile; // store boolean values indicating if an high profile match has taken place
- private static int[][] highProfilePosition; // store positions where high profile matches take place
- private static int tb = 0;
- private static int[] awayMatch; // array used to note consecutive away matches
- public static void main(String[] args)
- {
- for (int counter = 0; counter < 12; counter++)
- {
- int randomMatch = 0;
- currentTeam.clear();
- currentTeam.add(counter); // to make sure a team is not scheduled to play itself
- while(true)
- {
- if (currentTeam.size() == 12) // stop match generation if matches have been scheduled with all other teams
- break;
- randomMatch = generate.nextInt(12);
- Collections.sort(currentTeam);
- int index = Collections.binarySearch(currentTeam, randomMatch); // search arraylist to see if team has already been added
- if (index >= 0)
- continue;
- else
- {
- int[] newMatch = {counter, randomMatch};
- homeTeam.add(newMatch); // add match combo
- currentTeam.add(randomMatch); // ensure team is not selected at the next random pick
- }
- }
- }
- // shuffle schedule arraylist
- Collections.shuffle(homeTeam);
- Collections.reverse(homeTeam);
- Collections.shuffle(homeTeam);
- while (!filterComplete) // make sure schedule satisfies all preconditions
- filterMatches();
- System.out.printf("After %d passes, schedule generated:\n", tb);
- printSchedule(); // print match schedule
- }
- private static void printSchedule()
- {
- for (int t = 0; t < homeTeam.size(); t++)
- {
- int firstT = homeTeam.get(t)[0];
- int secondT = homeTeam.get(t)[1];
- System.out.printf("\nTeam %s vs Team %s", teamName(firstT), teamName(secondT));
- }
- }
- private static String teamName(int param)
- {
- String m = "";
- switch(param)
- {
- case 0:
- m = "A";
- break;
- case 1:
- m = "B";
- break;
- case 2:
- m = "C";
- break;
- case 3:
- m = "D";
- break;
- case 4:
- m = "E";
- break;
- case 5:
- m = "F";
- break;
- case 6:
- m = "G";
- break;
- case 7:
- m = "H";
- break;
- case 8:
- m = "I";
- break;
- case 9:
- m = "J";
- break;
- case 10:
- m = "K";
- break;
- case 11:
- m = "L";
- break;
- default:
- m = ";-)";
- break;
- }
- return m;
- }
- private static boolean filterMatches()
- {
- tb++;
- filterComplete = true; // boolean indicator to check if changes are made to satisfy preconditions
- awayMatch = new int[12];
- highProfilePosition = new int[3][3];
- highProfile = new boolean[3][3];
- int end = 0; // variable used in swapping
- int currentAway = -999; // arbitrary value to initialize variable
- for (int i = 0; i < homeTeam.size(); i++)
- {
- int firstT = homeTeam.get(i)[0];
- int secondT = homeTeam.get(i)[1];
- // away match precondition
- if (currentAway == secondT)
- awayMatch[secondT]++;
- else
- awayMatch[secondT] = 0;
- currentAway = secondT;
- if (awayMatch[secondT] == 2)
- {
- end = generate.nextInt(132);
- if (i != end)
- {
- Collections.swap(homeTeam, i, end); // swap positions in order to satisfy away match precondition
- filterComplete = false; // indicator set to false to indicate correction
- }
- else // re-filter if swap positions clash
- {
- filterComplete = false; // indicator set to false to indicate correction
- break;
- }
- }
- // high profile match precondition
- if ((firstT == 0 | firstT == 1 | firstT ==2) & ( secondT == 0 | secondT == 1 | secondT == 2))
- {
- highProfile[firstT][secondT] = true;
- highProfilePosition[firstT][secondT] = i;
- if (highProfile[secondT][firstT])
- {
- if ((highProfilePosition[secondT][firstT] <= 66) &
- (highProfilePosition[firstT][secondT] <= 66)) // two high profile teams play twice in first half of the season
- {
- end = 0;
- while (end <= 66)
- end = generate.nextInt(132);
- Collections.swap(homeTeam, i, end); // swap positions in order to satisfy high profile team-play precondition
- filterComplete = false; // indicator set to false to indicate correction
- }
- if ((highProfilePosition[secondT][firstT] > 66)) // two high profile teams play twice in second half of the season
- {
- end = generate.nextInt(66);
- Collections.swap(homeTeam, i, end); // swap positions in order to satisfy high profile team-play precondition
- filterComplete = false; // indicator set to false to indicate correction
- }
- }
- }
- // derby match precondition
- if (i == 0) // skip first match scheduled
- continue;
- else
- {
- int initX = homeTeam.get(i-1)[0]; // previous home team
- int initY = homeTeam.get(i-1)[1]; // previous away team
- int a = 0, b = 1, c = 2, d = 3; // initialize elements used in searching. Arbitrary values assigned.
- // search derby group array and take note of row positions
- for (int r = 0; r < derbyGroup.length; r++)
- {
- for (int s = 0; s < derbyGroup[r].length; s++)
- {
- if (derbyGroup[r][s] == initX)
- a = r;
- if (derbyGroup[r][s] == initY)
- b = r;
- if (derbyGroup[r][s] == homeTeam.get(i)[0])
- c = r;
- if (derbyGroup[r][s] == homeTeam.get(i)[1])
- d = r;
- }
- }
- if (a == b & c == d) // if derby matches follow
- {
- end = generate.nextInt(132);
- if (i != end)
- {
- Collections.swap(homeTeam, i, end); // swap positions to satisfy derbymatch precondition
- filterComplete = false;
- }
- else
- {
- filterComplete = false; // indicator set to false to indicate correction
- break;
- }
- }
- }
- }
- return filterComplete; // return filter status. if no corrections made, it returns true, else false.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement