I'm sorry about the confusion, I'm still trying to set all the room_id, description, mob_t, puzzle_t to either null or false. Sorry, but ty for the replies here is what I have:
this is level.h
Code:
#ifndef LEVEL_H
#define LEVEL_H
#include "monsters.h"
#include "commands.h"
#include <stdbool.h>
typedef enum {
NORTH,
SOUTH,
EAST,
WEST,
UP,
DOWN,
NUM_DIRECTIONS
} direction;
// maps direction enums to human readable direction strings
extern char *direction_names[];
// flips a direction enum (hint: enums start from 0 and increase)
#define flip_direction(original) (original ^ 1)
/* We need this typedef because exit_t contains a room_t*,
* but the compiler hasn't seen room_t yet. This works
* because pointers are all the same size, and the
* compiler doesn't need to know what *kind* of pointer a
* room_t* is. */
typedef struct room room_t;
// a struct representing an exit
typedef struct {
room_t *dest;
bool locked;
} exit_t;
// don't worry about the below struct
// the two functions which bring a puzzle to life!
typedef struct {
// prints a description of the puzzle
cmd_fxn_t description_fxn;
// lets the player attempt to solve the puzzle
cmd_fxn_t interact_fxn;
} puzzle_t;
// a struct containing all the information which represents a room
struct room {
int room_id;
char *description;
mob_t mob;
exit_t exits[NUM_DIRECTIONS];
puzzle_t puzzle;
};
// loads a level config file and returns a pointer to the starting room
// (FINISH IMPLEMENTING THIS FUNCTION IN level.c)
room_t *load_level(char *filename);
#endif
and this is what i have worked on
this is "level.c"
Code:
#include "level.h"
#include "common.h"
#include "util.h"
// array of directio names. must be consistent with the order of the enum
// in level.h
char *direction_names[] = {
"north",
"south",
"east",
"west",
"up",
"down"
};
// loads a level from a config file and returns a pointer to the starting room
room_t *load_level(char *filename) {
char buf[2048];
char *whitespace = " \t\n";
FILE *levelfile = fopen(filename,"r");
if(levelfile == NULL) {
printf("Could not open %s\n", filename);
exit(1);
}
skip_characters(levelfile, whitespace);
// get the total number of rooms
fgets(buf,256,levelfile);
num_rooms = atoi(buf);
skip_characters(levelfile, whitespace);
// allocate an array for all of the room structs to be stored
// store the pointer in the global room_array variable
/*** YOUR CODE HERE ***/
room_array = (room_t *)malloc(num_rooms * sizeof(room_t));
// Initialize room_array
/*** YOUR CODE HERE ***/
for (int i = 0; i < num_rooms; i++)
{
room_array[i].room_id = i;
room_array[i].description = NULL;
room_array[i].exits = (exit_t *)NULL;
room_array[i].mob = (mob_t *)NULL;
}
skip_characters(levelfile, whitespace);
// one line at a time, read in room description strings and set
// the appropriate field in each string's corresponding room struct
while(fgets(buf, 256, levelfile), buf[0] != '\n') {
/*** YOUR CODE HERE ***/
}
skip_characters(levelfile, whitespace);
// hook up rooms so that exits point to each other appropriately.
// exits should also be set to locked or unlocked as necessary
while(fgets(buf, 256, levelfile), buf[0] != '\n' && !feof(levelfile)) {
char *words[32];
tokenizer(buf, words, " \t\n");
assert(!strcmp("can-go", words[0]) || !strcmp("cant-go", words[0]));
direction dir;
switch(words[1][0]) {
case 'n':
dir = NORTH;
break;
case 's':
dir = SOUTH;
break;
case 'e':
dir = EAST;
break;
case 'w':
dir = WEST;
break;
case 'u':
dir = UP;
break;
case 'd':
dir = DOWN;
break;
default:
printf("%s isn't a direction\n", words[1]);
assert(false);
}
/*** YOUR CODE HERE ***/
}
return room_array; // equivalent to a pointer to the first element of the array
}