#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Little help with sokoban


    Hi, we must load levels to sokoban from file and it didnt work. Here is my code what I have:

    typedef struct Level {
    char *name;
    char *description;
    char *password;
    char *map;
    struct Level *next;
    char *solution;
    } LEVEL;


    LEVEL* parse_level(char *string) {

    char level_name[50];
    char level_password[50];
    char level_descrition[100];
    char level_map[200];

    int i = 0;
    int j = 0;
    while (string[i] != ';') {
    level_name[j] = string[i];
    i++;
    j++;
    }
    j = 0;
    while (string[i] != ';') {
    level_password[j] = string[i];
    i++;
    j++;
    }
    j = 0;
    while (string[i] != ';') {
    level_descrition[j] = string[i];
    i++;
    j++;
    }
    j = 0;
    while (string[i] != '\n') {
    level_map[j] = string[i];
    i++;
    j++;
    }
    j = 0;

    LEVEL *current;
    current = (LEVEL *) malloc(sizeof (LEVEL));
    if (current != NULL) {
    strncpy(current->name, level_name, strlen(level_name) + 1);
    strncpy(current->description, level_descrition, strlen(level_descrition) + 1);
    strncpy(current->password, level_password, strlen(level_password) + 1);
    strncpy(current->map, level_map, strlen(level_map) + 1);

    current->next = NULL;
    }
    return (current);
    printw(current->map);
    refresh();
    }

    LEVEL * load_levels(char * path)
    {
    // docasne udaje
    LEVEL *first, *last, *new;
    // nacitaj subor
    FILE * f = fopen(path, "r");
    // ak subor neexistuje tak ukonci
    if(!f)
    return NULL;
    // nacitaj riadok
    int i = 0,j;
    // sem citam retazce
    char t[128][512];
    // citaj retazce
    while(fgets(t[i], 512, f) != NULL)
    i++;

    // rozparsuj level
    first = NULL;
    first= parse_level(t[0]);
    first->next = NULL;
    last = first;
    //pokial som nacital level nacitaj dalsi
    for(j = 1; j < i; j++)
    {
    new = parse_level(t[j]);
    if(new != NULL)
    {
    new->next = NULL;
    //last->next = new;
    last = new;
    }
    else
    return NULL;
    }

    // zatvor subor
    fclose(f);

    // nacital som levely
    return (first);
    }

    AND this is how file looks:
    chicago;addie;story begins here;-----#####-----------|-----#@$.#-----------|-----#####-----------
    houston;harvey;we got a problem;-----#####-----------|-----#---#-----------|-----#$--#-----------|---###--$##----------|---#--$-$-#----------|-###-#-##-#---######-|-#---#-##-#####--..#-|-#-$--$----------..#-|-#####-###-#@##--..#-|-----#-----#########-|-----#######---------
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481

    Scream


    current = (LEVEL *) malloc(sizeof (LEVEL));

    current points to a region with unspecified memory content. Followed by

    strncpy(current->name,more,args);

    current->name == (*current).name

    which is unspecified.


    The easiest solution---that is, after you terminate your strings with nul byte---change struct level to

    typedef struct Level {
    char name[50];
    char description[50];
    char password[100];
    char map[200];
    struct Level *next;
    char *solution;
    } LEVEL;

    The next easiest solution, in my opinion, and again, this is after you terminate strings like level_descrition with a 0 byte, use strdup.

    current->name=strdup(level_name);
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    Please learn to use code tags "[ code ] [ /code ]". Minis the white space inside the square brackets. Just paste your original code in between them, don't past the code from the original message, it's already lost all formatting.
    I no longer wish to be associated with this site.

IMN logo majestic logo threadwatch logo seochat tools logo