This is a recreational project for my fantasy football league. I will try to very thoroughly explain my thinking and what I have done so far.
I'm trying to create a random schedule generator. But the problem is there are a lot of conditions that need to be met and whenever I try to implement some of these restrictions it causes infinite loops, which in my opinion, should not be happening. I could just be thinking wrong, or it could be the random number generator.
It is a ten team league and a 13 week season. Every team plays the other nine teams one time. Then each team plays 4 teams twice. What I have done is created an array list of every possible matchup by assigning an id to each team with numbers ranging from 21 to 30 (I chose this because it will create a unique matchup id when any two of the numbers are 21 to 30 are multiplied together). Every team is in an array list called teams. So I did this to create all the unique matchups:
This works every time and is not the problem. The problem comes when I try to randomly generate the 4 duplicate matchups for each team. My function to do this is as follows:ArrayList<Matchups> defMatchups = new ArrayList<Matchups>(); for(i = 0; i < 10; i++){ for(j = i + 1; j < 10; j++){ defMatchups.add(new Matchups(teams.get(i), teams.get(j))); }
package helpers; import java.util.ArrayList; import java.util.Iterator; import java.util.Random; import matchups.Matchups; import teams.Teams; public class RandomizeSecondMatchups { public static ArrayList<Matchups> run(ArrayList<Matchups> original){ ArrayList<Matchups> list = new ArrayList<Matchups>(); ArrayList<Matchups> copy = new ArrayList<Matchups>(); Iterator<Matchups> it = original.iterator(); Matchups current; while(it.hasNext()){ current = it.next(); copy.add(current); } int counter = 0; Random rand = new Random(); Teams temp1, temp2; Matchups tempMU; do{ [B] do{ tempMU = copy.get(rand.nextInt(copy.size())); System.out.println("--"); }while(tempMU.getTeamOne().getCount() >=4 || tempMU.getTeamTwo().getCount() >= 4 );[/B] temp1 = tempMU.getTeamOne(); temp2 = tempMU.getTeamTwo(); temp1.incCount(); if(temp1.getCount() == 4){ counter++; } temp2.incCount(); if(temp2.getCount() == 4){ counter++; } list.add(tempMU); copy.remove(tempMU); }while(counter < 10); return list; } }
An infinite loop usually occurs in the bolded section (the do while loop with the bold tags around it). But there are a few times (about once every 4 or 5 times) where it does not infinite loop and it executes exactly how I want. Which is making me think that my random number is the problem or possibly even eclipse. I have gotten farther into the program and know I will experience more infinite looping problems later on as I will be adding more restrictions in, (e.g. matchups can't repeat themselves in consecutive weeks), so I would like to get this one to work perfectly before I move on.
Anything you can do to help me out be greatly appreciated. If you need to see other classes in my code to help me out let me know, and I will post it up.