I am writing some code to simulate a football tournament by comparing several teams on several parameters. I need to be able to knock out teams, edit some stats mid simulation, and be able to easily access individual teams data, what structure would I be best using? I had thought possibly vectors but I am very new to c++ and could do with some advice.
sorry if something similar has already been answered.

cheers
Dan

Recommended Answers

All 6 Replies

I would make a football team class and then store the class in a vector

class football {
int wins;
int lost;
bool goodteam;
};

The best way to do this is to use classes. This is what C++ is all about, and its main difference from C.

I would make a class to store all the information relevant to a team, with some member functions to manipulate that data. For example:

class Team {
  public:
    std::string name;
    std::vector<Player> players;
    int current_standing;
    //...
};

I would probably suggest you store the data in a std::map which allows you to access the elements by a key-value, for instance, the team's name. For example:

int main() {
  std::map<std::string, Team> teams;

  Team& t1 = teams["Patriots"]; //teams can be looked-up and created by name.
  t1.name = "Patriots";
  t1.current_standing = 1;
  //..

  return 0;
};

As for choosing a container, the image attached might help.

Thanks for all the help guys, the only thing i'm still not sure about is how to call the various teams, for example in a 4 team tournament, team 1 plays team 2 and team 3 plays team 4, I can get that far, but lets say 2 and 4 win, how do I then select those teams given that the person running the sim will have named the teams whatever they want to?

If you are new to c++ and looking to learn some data structures, I would suggest you look into creating your own list via pointers. Try googling c++ linked list. Create your own linked list, and when a team loses, you can write a function that will remove that team from the list. There are easier ways to do this using some stl lists, but if you want to learn, do it from scratch!

For structuring the games of the tournament, I would suggest you construct a separate tree-structure for the tournament games. For example, if you construct one game as follows:

class Game {
  private:
    Team* team[2]; //have two teams that are involved in this game.
    Game* next_for_winner; //the game that the winner would advance to.
    Game* next_for_loser;  //the game the loser might do (like a bronze-medal final).
  public:
    //..
};

Then, you construct a list of games, link them and initialize the teams for the first round of elimination. Say you have 8 teams in total, than you know there will be 4 quarter-final games, 2 semi-final games, and 2 finals (gold and bronze), that's 8 games in total (Number of games in a tournament with small-final is equal to the number of teams in total, if it is a power of two). So, you create 8 games (in a std::vector). Then you fill the first 4 games with the 8 teams, with whatever match-ups make sense in your program (like season standings, or preliminary round results). Then, you link the "next_for_winner" and "next_for_loser" pointers to the games that follow in the list, if there is no game to follow for the loser, or if it is a final, then you set the next game pointers to NULL. If there are yet no teams assigned to a game (results are not known yet, previous round is not completed), then set the team pointers to NULL as well. When you play a game and determine the winner, you follow the "next_for_winner" pointer and assign the first NULL team-pointer to that winning team. By the time you have played the first round, all the team pointers in the second round will have been filled appropriately and you can start playing out the second round, then the third, and so on. This type of structure is akin to a binary tree, but reversed. All you have to do to play out the tournament, once the structure is in place, is to traverse the list of games from start to finish, and playing all the games along the way.

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.