im trying to write code that will simulate the matches and flow of a football tournament, where the code will ask the user for - number of teams participating, the name of each team as well as their attributes (attack, defence, etc) and then split them into groups of four. they all play each other once then the top two teams from each group move into knockout stage until one team is the overall winner.
i have got the code reading in all the teams and their attributes and storing these as structs and but i am having serious trouble putting the teams into groups!! any help would be much appreciated :) Im very new/rubbish at this!
int main (int argc, char * const argv[])
{
int NumberOfTeams;
cout<<"Welcome to the World Cup Simulator"<<endl;
cout<<"\nPlease select the number of teams participating";
cout<<"\n(please note, the required number of teams per group is 4; with";
cout<<"\n the number of groups corresponding to each selection shown below)."<<endl;
cout<<"\n 8 (2 groups)";
cout<<"\n 16 (4 groups)";
cout<<"\n 32 (8 groups)\n"<< endl;
cin>>NumberOfTeams;
if (NumberOfTeams == 8)
{
team A;
teaminput(&A);
team B;
teaminput(&B);
team C;
teaminput(&C);
team D;
teaminput(&D);
group groupOne;
//groupOne.teamOne = A.name;
groupOne.teamOne(&A);
//A->groupOne.teamOne;
team E;
teaminput(&E);
team F;
teaminput(&F);
team G;
teaminput(&G);
team H;
teaminput(&H);
cout<<"\n"<< A.name<<"\n"<<A.attack<<"\n"<<A.defence;
}
else if (NumberOfTeams == 16)
{
team A;
teaminput(&A);
team B;
teaminput(&B);
team C;
teaminput(&C);
team D;
teaminput(&D);
team E;
teaminput(&E);
team F;
teaminput(&F);
team G;
teaminput(&G);
team H;
teaminput(&H);
team I;
teaminput(&I);
team J;
teaminput(&J);
team K;
teaminput(&K);
team L;
teaminput(&L);
team M;
teaminput(&M);
team N;
teaminput(&N);
team O;
teaminput(&O);
team P;
teaminput(&P);
}
else if (NumberOfTeams == 32)
{
team A;
teaminput(&A);
team B;
teaminput(&B);
team C;
teaminput(&C);
team D;
teaminput(&D);
team E;
teaminput(&E);
team F;
teaminput(&F);
team G;
teaminput(&G);
team H;
teaminput(&H);
team I;
teaminput(&I);
team J;
teaminput(&J);
team K;
teaminput(&K);
team L;
teaminput(&L);
team M;
teaminput(&M);
team N;
teaminput(&N);
team O;
teaminput(&O);
team P;
teaminput(&P);
team Q;
teaminput(&Q);
team R;
teaminput(&R);
team S;
teaminput(&S);
team T;
teaminput(&T);
team U;
teaminput(&U);
team V;
teaminput(&V);
team W;
teaminput(&W);
team X;
teaminput(&X);
team Y;
teaminput(&Y);
team Z;
teaminput(&Z);
team AA;
teaminput(&AA);
team BB;
teaminput(&BB);
team CC;
teaminput(&CC);
team DD;
teaminput(&DD);
team EE;
teaminput(&EE);
team FF;
teaminput(&FF);
}
}
using this piece of code, where would it be placed?
I have a function that can decide the outcome of a fixture between two teams and give the relevant points but it stands alone at present and im struggling to pass the right stuff into/out of it to utilise it properly.
int Fixture ()
{
string teamA, teamB;
int teamAattack, teamBattack = 0;
int teamAdefence, teamBdefence = 0;
int teamAgoals, teamBgoals = 0;
int teamApoints, teamBpoints = 0;
if (teamAattack-teamBdefence>0)
teamAgoals = teamAattack-teamBdefence;
if (teamBattack-teamAdefence>0)
teamBgoals = teamBattack-teamAdefence;
You'll need a points member in your team structure,
1 2 3 4 5 6 7
typedefstruct team
{
string name;
int attack;
int defence;
int points;
} team;
As you can probably see from your current code, there is a lot of repetition.. and it only gets worse with the more teams you add. You can minimize this by using nested for loops and a vector of vectors,
if (NumberOfTeams == 8 || NumberOfTeams == 16 || NumberOfTeams == 32)
{
constint GroupSize = 4, NumberOfGroups = NumberOfTeams / GroupSize;
vector< vector<team> > groups;
for (int i = 0; i < NumberOfGroups; i++)
{
vector<team> group;
for (int j = 0; j < GroupSize; j++)
{
team X = {};
teaminput(&X);
group.push_back(X);
}
groups.push_back(group);
}
// Assuming NumberOfTeams is 8, groups contains 2 groups, each group consists of 4 teams.
// Now to have each team in each group play against every team in every other group,
for (auto groupA = groups.begin(); groupA != groups.end(); groupA++)
{
for (auto groupB = groupA+1; groupB != groups.end(); groupB++)
{
for (auto teamA = groupA->begin(); teamA != groupA->end(); teamA++)
{
for (auto teamB = groupB->begin(); teamB != groupB->end(); teamB++)
{
Fixture(*teamA, *teamB);
}
}
}
}
// phew!
}