如何在字符串中为枚举字符串交换字符?

时间:2014-05-11 00:41:36

标签: c enumeration pointer-to-pointer

表示Sokoban级别的字符串:-----#####-----------|-----#@$.#-----------|-----#####-----------
执行函数init_game(LEVEL *level)后,结构game应如下所示:

GAME game = {
.x = 6,
.y = 1,
.width = 21,
.height = 3,
.steps = 0,
.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},
},
.level_name = "chicago"
}

我已经为xywidthheightsteps解决了这个问题。 map仍然存在

**/*............program begins here.......*/**

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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

typedef struct level {  //level parsed from file
    char *name;
    char *description;
    char *password;
    char *raw_map;
    struct level *next;
    //char *solution;
} LEVEL;

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

GAME *init_game(LEVEL *level);

int main(){
    LEVEL level;
    level.name="chacago";
    level.password="addie";
    level.description="story beggins here";
    level.raw_map="-----#####-----------|-----#@$.#-----------|-----#####-----------";  //this string should be exchanged for pointers o pointers to enumeration string

    GAME *game;
    game=init_game(&level);
    return 0;
}

GAME *init_game(LEVEL *level){
    GAME *game=malloc(sizeof(GAME));
    int row=0,col=0;
    int idx=0;
    int x=0;
    int y=0;
    int width=0;
    int height=0;

    while (level->raw_map[idx]!='\0'){
        while (level->raw_map[idx]!='|'){
            if (level->raw_map[idx]=='@'){
                x=col;
                y=row;
            }
            if (level->raw_map[idx]=='\0')
                break;
            if (row==0)
                width++;
            idx++;
            col++;
        }
        idx++;
        col=0;
        row++;            
    }
    height=row;

    int steps=0;

    MAP_ITEM **map=(MAP_ITEM**)malloc((row+1)*(col+1));

    game->x=x;
    game->y=y;
    game->width=width;
    game->height=height;
    game->steps=steps;

    printf("\n");
    printf("position of player is: %d , %d\n",game->x,game->y);
    printf("width is: %d , height is: %d\n",game->width, game->height);
    return game;
}

2 个答案:

答案 0 :(得分:0)

分配并设置样本

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

int main(){
    MAP_ITEM **map;
    int height = 3;
    int width  = 21;
    int i;
    map = malloc(height * sizeof(MAP_ITEM*));
    for(i=0;i<height;++i)
        map[i] = malloc(width * sizeof(MAP_ITEM));
    char *raw_map="-----#####-----------|-----#@$.#-----------|-----#####-----------";
    int r, c;
    for(r=c=i=0;raw_map[i]!='\0';++i){
        switch(raw_map[i]){
        case '-':
        case '@':
            map[r][c++] = EMPTY;
            break;
        case '#':
            map[r][c++] = WALL;
            break;
        case '$':
            map[r][c++] = BOX;
            break;
        case '.':
            map[r][c++] = DESTINATION;
            break;
        case '|':
            ++r;
            c = 0;
            break;
        default:
            ;//error : Character data that does not assume the presence.
        }
    }
    if(map[1][7] == BOX)
        printf("map(1,7) is BOX\n");

    //deallocate map
    return 0;
}

答案 1 :(得分:0)

我把碎片放在一起,这是一个结果。我认为它能完成这项工作。如何解除分配map?它应该在主要功能结束时完成?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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

typedef struct level {
    char *name;
    char *description;
    char *password;
    char *raw_map;
    struct level *next;
    //char *solution;
} LEVEL;

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

GAME *init_game(LEVEL *level);

int main(){
    LEVEL level;
    level.name="chicago";
    level.password="addie";
    level.description="story begins here";
    level.raw_map="-----#####-----------|-----#@$.#-----------|-----#####-----------";

    GAME *game;
    game=init_game(&level);
    return 0;
}

GAME *init_game(LEVEL *level){
    GAME *game=malloc(sizeof(GAME));
    int row=0,col=0;
    int i=0;
    int x=0;
    int y=0;
    int width=0;
    int height=0;

    while (level->raw_map[i]!='\0'){
        while (level->raw_map[i]!='|'){
            if (level->raw_map[i]=='@'){
                x=col;
                y=row;
            }
            if (level->raw_map[i]=='\0')
                break;
            if (row==0)
                width++;
            i++;
            col++;
        }
        i++;
        col=0;
        row++;            
    }
    height=row;

    int steps=0;

    MAP_ITEM **map;
    game->map = malloc(height * sizeof(MAP_ITEM*));
    for(i=0;i<height;++i)
        game->map[i] = malloc(width * sizeof(MAP_ITEM));
    char *raw_map="-----#####-----------|-----#@$.#-----------|-----#####-----------";
    for(row=col=i=0;raw_map[i]!='\0';++i){
        switch(raw_map[i]){
        case '-':
            game->map[row][col++] = EMPTY;
            break;
        case '@'://DELIVERD ?,
            game->map[row][col++] = EMPTY;
            break;
        case '#':
            game->map[row][col++] = WALL;
            break;
        case '$':
            game->map[row][col++] = BOX;
            break;
        case '.':
            game->map[row][col++] = DESTINATION;
            break;
        case '*':
            game->map[row][col++] = DELIVERED;
            break;
        case '|':
            ++row;
            col = 0;
            break;
        default:
            ;//error : Character data that does not assume the presence.
        }
    }

    game->x=x;
    game->y=y;
    game->width=width;
    game->height=height;
    game->steps=steps;

    printf("\n");
    printf("position of player is: %d , %d\n",game->x,game->y);
    printf("width is: %d , height is: %d\n",game->width, game->height);

    if(game->map[1][8] == DESTINATION)
        printf("map(1,8) is DESTINATION\n");
    printf("item on [1,7] is: %d\n", game->map[1][7]);

    //deallocate map

    return game;
}