samedi 26 novembre 2016

Deleting a random number of enemies from a linked list

I am required to create a function that does the following:

Pick at most 4 random active enemies to kill: Randomly pick two from high priority active enemies and randomly pick two from normal active enemies. High priority enemies are the ones that have type of shielded fighter or tank or chooper.

First of, I have the following structs and enums

enum Region
{
A,
B,
C,
D,
};
enum Type{
    paver,
    fighter,
    shielded_fighter,
    tank,
    chooper,
};
const char *types[] = { "Paver", "Fighter", "SF","Tank","Chooper"};
struct enemy
{
    int ID;     //Each enemy has a unique ID (sequence number)
    int time_step; //The time step of each enemey
    Region region;  //Region of this enemy
    int Health; //Enemy health
    Type type;  //Paver,Fighter,SHielded fighter
    int reload_power;
    int fire_power;};
struct node
{
    enemy enemyy;
    node* next;
};

I current have a linked list which contains enemies data and should randomly pick a number from 1-4 that represents the number of enemies that should be killed if the number is 1, 1 high priority enemy is killed, if there is no priority enemies 1 normal one is killed same goes for 2, if there is 2 high priority enemies or more 2 are killed, if only 1 exists, he is killed along with a normal one, if none exists 2 normal ones are killed. the maximum number of high priority enemies killed is 2. one thing to note tho, is the deleted nodes are chosen randomly so if the number of high priority enemies is 5, and I must kill 2, these 2 are chosen randomly, they aren't chosen from the end nor the beginning.furthermore, killed enemies are deleted from the actual list and transferred to another one made for dead enemies.

my first idea, was to create 2 lists from the actual linked list I have. one for High priority targets and one for the normal ones,then I would simply randomly delete n number of them but I got lost with all the rands and if conditions.

I made the following functions successfully:

bool merge(node*&temp,node*&temp2){
    if (isEmptyLinked(temp) && isEmptyLinked(temp2))
    {
        return false;
    }
    else if (isEmptyLinked(temp) && !isEmptyLinked(temp2))
    {
        merge(temp2,temp);
    }


        node*temp3=temp2;
        while (temp3->next!=NULL)
        {
            temp3=temp->next;
        }
        temp3->next=temp2;
        return true;
}


  void destroy(node*&head){
        node*temp=head;
        if (head->next==NULL)
        {
        delete head;
        head=NULL;
        }
        else if (head==NULL)
        {
            cout<<"Empty list"<<endl;
        }
        else
        {

        while (head->next!=NULL)
        {
            head=head->next;
            delete temp;
            temp=head;

        }
        delete head;
        head=NULL;
        }
    }
bool deletewithindex(node*&head, int index){
    if (head==NULL || index>countList(head) || index<0)
    {
        return false;
    }
    else if (head->next==NULL || index==0)
    {
        delete head;
        head= NULL;
            return true;
    }
    else
    {
        node*pre=head;
        node*post=head->next;
        int c=1;
        while (c!=index && post!=NULL)
        {
            pre=pre->next;
            post=post->next;
            c++;
        }
        pre->next=post->next;
        delete post;
        post=pre->next;
    }
}
void DivideList(node*&head){
    node* high=NULL;
    node* normal = NULL;
    int counttotal=countList(head);
    while (head!=NULL)
    {
        enemy x;
        if (head->enemyy.type==fighter)
        {
            x.fire_power=head->enemyy.fire_power;
            x.Health=head->enemyy.Health;
            x.ID=head->enemyy.ID;
            x.region=head->enemyy.region;
            x.reload_power=head->enemyy.reload_power;
            x.time_step=head->enemyy.time_step;
            x.type=head->enemyy.type;
            insertatEnd(normal,x);
        }
        else
        {
            x.fire_power=head->enemyy.fire_power;
            x.Health=head->enemyy.Health;
            x.ID=head->enemyy.ID;
            x.region=head->enemyy.region;
            x.reload_power=head->enemyy.reload_power;
            x.time_step=head->enemyy.time_step;
            x.type=head->enemyy.type;
            insertatEnd(high,x);
        }
    }
    srand(time(NULL));
    int numberofdeleted;
    if (counttotal>4){
    numberofdeleted= rand()%4+1;
    }
    else
    {
    numberofdeleted= rand()%counttotal+1;
    }

    int counthigh=countList(high);
    int countnormal=countList(normal);
    if (numberofdeleted>4 && counthigh>=2)
    {
        for (int i = 0; i < 2; i++)
        {

        }
    }
}

I couldn't complete the last function as I couldn't wrap my head around it. Thanks for your time,




Aucun commentaire:

Enregistrer un commentaire