Solved

Creating a sports simulation

Posted on 1998-10-07
5
461 Views
Last Modified: 2010-07-27
I am beginning to create a sports(hockey) simulation in C++ using Win95/MSVC++ 4.0  It will be text/statistical related only with no graphics.  I would like to move it to MFC after the main "engine" is completed.

I would like to know an efficient way to hold the roster of players in memory and how they would be linked. My first idea was this:

class Player{
   //list of attributes such as name, jersey number,
       //array of player abilities,(attributes)...etc
   //Here I might put pointers of type Team and Player so
      // that it might point the player to his team and to other
      //players for the purpose of sorting.
};

 I have decided to make it very object oriented so that players and teams could be created and destroyed easly.  All the linking will be done by me without the help of things such as STL(unless you think that would work better).

I have tried to come up with a way to link players to box scores, team and to each other in a way that will make it easy to sort, and create leaderboards.  Also, I would like an easy way to store them to a dat file using as little space as possible (maybe using bitfields)
If you need more information, I will be happy to provide it
              Chris
              nexguy@quik.com
0
Comment
Question by:nexguy
  • 5
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1174635
>>All the linking will be done by me without the help of things such as
>>STL(unless you think that would work better)

STL was written to make the programmer's job easier.  If you want to make this easier, use it.  If you want to do the wrok yourself--good experience--then do it yourself.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174636
I would store the players in either a linked list or if used STL, a vector.  

However, another posibility to consider is to store them in a hash table.  This would allow you to access them quickly by name, or some other attribute you have them hashed on.  (This decission really needs to be made by looking at what data inside the player class will be used in finding a player.)  If there is one aattribute that will always or almost always be used to find a player, like the name, a hasn table may be a good idea.  (If you need to find them based on lots of things, then you may want to store them in a linked list, but provided several hash tables that reference-but don't store, the players.)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174637
The team should be a seperate class.  It should store a a list (linked list or STL vector) of pointers to the players.  Note that it doesn't store the players, but links or pointers to them.  Similarly, the player class will have a pointer to the team object it belongs to.

To make this work, the player is created (constructed) with a NULL team pointer and the team is constructed with a empty player list.  The team will have a member function for adding a player to the team that takes a pointer to the player object to be added.  This function will check that the player is not the member of any team, if it is it probably should remove the player from the team before continueing (or it could report an error, depends on your needs).  Then it would set the player's team pointer to the currrent team (has to be a friend), and it would add the player to te player list.  There would be a remove player member function that set the player's team pointer to NULL and removed the player from the list.  Then to make everything safe, the player's destrcutor will look at its team pointer, if it is not NULL, it is still part of a team (and the team doesn't know the player is being destroyed (danger!) so the player destructor will remove the player from the team.  (this is why the player needs a pointer back to its team).  Similarly, the team's destructor must look through its list of players when it is being destroyed and remve each player in the list from the team.  (because again the players don't know the team is being destroyed.)


0
 
LVL 22

Expert Comment

by:nietod
ID: 1174638
That should get you started.  Think about it and ask if you have questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174639
No questions?  If so, you can ask later.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now