backpack.h #ifndef BACKPACK_H #define BACKPACK_H #include #include "item.h" #include "container.h" /** * Structure representing the backpack */ struct backpack { int capacity; /// backpack capacity...

The assignment is here in this google doc:https://docs.google.com/document/d/1QDcK3VDQqolymC9u6k8cRMRF1JWqbbHsvrMks9HmDS4/edit?usp=sharing


backpack.h #ifndef BACKPACK_H #define BACKPACK_H #include #include "item.h" #include "container.h" /** * Structure representing the backpack */ struct backpack { int capacity; /// backpack capacity (max nr. of items) int size; /// current size struct container* items; /// list of items }; /** * Creates backpack * @param capacity backpack capacity * @return Reference to created backpack. */ struct backpack* create_backpack(const int capacity); /** * Destroys backpack * @param backpack backpack to destroy * @return Always returns NULL. */ struct backpack* destroy_backpack(struct backpack* backpack); /** * Adds item to backpack * @param backpack * @param item * @return true, if item was added successfully, false otherwise (backpack is full) */ bool add_item_to_backpack(struct backpack* backpack, struct item* item); /** * Deletes given item from backpack * @param backpack backpack, from which the item will be deleted * @param item item to delete */ void delete_item_from_backpack(struct backpack* backpack, struct item* item); /** * Gets the item by name * @param bakcpack backpack to seaerch for item * @param name name of item to get * @return Reference to the item, if found, NULL otherwise. */ struct item* get_item_from_backpack(const struct backpack* backpack, char* name); #endif command.h #ifndef COMMAND_H #define COMMAND_H #include /** * Struct defining command * * Note that name and description are expected to be allocated on the heap, * and they should be freed on command destruction. */ struct command{ char* name; /// command name, must be unique char* description; /// command description regex_t preg; /// precompiled pattern buffer size_t nmatch; /// number of matches char** groups; /// matched groups }; /** * Creates command with name and description * * This function creates new command. Every command is defined with at least * it's name and description. For better recognition of command and it's * parameters you can specify search pattern as regular expression following with * the number of groups to match. If you specify the search pattern for your * command, you can easy specify several forms of typing the same command. * Function returns reference to the newly created command, or NULL, if command * could not be created. * @param name command name * @param description command description for usage with HELP command * @param pattern search pattern for regular expression * @param nmatch the number of groups which can be matched with regexp usage * @return The reference of the command in memory or NULL, if command could not be created or name or description were not provided. */ struct command* create_command(char* name, char* description, char* pattern, size_t nmatch); /** * Destroys command * * Destroys (frees) command and all of its resources. * @param command command to be destroyed * @return Always returns NULL. */ struct command* destroy_command(struct command* command); #endif container.h #ifndef CONTAINER_H #define CONTAINER_H #include #include "room.h" #include "item.h" #include "command.h" /** * Container type * * Enumeration type defining the type of container data. It can be one of four * types: ROOM, ITEM, COMMAND or TEXT. */ enum container_type { ROOM, ITEM, COMMAND, TEXT }; /** * Container structure * * Every list used in the game is of type of this structure. The union inside * represents the container data, which is pointer to room, item, command * or text. * * Note that the text is expected to be allocated on the heap, and it should be * freed on container destruction. */ struct container { enum container_type type; /// type of container union{ struct room* room; /// pointer to room struct item* item; /// pointer to item struct command* command; /// pointer to command char* text; /// pointer to text }; struct container* next; /// pointer to next data container in list }; /** * Creates and appends new container to list * * Creates new container, which will hold reference to room or item or command * or text. The created container is appended to the end of the list pointed at * by 'first'. Function returns reference to the newly created container. * @param first reference to the first container of list * @param type type of container * @param entry container data * @return Reference to the created container, or NULL, if entry is NULL or * entry type is not the same as the type of first container. */ struct container* create_container(struct container* first, enum container_type type, void* entry); /** * Destroys all containers in the list * * This function destroys all the containers in the list, which starts with the * container specified with the parameter first. Function returns value NULL as * the new reference to this list of containers. * @param first pointer to the first container of the list * @return Function always returns NULL as the pointer to this list. */ struct container* destroy_containers(struct container* first); /** * Returns container content based on it's name * * Function returns container content of specific type based on it's name. So it * returns command object based on the command name, or item object based on the * item name, or room object based on the room name, or text entry if such entry * exists. Search pattern is not case sensitive. If such object doesn't exist, * NULL is returned. * @param first pointer to the first container of the list * @param name search pattern * @return Reference to the found object or NULL, if the object was not found. */ void* get_from_container_by_name(struct container *first, const char *name); /** * Deletes container, which holds given entry leaving the entry intact * * This function destroys only one container in the list. The container is * identified based on the pointer to the entry it contains. Content of the * container stays unchanged! Function returns reference to the beginning of * this list. * @param first pointer to the first container of the list * @param entry container entry * @return Reference to the list of containers without given container. */ struct container* remove_container(struct container *first, void *entry); #endif game.h #ifndef GAME_H #define GAME_H #include "room.h" #include "command.h" #include "backpack.h" #include "parser.h" #include "container.h" /// Max. size of input buffer (line) #define INPUT_BUFFER_SIZE 100 /** * Available game states */ enum gamestate { GAMEOVER, // player died or typed command for quit PLAYING, // game is still playing SOLVED, // game has been solved RESTART // request for game restart was given }; /** * Game context structure */ struct game{ enum gamestate state; /// game state struct parser* parser; /// command line parser struct container* world; /// list of all rooms struct room* current_room; /// reference to the current room struct backpack* backpack; /// player's backpack }; /** * Starts game loop for the given game * * Main function of the game, which contains the game loop. So it's * responsible for reading the input from player, updating the state * of game and rendering the new situation. * @param game the game to be played */ void play_game(struct game* game); /** * Creates game * * Function creates game structure and returns it's reference. * But it will create not only the game structure, but also * initializes and creates all of it's members. * @return The created game object or NULL, if game could not be created. */ struct game* create_game(); /** * Destroys game * * Destroys the game structure. With the destroying the game function * destroys also all of it's members. This function should be called * as the last, when the game is going to quit. * Function always returns NULL references as the new reference to * the game structure. * @param game the game to be destroyed * @return Always returns NULL */ struct game* destroy_game(struct game* game); /** * Executes command in the game * * Function executes the command given as the function parameter * on the game object. It doesn't return any value, but updates the * state of the game. * This function can be considered as the main function, which * contains the game logic. * @param game the game * @param command the command to be executed */ void execute_command(struct game* game, struct command* command); #endif item.h #ifndef ITEM_H #define ITEM_H /** * Item properties */ enum properties{ NONE = 0, // no extra properties
May 05, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here