将字符串转换为枚举并传入struct

时间:2014-04-28 10:54:02

标签: c function struct enums project

我正在做一个学校项目。我的目标是创建一个初始化游戏的功能。函数的输入值是一个结构。输出也是一种结构。但是,我在输出结构的最后部分遇到了问题。 在最后一部分中,我必须将raw_map字符串与字符进行比较,并从中创建一个由枚举组成的输出。代码:

输入结构

typedef struct level {
char* raw_map;      // original string representing the level map
char* name;         // level name
char* description;  // level description
char* password;     // level password
struct level *next; // pointer to the next level
} LEVEL;

输出结构

typedef struct game {
int x;      // player x position
int y;      // player y position
int width;  // map width
int height; // map height
int steps;  // number of steps player made
MAP_ITEM **map;  // game map
} GAME;

MAP_ITEM

typedef enum {
EMPTY,
WALL,
BOX,
DELIVERED,
DESTINATION
} MAP_ITEM;

以下raw_map字符串

level->raw_map="-----#####-----------|-----#@$.#-----------|-----#####-----------";

输出**地图字符串应该看起来像

.map = {
    {EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL, WALL, WALL, WALL, WALL, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY},
    {EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL, EMPTY, BOX, DESTINATION, WALL, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY},
    {EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, WALL, WALL, WALL, WALL, WALL, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY}
}
}

我的代码:

GAME* init_game(LEVEL* level){
GAME* init;
init=(LEVEL*)malloc(100*sizeof(LEVEL*));
int i,x=0,y=1;
char string[strlen(level->raw_map)+1];
char* rawMapa,*mapa[50][50];
strcpy(string,level->raw_map);
rawMapa=&string;
printf("%s\n",string);

//width & height
for(i=0;i<=strlen(string);i++){
    if(string[i]!='|')
        x++;
    else break;
}
init->width=x;
for(i=0;i<=strlen(string);i++)
    if(string[i]=='|')
        y++;
init->height=y;
y=0;
x=0;    

//player
i=0;
while(string[i]!='@'){
    if(string[i]!='|')
        x++;
    if(string[i]=='|'){
        y++;
        x=0;
    }
    i++;
}
init->x=x;
init->y=y;
init->steps=0;


/*!! PROBLEM HERE !!*/
//map item
init->map=(char**)malloc(100*sizeof(char**));
*init->map=&level->raw_map;
i=0;
while(level->raw_map!=EOF){
    if(*(level->raw_map+i)=='#'){
        *(init->map+i)=(MAP_ITEM*)malloc(10*sizeof(MAP_ITEM));
        *(init->map+i)=WALL;
    }
    if(*(level->raw_map+i)=='-'){
        *(init->map+i)=(MAP_ITEM*)malloc(10*sizeof(MAP_ITEM));
        *(init->map+i)=EMPTY;
    }
    if(*(level->raw_map+i)=='@'){
        *(init->map+i)=(MAP_ITEM*)malloc(10*sizeof(MAP_ITEM));
        *(init->map+i)=EMPTY;
    }
    if(*(level->raw_map+i)=='$'){
        *(init->map+i)=(MAP_ITEM*)malloc(10*sizeof(MAP_ITEM));
        *(init->map+i)=BOX;
    }
    if(*(level->raw_map+i)=='.'){
        *(init->map+i)=(MAP_ITEM*)malloc(10*sizeof(MAP_ITEM));
        *(init->map+i)=DESTINATION;
    }
    if(*(level->raw_map+i)=='+'){
        *(init->map+i)=(MAP_ITEM*)malloc(10*sizeof(MAP_ITEM));
        *(init->map+i)=DESTINATION;
    }
    if(*(level->raw_map+i)=='*'){
        *(init->map+i)=(MAP_ITEM*)malloc(10*sizeof(MAP_ITEM));
        *(init->map+i)=DELIVERED;
    }
    i++;
}


return init;
}

1 个答案:

答案 0 :(得分:0)

试试这个:

init->map = (MAP_ITEM**)malloc(init->height*sizeof(MAP_ITEM*));
for (i=0; i<init->height; i++)
{
    init->map[i] = (MAP_ITEM*)malloc(init->width*sizeof(MAP_ITEM));
    for (j=0; j<init->width; j++)
    {
        switch (level->raw_map[i*(init->width+1)+j])
        {
            case '-': init->map[i][j] = EMPTY      ; break;
            case '@': init->map[i][j] = EMPTY      ; break;
            case '#': init->map[i][j] = WALL       ; break;
            case '$': init->map[i][j] = BOX        ; break;
            case '.': init->map[i][j] = DESTINATION; break;
            case '+': init->map[i][j] = DESTINATION; break;
            case '*': init->map[i][j] = DELIVERED  ; break;
        }
    }
}