C++ Fantasy Football Draft Program - Adding Players to Teams - c++

I am working on making a C++ program that will simulate a fantasy football draft.. I have used a linked list to create a list of team names, for each person participating in the draft. Now, I would like to know which way I should go about adding the players a team drafts to their respective team. I have the football players in a read in file and can figure out how to let them choose which, but cannot figure out how to store them on their respective team.
Any help appreciated
Thanks in advance

Well, you ought to have a Team class; the Team class ought to have a container to hold player names (another linked list, let's say). The list you have now should hold Teams rather than Strings.
Eventually the list of player names will probably be upgraded to a list of Player objects -- yet another class you'll want to define.
I know this is vague, but does it help?

It seems like you just need to understand the basic C++ containers a bit better.
One way might be to simply remove the player from a list or array of all players in the league, and add it to the fantasy player's list or array of players.
class Player; // ...
class FantasyPlayer
{
public:
std::vector< Player > picks; // array of my picks
};
std::vector< Player > all_players; // all available picks
std::vector< FantastyPlayer > fantasy_players; // all pickers
int iPicked = ...; // index of picked player in all_players
int iPicker = ...; // index of fantasy player currently picking
// add picked player to this fantasy player's pick list
fantasy_players[iPicker].picks.push_back(all_players[iPicked]);
// remove picked player from available players list
all_players.erase(iPicked);
Another, maybe easier, way to handle it might be to reference the "pickers" directly from the players themselves.
class FantasyPlayer; // ...
class Player
{
public:
Player() : owner(0) { /* empty */ }
FantastyPlayer* owner; // pointer to fantasy player who picked me
};
std::vector< Player > all_players; // all available picks
std::vector< FantastyPlayer > fantasy_players; // all pickers
int iPicked = ...; // index of picked player in all_players
int iPicker = ...; // index of fantasy player currently picking
// create link between a player and its picker
all_players[iPicked].owner = &(fantasy_players[iPicker]);
This code is all intentionally brief and incomplete, but maybe it'll get you started in the right direction. Good luck!

Related

UE4 How to use UMG ListView to display a list of Structs?

I apologize if this seems trivial, but I've searched for an answer for a while now, and I can't seem to find a solution.
I have a list of structs (TArray of structs to be exact.) that represent high scores in my game. (Each struct represents a high score, the fields are something like "Name, Date, Player Score, Game Mode etc" I use this SaveGame method to save a load my array of high scores to and from a file.
I used this with mock values and the system works, now I want to create a UMG widget that will display the list of high scores and this is where I ran into a snag.
I want to use a ListView to show each struct as a ListViewEntry. As far as I understand(I was following this tutorial), A UMG List View needs it's entry widgets to implement the IUserObjectListEntry specifically one has to implement the OnListItemObjectSet (UObject* ListItemObject) method. This method is responsible for assigning an object to the Listview entry and mapping its fields to the various visual widgets. You can see my problem now, I have a list of UStructs and this method needs a UObject pointer.
I'm really at a loss at what I need to do to make this work with a UStruct. Short of creating a dummy UObject that's pretty much identical to my struct and before passing the struct to this function I need to copy its fields into the dummy UObject and pass it instead. I think this method is very inelegant. There has to be a better way. Do you know any?
I wanted to avoid creating a dummy UObject just for the sake of passing it to this function.
I tried to use an array of UObjects instead of an array of Structs but the problem is, an array of UObjects is always an array of pointers, and when it gets saved, the pointers getting saved and not the actual data, so when it's loaded the data is useless.
Maybe there is a Struct-specific interface one can implement for a ListViewEntry widget? Or maybe there is a way to dereference the pointers of the array of Uobjects before saving them?
TL;DR
I have the following stuct:
c++
USTRUCT()
class FHighScoreEntry
{
GENERATED_BODY()
public:
//Player name
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString PlayerName;
//Player Score
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 PlayerScore;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FDateTime CurrentDateTime;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TEnumAsByte<EGameType> GameType;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 AccuracyTrialMaxTries;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 TimeTrialMaxTime;
}
In the following array;
c++
TArray<FHighScoreEntry> HighScores;
I want to show the array of high scores in a UMG ListView. The ListView requires its entries to implement the User List Object interface, which has this function:
As you can see, the event only accepts UObjects. Hence my problem.
This was asked 8 months ago, so may no longer be useful to you. But this post is the only thing I could find when searching for this issue on the Internet, so I am posting my solution for the next person researching this.
At a high level, create a UObject wrapper for the struct.
In my USaveGame class I have an array of structs because as you mentioned, an array of UObject pointers does not actually save any data. I created an UObject derived class that simply contains the same struct as the sole UPROPERTY.
UCLASS(Blueprintable, BlueprintType)
class PORTALTEST_API UHighScoreObject : public UObject
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Score")
FHighScore HighScore;
};
In my Game Instance class, I have an array of pointers to this UObject
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Score")
TArray<UHighScoreObject*> HighScoreArray;
I use this array of UObject pointers for the List View of the widget.
HighScoreWidgetBlueprint
In the Save function of my Game Instance class I clear the struct of arrays and fill it with with the data contained in the array of UObject pointers. (I am only keeping the top ten high scores, so this seemed more efficient than keeping track of changes in both arrays.)
bool UMyGameInstance::SaveHighScore()
{
// Call SaveGameToSlot to serialize and save our SaveHighScoreObject with
//name HighScoreSaveSlot.sav
// Retrieve save values
SaveHighScoreObject->HighScoreArray.Empty();
for (auto HighScore : HighScoreArray)
{
SaveHighScoreObject->HighScoreArray.Add(HighScore->HighScore);
}
// Save game to file
const bool IsSaved = UGameplayStatics::SaveGameToSlot(SaveHighScoreObject,
UNIQUE_HIGHSCORE_SLOT, 0);
return IsSaved;
}
And in the Load function of my game instance I read in the array of structs and populate the array of UObjects.
bool UMyGameInstance::LoadHighScore()
{
// Try to load a saved game file with "HighScoreSaveSlot.sav if it exists
USaveGame* LoadedHighScore =
UGameplayStatics::LoadGameFromSlot(UNIQUE_HIGHSCORE_SLOT, 0);
SaveHighScoreObject = Cast<UHighScoreSaveGame>(LoadedHighScore);
//If the file does not exist, create a new one
if (!SaveHighScoreObject)
{
// Instantiate a new SaveGame object
SaveHighScoreObject = Cast<UHighScoreSaveGame>
(UGameplayStatics::CreateSaveGameObject(UHighScoreSaveGame::StaticClass()));
// Call SaveGameToSlot to serialize and save our game object with name
// "HighScoreSaveSlot.sav"
const bool IsSaved =
UGameplayStatics::SaveGameToSlot(SaveHighScoreObject, UNIQUE_HIGHSCORE_SLOT, 0);
return IsSaved;
}
else
{
for (auto HighScore : SaveHighScoreObject->HighScoreArray)
{
UHighScoreObject* HighScoreObj = NewObject<UHighScoreObject>
((UObject*)GetTransientPackage(), UHighScoreObject::StaticClass());
HighScoreObj->HighScore = HighScore;
HighScoreArray.Add(HighScoreObj);
}
return true;
}
}
ListView is made to represent unique objects, so its items need to be UObject, that’s the way the list view class is made.
That’s because adding/removing/looking up the widget for an item needs to be very fast. An object pointer is just a memory address, so it’s fast to find an item, and you can be sure they’re unique (your list won’t accidentally show two widget for the same object). Structs on the other hand, are any arbitrary data, which can be very long (depends on what they contain). So unless you make a hashing algorithm, it’s very expensive to look up if a struct is already in the list.
So for your needs, you can use objects instead of structs to show high scores. For example, objects for each player, since the players are probably already objects. The widget can then cast to the player class when on item object set, and take the high score variable of the player to show it.
If you want to use structs though, you can create a custom widget to show the high scores. To make a vertical list, just make a VerticalBox in your parent widget and a number of widgets for each item in your list, using the create widget from class node (or NewObject in cpp). Then, add your widgets as children of vertical box using the add child to vertical box function.

(C++) Pointers to Pointers of objects in different classes

everyone. I'm working on a final project for school and it's coming along great, but I've run into a bit of a problem with trying to use a pointer to a pointer. I'll do my best to explain the problem below:
So I have a class called Player that sort of looks like this:
class Player
{
Player();
int health;
void adjustHealth(int);
};
Player::Player()
{
health = 40;
}
void Player::adjustHealth(int adjust)
{
health += adjust;
}
I have another class called Shelter, that include "Player.h" and looks a little like this:
class Shelter
{
Shelter();
Player* player; // Create a pointer to Player class.
};
In the Shelter header file, I have the following in my default constructor:
Shelter::Shelter()
{
...Other code here.
player = new Player();
}
In the Shelter header file, I use this new player for things like:
player->adjustHealth(-1); // Subtract one health from the player.
Which works great.
The problem I'm facing is with creating another class called Church, that is in a separate header file and acts as a separate location in the game. I want Church to use the same player that Shelter does, so it has all of the same stats, etc, rather than creating a new player in Church (which is what I did in Shelter.h).
Right now, I have something like:
class Church
{
Church();
Shelter **cplayer; // This is supposed to be the pointer to the pointer.
};
The default constructor is where I'm having my problem. I want to use the same player from Shelter, not create a new player like I did in Shelter.
Church::Church
{
What should I do here?
}
I've tried a number of things, but I can't quite get it working. Eventually I want to be able to do something like this:
player->adjustHealth(-1); // Subtract one health from the player.
Only in Church, so that player's stats, like health, are adjusted no matter which location they are in.
I hope my question makes sense. If not, I can try to clarify better. Anyway, thanks in advance.
The problem I'm facing is with creating another class called Church, that is in a separate header file and acts as a separate location in the game. I want Church to use the same player that Shelter does, so it has all of the same stats, etc, rather than creating a new player in Church
This sounds like an ideal situation to use std::shared_ptr.
class Shelter
{
Shelter();
std::shared_ptr<Player> player; // Create a pointer to Player class.
// Provide a public accessor
public:
std::shared_Ptr<Player> getPlayer() const { return player; }
};
and
class Church
{
Church(std::shared_ptr<Player> pl) : player(pl) {}
std::shared_ptr<Player> player;
// You haven't explained in your post why you need this.
// Maybe you don't need it.
// Shelter** shelter;
};

Stack array of structures

I'm trying to figure our how implement an array of structures into stacks in this machine problem that i have. Here is the problem:
Make a program that will allow the user to perform the following activities stated below through array approach. Car Park Station offers 10 park lanes and each lanes can accommodate 5 cars.
a. Entry. It must be able to register new car and assign an available parking lane. Registration requires plate number, brand name, car color, owner and telephone number.
b. Release. It must be able to release car from an identified lane
c. Search. It must be able to identify the car location based on plate number.
d. Vacancy. It must be able to display available parking space.
From what i understand, i have to make a 2 dimensional array of structures containing various data types and i have to make a lifo code out of it. But my problem is, i know how to simulate an array approach lifo code with just a single data type but not with multiple data types. The part where i have to implement the 2 dimensional array of structure into a stack is where i got me stuck and confused. I've searched online to look for problems similar to mine but i couldn't find any. I tried to make a code shown below:
struct Car
{
string brand, color, owner;
int plate, phone;
};
class Stack
{
private:
Car * pointer = new Car[10][5];
int top;
public:
Stack() // Constructor
{...}
void Push(int plate, string brand, string color, string color, string owner, int number)
{...}
Pop()
{...}
}
Try to analyze your problem further before choosing an actual implementation solution (like 2-dim array).
In your task you have the following abstract entities:
CarParkStation with attributes: NumberOfParkLanes, which should be able to do something like: FindFreeLane, FindCar(plateNum), RegisterACar(car), ReleaseACar(car)
Lane with attributes: NumberOfPlaces, and with actions like: IsFull(), and maybe also: FindACar(plateNum), AddACar(car), RemoveACar(car)
Car with attributes: PlateNumber, Brand, Color, Owner, which should be able to do: GetPlateNumber(), and maybe also provide access to other parameters
Below is a very general example of possible CarParkStation class declaration:
class CarParkStation
{
public:
CarParkStation() = delete;
explicit CarParkStation(int numParkLanes);
~CarParkStation() = default;
std::shared_ptr<const Lane> FindFreeLane();
std::shared_ptr<const Car> FindCar(const PlateNumber& pNumber);
std::pair<std::shared_ptr<const Lane>, int position> FindWhereMyCarIsParked(const PlateNumber& pNumber);
bool RegisterACar(const Car& car);
bool ReleaseACar(const Car& car);
private:
std::vector<std::shared_ptr<const Lane> > lanes_;
std::set<std::shared_ptr<Car>, CustomCompareFunction > cars_;
};
Don't forget to write the tests along with your implementation. This will help you to verify your requirements and overall design. For example:
TEST_F(CarParkStationShould, returnCorrectFreeLane)
{
ASSERT_EQ(expectedFreeLane, carParkStation->FindFreeLane());
}
In general, focus on what should your application do first, and then think about how to implement it.

storing struct info in a vector (text rpg game)

I'm currently writing a text-based RPG for the Game Programming Institute course. I currently have a store in which the character can purchase armor, but now I need to add items to that. I have created a items class, but am having difficulty understanding how I can relate the store class, items class and player class together using a vector array.
I think what I should be doing is create a Item vector in the player class that holds information about the struct Items. Then when the player enters the store, the purchased items are entered into that vector.
I have included, what I feel are, relevant bits of my code. If anyone could give me some pointers or tips just to clear up my brain fog on understanding how to share information across these 3 classes that would be greatly appreciated.
Thanks!
Item.h
struct Item{
int itemType;
int goldValue;
};
In player.h class I have added the private data:
std::vector<Item> mItem;
In player.cpp I'm attemping to display the items with the function
void Player::purchaseItem(Item& mItems, int itemType, int itemValue)
Here is store.h
class Store{
public:
void enterStore(Player& player);
private:
Armor mStoreArmor;
int mCost;
};
A vector is designed to be a faster dynamically-allocated array.
I would think you'd want to use a hash or just a static array, with either keys or just indices, storing the quantity of each type of item.
you would obviously store possessions in a player class, and have the store class have a list of certain items (possibly with keys for what is in stock - this is why a hash would be a good idea).
When the player sees the store items and chooses one, the quantity in the player class's hash would be incremented.

Linked List in the same Linked List

I am new at C++, and I'm dealing with the Linked List. However, I'm confused that, while I'm writing a Linked List class. I'm trying to write a code for soccer league. I'm trying to hold each team in a linked list and since each team has players I would like to store them also in a linked list. So the linked list class should have a member to point a linked list which is the same property with the class I'm writing. Can it be possible?
Yes. A node of the teams list would contain a list of players. If you can't use std::list:
struct Player
{
Player* nextPlayer;
};
struct Team
{
Team* nextTeam;
Player* firstPlayer;
};
I'm pretty sure though that a list is not the optimal structure to model this.
I would prefer the following structure as you may not want to use players and teams always in a list:
struct PlayerNode
{
Player* item;
PlayerNode* nextNode;
};
struct TeamNode
{
Team* item;
TeamNode* nextNode;
};
struct Team {
// ...
PlayerNode* firstPlayer;
};
This allows you to use a team or a player as a standalone object (not connected to other teams/players).
But within a team you can also access its players, which I guess is desirable.
If you are using C++ then use STL instead. I am explaining one example with vector
Suppose this is the heirarchy
You have 2 League League1, League2
each have 2 teams Team1, Team2
each team has 10 players P1, P2
All the players of one team can be stored in a vector named temp_team
All temp_team can be pushed to vector named temp_league
All temp_league vector can be pushed to vector named footBall, League, watever.