I'm making a basic football (soccer) manager game as my school project. Most of the other stuff is sorted out, but the scheduler is not working...
Background:
I'm simulating the English Premier League,which has 20 teams. Each team plays 38 matches (exactly one match per week over 38 weeks) in a double round robin format.There are 20 teams, so 10 matches are played every week. First each team plays the other exactly once over the first 19 weeks, then the whole thing repeats it self.
I'm scheduling the first 19 weeks and then duplicating the schedule for the next 19 weeks.

I've written the code for the first 19 weeks (as soon as it works I'll write the code to duplicate the the next 19):

I HAVE TO use Turbo C++ to compile it (as per the school rules/syllabus)... hence the old headers..

``````#include<iostream.h>
#include<stdlib.h>
#include<time.h>

int main()
{
unsigned int seedval;
time_t ti;
seedval =(unsigned)time(&ti);
srand( time(&ti) );

int matchlist[21][39]; //[Team id][Week number]
for (int i=1;i<=20;i++)
{
for (int j=1;j<=38;j++)
{

matchlist[i][j]=0;

}

}
cout<<"Schedule init completed";
int t;
int flag;
for (int i=1;i<=20;i++)
{
for (int j=1;j<=19;j++)
{

if ((matchlist[i][j]==0))
{
flag=1;//So that the while loop is run atleast once
while (flag==1)
{
flag=0;
t=rand()%20 + 1; //Generate random opponent 't' for team 'i' on week 'j'

if (t==i)
{
flag=1;
}

if (flag==0)
{
for (int p=1;p<=20;p++) //Check if team 't' is playing anyone else the same week..
{
if (matchlist[p][j]==t)
{
flag=1;
break;
}

}
}

if (flag==0)
{
for (int z=1;z<=19;z++) //Check if same teams are playing each other more than once in the 19 weeks
{
if (matchlist[i][z]==t)
{
flag=1;
break;
}
}
}

if (flag==0)
{
matchlist[i][j]=t;//Team i plays t
matchlist[t][j]=i;//So Team t plays i
}
}
}
}
}

cout<<"Scheduled";
int listed[21];

//LOOP TO DISPLAY Week-wise Schedule (first five weeks):
for (int i=1;i<=5;i++)
{
for (int z=1;z<=20;z++)
{
listed[z]=0;
}

cout<<"\n\nWeek "<<i<<"\n\n";
for (int j=1;j<=20;j++)
{
if (listed[j]==0)//One match should be displayed just once
{
cout<<"\n\n"<<j<<" "<<matchlist[j][i];
listed[j]=1;
listed[matchlist[j][i]]=1;
}
}
}
}``````

The code is compiling , but the loop on line 59 is leading to an infinte loop or some other problem .. I guess it's some type of logical error.

The program runs completely without that particular loop.. but without it, the same match may get scheduled again for other weeks. This musn't happen as each team plays the other exactly once..

Appreciate any help in identifying the mistake in the code..

I added this code on line 32 to identify the problem:

``cout<<" Team "<<i<<" Match"<<j;``

Thus managed to identify where it was stopping.
The program gets stuck at some random point every time
Once it got stuck at Team 5 Week 9
on the next run it managed to reach Team 9 Week 19 before getting stuck

I think this should be happening only if it is running out of combinations .. but I can't figure out why that's happening

I modified the code to display errors:

``````int t;
int flag;
for (int i=1;i<=20;i++)
{
for (int j=1;j<=19;j++)
{

if ((matchlist[i][j]==0))
{cout<<" Team "<<i<<" Match"<<j;
flag=1;//So that while loop is run atleast once
while (flag==1)
{
flag=0;
t=rand()%20 + 1;
cout<<"\n\n team1 "<<i<<" team2 "<<t;
if (t==i)//Paired with self
{
flag=1;
cout<<" self";

}

if (flag==0)
{
for (int p=1;p<=20;p++) //Check if team t is playing anyone else the same week..
{
if (matchlist[p][j]==t)
{
flag=1;
cout<<" "<<t<<" is already playing with"<<p;
break;
}

}
}

if (flag==0)
{
for (int z=1;z<=19;z++) //Check if same match is scheduled for any other week..
{
if (matchlist[i][z]==t)
{
flag=1;
cout<<" REJECTED Same week "<<t<<"is playing with"<<z;
break;
}
}
}

if (flag==0)
{
matchlist[i][j]=t;
matchlist[t][j]=i;
}
}
}
}
}``````

Here's the output:
[IMG]http://i45.tinypic.com/2w4k00m.jpg[/IMG]

CAN ANYONE Help me to identify what's wrong ????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.