Player List

This is a discussion on Player List within the C++ Programming forums, part of the General Programming Boards category; Hello, struct chr { char *name; char *lname; int level; int bwis; int bint; int bstr; int bdex; int bagi; ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Player List

    Hello,

    struct chr {
    char *name;
    char *lname;
    int level;
    int bwis;
    int bint;
    int bstr;
    int bdex;
    int bagi;
    int wisc;
    int intc;
    int strc;
    int dexc;
    int agic;
    int gold;
    int silver;
    int ac;
    int hp;
    int maxhp;
    int mana;
    int maxmana;
    int move;
    int maxmove;
    int pos;
    char Race;
    char Class;
    int contents[50];
    int roomnum;
    };

    I have several chr files which I need to work with together. For example, I'd like to be able to hold like 3000 of these structs together.
    chr playerlist[3000]; works but I want to be able to delete chrs (shift them down when a user logs off for example).
    Thanks for the help,

    Drek

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    What about a linked list?

    Code:
    struct node
    {
        int some_data;
        struct node *next_node;
    };
    Now you can easily remove nodes by changing pointers. Don't forget to free memory!

  3. #3
    Unregistered
    Guest
    or how about another member variable to indicate if the player represented by the current struct is playing/active or not? then evaluate/manipulate the variable as necessary, leavving the array/files otherwise undisturbed and not requiring the player to reenter all the information next time they come to play.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    Wouldn't I still need an array like players[300] to hold the actual data with a linked list? And then the linked list would point to the actual elements which would be holding the data? Removing a player from the linked list would be easy but how would I take the player out of the array so that it wouldn't tie up the spot permanently?

    Drek
    Last edited by Drek; 01-25-2002 at 12:36 PM.

  5. #5
    Unregistered
    Guest
    a linked list is not the same as an array. A linked list relys on a struct with a pointer to the struct type embedded in the struct as a member variable. If you want to be able to remove the player from the group of players it is easier to do it with a linked list.

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    A structure is just a block of memory. In a structure you can have a pointer to another structure.

    Here's some example code. I haven't test it, but I thought it's correct.

    Code:
    /* Assume : */
    struct node
    {
        int data;
        struct node *next;
    };
    
    /* Now you could use this as */
    struct node *node1, *node2, *node3;
    
    /* That were pointers, now you need to allocate memory for them */
    
    node1 = (struct node *) malloc (sizeof (struct node));
    node2 = (struct node *) malloc (sizeof (struct node));
    node3 = (struct node *) malloc (sizeof (struct node));
    
    /* If one of the pointers has value NULL, then there is a memory problem */
    if (node1 == NULL || node2 == NULL || node3 == NULL)
        return ERRORVALUE;
    
    /* Let node1 point to node2 */
    node1->next = node2;
    
    /* Let node2 point to node3 */
    node2->next = node3;
    
    /* Now remove node2 from the list, so node1 points directly to node3 */
    node1->next = node2;
    
    /* Don't forget to free the memory! */
    free (node2);
    
    /* Ofcourse at the end you need to free all memory */
    free (node1);
    free (node3);
    In this example there were 3 nodes. But linked lists are usually used in situations where you need those nodes, but you don't know how many when beginning. So usually you let the program remember the address of the first node, i.e. having a pointer to the beginning of the list and then adding nodes.

    When walking through the list, you use a different pointer-variable, you should NEVER change the pointer which point to the start of the list, since then you will lose that information.
    Last edited by Shiro; 01-25-2002 at 01:47 PM.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    The only problem is how would I come up with names for the differnt nodes. Lets say 10 different players connect, I would need to create 10 different nodes containing struct chr. This would be very nice to implement but I do not know how I can keep creating different nodes ( "node1", "node2", "node3", etc.)
    How would I do this? Thank you guys very much for trying to help me, I'm learning so please dont get too angry with me

    Drek

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    In the node you can allocate some memory for an ID. Perhaps a string in which you store a player's name. It could look like something like this.

    Code:
    #include <stdlib.h> 
    #include <stdio.h> 
    
    struct node 
    { 
        /*data*/ 
        char player_name [MAX_STRING_LEN];
        struct node *nextnode; 
    }; 
    
    struct node *create_node (char *name)
    {
        struct node *temp;
        temp = (struct node *) malloc (sizeof (struct node));
        strcpy (temp->player_name, name);
        temp->nextnode = NULL;
        return temp;
    }
    
    
    int main() 
    {
        struct node *first, *list_ptr, *new;
        char name [MAX_STRING_LEN];
     
        puts( "how many nodes?" ); 
        int inp; 
        scanf( "%d", &inp ); 
    
        retrieve_name (name);
        first = create_node (name); 
        list_ptr = first;
    
        for( int i = 0; i < inp; i++ ) 
        { 
            retrieve_name (name);
            new = create_node (name);
            list_ptr->nextnode = new;
            list_ptr = new;
        } 
    
        /* process the data and finally free memory */
      
        return 0;
    }
    Hmmm. Above looks a lot like C, though we're on the C++ board. :-)
    Last edited by Shiro; 01-25-2002 at 02:24 PM.

  9. #9
    Unregistered
    Guest
    That's the beuty of it. You only have to remember the name of the pointer to the first node. The other nodes don't have a name, they are identified by their address, which is stored in the node just before a given node. You don't even need to know the address(es), the program/compiler/computer does that for you!

    When you search for a node you search for an inditifying value in one of the member variables of the struct, or by looking for a NULL value in the pointer variable (indicating the last node in the list). There is a pretty good tutorial on lists available from the home page of this site.

    YOu should be aware that the new operator and the delete command have replaced the C version of malloc and free, although the latter still work.

    Also in the example list posted to delete node2 from the list you would do this:

    node1->next = node3;
    free node2; //or delete node2;

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    And that code would create a new node, not just a pointer, correct? If so,

    THANK YOU =)

    I'm sure I'll have some more problems in the future, so until then...

    Drek

  11. #11
    Unregistered
    Guest
    //example node
    struct node
    {
    int id;
    int number;
    char name[50];
    node * next;
    }

    //to create a new node to add to the list do this:
    node *newNode = new node;
    //then you can default the member data like this
    newNode->id = 0;
    newNode->number = 0;
    strcpy(newNode->name, "");
    newNode->next = NULL;

    //now add whatever real data you want for the id, number, name and use whatever protocol you want to add/insert the new node to the list. For many more details you really should read the tutorial however.

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    I got it from here, many thanks everyone.

    Drek

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21