# Help with Schedule Generator

• April 9th, 2013, 12:02 PM
popnfresh
Help with Schedule Generator
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:

Code :

``` 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))); }```
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:
Code :

```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.
• April 9th, 2013, 12:45 PM
Norm
Re: Help with Schedule Generator
Quote:

An infinite loop usually occurs
What keeps the code going around the loop? Is there a variable whose value needs to change to allow the loop to exit and the code is not changing the variable's value?

Try debugging the code by adding some println statements to print out the values of the variables that control the loop so you can see how its value is changing (or not) and understand why there is the long looping.
• April 9th, 2013, 12:55 PM
popnfresh
Re: Help with Schedule Generator
Quote:

Originally Posted by Norm
What keeps the code going around the loop? Is there a variable whose value needs to change to allow the loop to exit and the code is not changing the variable's value?

Try debugging the code by adding some println statements to print out the values of the variables that control the loop so you can see how its value is changing (or not) and understand why there is the long looping.

The loop occurs where I thought it was happening.
I figured out that it happens when only two teams have a count of 3 left, but those two teams have already played each other twice, meaning that they cannot play each other for a third time. Which means I have some thinking to do. If you can think of a fix I would be forever grateful. I've been working on this for quite some time(originally in C, I just started in java). There was also a case where 9 teams would have count of 4 and one team would have a count of 2. Not sure how that happened
• April 9th, 2013, 01:08 PM
Norm
Re: Help with Schedule Generator
Sounds like you need to do some thinking and make a design that will handle the cases.