football tournament simulator

Aug 17, 2011 at 9:34pm
hi guys

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!

header file:


#include <iostream>
#include <sstream>
#include <string>

using namespace std;

typedef struct team
{
string name;
int attack;
int defence;
} team;

typedef struct group
{
string teamOne;
string teamTwo;
string teamThree;
string teamFour;

} group;

main cpp:


#include "main.h"

using namespace std;


void teaminput(team *A)
{
//team A;
string x;
int y;
//for (int i=0; i<4; i++)
//{
// cout << "\nPlease enter team " << i+1 << " data." << endl;

cout<<"\nTeam Name\t\t\t: \t";
cin>> x;
A->name = x ;//TeamName

cout<<"Attack Level\t\t: \t";
cin >> y;
A->attack=y; //AttackLevel

cout<<"Defence Level\t\t: \t";
cin>> y;
A->defence = y;//DefenceLevel
}


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);
}
}

Aug 18, 2011 at 6:12am
closed account (DSLq5Di1)
Consider using a vector to group your teams,
1
2
3
4
5
6
vector<team> groupOne;

groupOne.push_back(A);
groupOne.push_back(B);
groupOne.push_back(C);
groupOne.push_back(D);

1
2
3
4
5
6
7
8
9
// pseudo-code
for (auto home_team = groupOne.begin(); home_team != groupOne.end(); home_team++)
{
    for (auto away_team = groupTwo.begin(); away_team != groupTwo.end(); away_team++)
    {
        home_team->play(away_team); // determine a winner and assign points accordingly
    }
}
sort(groupOne.begin(), groupOne.end(), points_desc) // sort by points in descending order 

http://www.cplusplus.com/reference/stl/vector/
http://www.cplusplus.com/reference/algorithm/
Aug 18, 2011 at 11:39am
Thanks sloppy9, looking at vectors and they seem like a better option :)

vector<team> groupOne;

groupOne.push_back(A);
groupOne.push_back(B);
groupOne.push_back(C);
groupOne.push_back(D);

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;

if (teamAgoals>teamBgoals)
{
teamApoints = teamApoints +3;
}
else if(teamBgoals>teamAgoals)
{
teamBpoints = teamBpoints +3;
}
else if (teamAgoals==teamBgoals)
{
teamApoints = teamApoints +1;
teamBpoints = teamBpoints +1;
}


cout << teamA << "\t" << teamAgoals << " v " << teamBgoals << "\t" << teamB << endl;
cout << teamA << "\t - " << teamApoints << " points." << endl;
cout << teamB << "\t - " << teamBpoints << " points." << endl;

return teamApoints, teamBpoints;
}

Once again, im pretty poor at coding, and any help is greatly appreciated

anna :)
Aug 18, 2011 at 4:45pm
closed account (DSLq5Di1)
Ah sorry.. you can place that anywhere after your teaminput lines,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
if (NumberOfTeams == 8)
{
    team A;
    teaminput(&A);
    team B;
    teaminput(&B);
    team C;
    teaminput(&C);
    team D;
    teaminput(&D);

    vector<team> groupOne;
    groupOne.push_back(A);
    groupOne.push_back(B);
    groupOne.push_back(C);
    groupOne.push_back(D);

    team E;
    teaminput(&E);
    team F;
    teaminput(&F);
    team G;
    teaminput(&G);
    team H;
    teaminput(&H);

    vector<team> groupTwo;
    groupTwo.push_back(E);
    groupTwo.push_back(F);
    groupTwo.push_back(G);
    groupTwo.push_back(H);

    for (auto teamA = groupOne.begin(); teamA != groupOne.end(); teamA++)
    {
        for (auto teamB = groupTwo.begin(); teamB != groupTwo.end(); teamB++)
        {
            Fixture(*teamA, *teamB);
        }
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void Fixture(team& teamA, team& teamB)
{
    string teamA, teamB;
    int teamAattack, teamBattack = 0;
    int teamAdefence, teamBdefence = 0;
    int teamAgoals = 0, teamBgoals = 0; // previously you were only initializing teamB' variables
    int teamApoints = 0, teamBpoints = 0;

    if (teamA.attack - teamB.defence > 0)
        teamAgoals = teamA.attack - teamB.defence;
    if (teamB.attack - teamA.defence > 0)
        teamBgoals = teamB.attack - teamA.defence;

    if (teamAgoals > teamBgoals)
    {
        teamA.points = teamA.points +3;
    }
    else if(teamBgoals > teamAgoals)
    {
        teamB.points = teamB.points +3;
    }
    else if (teamAgoals == teamBgoals)
    {
        teamA.points = teamA.points +1;
        teamB.points = teamB.points +1;
    }
}

You'll need a points member in your team structure,

1
2
3
4
5
6
7
typedef struct 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,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
if (NumberOfTeams == 8 || NumberOfTeams == 16 || NumberOfTeams == 32)
{
    const int 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!
}
Topic archived. No new replies allowed.