I am trying to take an array of player mobs and an array of enemy mobs and turn them into a sorted linked list based on a data member within them. However I can't find any tutorials or suggestions on how to do this. All I can find on the internet is how to build a very basic linked list. If anyone could take a look at what I have put together, and offer either suggestions or point me to a tutorial that can tell me how to do this, that would be great. Thanks.
// Declarations //
class character{
public:
character(); // Constructor
booloperator== (character); // use when comparing init of objects
booloperator< (character);
booloperator> (character);
string getname (); // return name of object
int getinit () ; // return init of object
void set_init (int a); // set init of object to 'a'
void heal_full(); // max out hp and 0 nl dam
void heal(int amt); // modify hp by given amount
void heal_nl (int amt); // heal nl dam by given amount
void dam_nl (int amt); // give nl dam by given amount, checks for disabled or unconscious state.
int getHP(); // return current hp
int getnlDam(); // return nl dam
void SetnMob (character* nmob); // Sets next mob
character* GetnMob (); // Gets the address of next mob.
private:
string name; // name of character
int mHP; // max hit points
int HP; // current hit points
int nlDam; // amount of non lethal damage object currently has
int init; // initiative of character
character* nextMob; // points to next mob to iterate to
};
class encounter{
public:
encounter (character*, int); // Constructor
void listFull ();
private:
int ENEMIES; // number of enemies in this encounter
vector<character> enemy; // vector of all enemies in encounter
vector<character>* holding;
character* player;
character* nMob;
};
// Constructor for Encounter // (sets up the linked list for later iteration) sorting is based on init, high to low.
encounter::encounter (character players_in[], int size ){
banner ("NEW ENCOUNTER", "*");
nMob = NULL;
player = players_in;
cout << "How many enemies?";
cin >> ENEMIES;
for (int i = 0; i < ENEMIES; i++ ){
enemy.push_back(character () );
};
cout << "Roll for initiative and input for each person or enemy:";
for ( int i = 0; i < size; i++){ // get players init
int in;
cout << player[i].getname() << ": ";
cin >> in;
player[i].set_init(in);
};
for ( int i = 0; i < ENEMIES; i++) { // get enemies init
int in;
cout << enemy[i].getname() << ": ";
cin >> in;
enemy[i].set_init(in);
};
//////////////////////////////
// How should I create and sort the linked list?
// My thought is to sort each array individually and then iterate through both lists
// and combine them. My issue is that I am not sure how to do that.
//////////////////////////////
// sort players by init w/ linked list
for (int i = 0; i < size; i++) {
if (player[i].getinit() > high.getinit() ) {
}
elseif (player[i].getinit() == high.getinit() ) {
}
elseif (player[i].getinit() < high.getinit() ) {
}
}
// sort enemies by init w/ linked list
for (int i = 0; i < ENEMIES; i++) {
if (enemy[i].getinit() > high.getinit() ) {
}
elseif (enemy[i].getinit() == high.getinit() ) {
}
elseif (enemy[i].getinit() < high.getinit() ) {
}
}
//sort all mobs w/ linked list
for ( int i = 0, int j =0; i < size && j < ENEMIES ; ) {
if ( player[i] > enemy[j] ) {
player[i].SetnMob( enemy[j] );
i++;
}
elseif ( player[i] > enemy[j] ) {
enemy[j].SetnMob( player[i] );
j++;
}
elseif ( player[i] == enemy[j] ) {
bool a = false;
while (a == false) {
cout<< player[i].getname() << " and " << enemy[j].getname()
<< "are tied. Please roll again and select higher init."
<< "\n1. " << player[i].getname()
<< "\n2. " << enemy[j].getname();
int select;
cin >> select;
if (select == 1) {
player[i].SetnMob( enemy[j] );
i++;
a = true;
}
elseif (select == 2) {
enemy[j].SetnMob( player[i] );
j++;
a = true;
}
else {
cout << "Invalid selection, Please try again";
}
}
}
}
}