String representing the level in Sokoban:-----#####-----------|-----#@$.#-----------|-----#####-----------
After executing function init_game(LEVEL *level)
, structure game
should look like this:
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"
}
I have already solved this issue for x
,y
,width
,height
and steps
. map
is yet remaining
**/*............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;
}