Thread: Please help (urgent)

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    2

    Please help (urgent)

    I am confused in general by this and would really appreciate some help.

    Create a struct called enemy which will contain information such as enemy health and damage, etc. (The user must be able to choose how many enemy's he wants)

    This is what I had in mind

    Code:
     struct enemy {
    int health; 
    int damage; 
    };
    struct enemy enemy1;
    Create a struct that will hold two lists.
    1. a list representing the enemy's killed.
    2. a list representing the enemy's that are still alive.

    Assuming enemy1 is alive how do I add it to the still alive list??

  2. #2
    Registered User
    Join Date
    Feb 2015
    Posts
    33
    Maybe you could have a dead list instead so all enemies are always in use until in that list. So every time the user attacks an enemy it goes through something like:

    enemy1.health -= 'usersattack'
    if(enemy1.health <= 0)
    'add enemy1 to dead list'

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    The description uses list, not array, so I would use
    Code:
    struct enemy {
        struct enemy *next;
        int health; 
        int damage; 
    };
    
    struct enemy *alive = NULL; /* No alive enemies yet */
    struct enemy *dead = NULL; /* No dead enemies yet */
    Because the enemies are in a singly-linked list, when damaging an enemy kills it, we also need a pointer to the list pointer to be able to kill the enemy. For example, if we have:
    Code:
    void kill_enemy(struct enemy **alive_list, struct enemy **dead_list, struct enemy *target)
    {
        /* If no target, do nothing. */
        if (target == NULL)
            return;
    
        if (alive_list != NULL) {
            /* Find target in alive list, and detach it. */
    
            if (*alive_list == target) {
                *alive_list = target->next;
                target->next = NULL;
    
            } else {
                struct target *prev = *alive_list;
    
                while (prev->next != target && prev->next != NULL)
                    prev = prev->next;
    
                if (prev_next) {
                    prev->next = target->next;
                    target->next = NULL;
                }
            }
        }
    
        if (dead_list) {
            /* Prepend target to dead list. Most recently deceased first. */
            target->next = *dead_list;
            *dead_list = target;
        }
    }
    then we could kill a known target enemy foo using
    Code:
        kill_enemy(&alive, &dead, foo);
    assuming the global lists are used in the first snippet.

    The reason I didn't use the global lists directly in kill_enemy() is that this way you can have multiple alive and dead lists in a single program. It's not like providing a pointer to the list pointer is that annoying.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nominal Animal
    The description uses list, not array
    Good point, though I would recommend clarification of the requirements rather than immediately going for a linked list. A list is not necessarily a linked list, e.g., an array can also be used to implement a list.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by laserlight View Post
    Good point, though I would recommend clarification of the requirements rather than immediately going for a linked list.
    You're right.

    I guess I got excited thinking how this would be a pretty good way to exercise pointers and linked lists, especially when you are going to move enemies from one list to another. Maybe adding additional lists like bystander or turned_to_friendly further on

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent help on ls- ld
    By vaibhavs17 in forum Tech Board
    Replies: 10
    Last Post: 05-18-2009, 01:50 PM
  2. urgent please help!!
    By ali_1234 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 12:38 PM
  3. Urgent!! Need help
    By nareik9394 in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 02:11 PM
  4. Help!!! Urgent
    By ashesh in forum C Programming
    Replies: 5
    Last Post: 03-20-2003, 06:19 AM
  5. in need of urgent HELP!!!!!!!!!
    By Kaz in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2002, 07:23 AM