Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1.0 Overview Adventure game programs frequently use linked lists to keep up with the characters in a role playing game. This program will be a
1.0 Overview | ||||
Adventure game programs frequently use linked lists to keep up with the characters in a role playing game. This program will be a special implementation of a linked list structure designed for just this purpose. In this linked list there will be a node representing each character in the game. Within each "character" node there will be references to items that the player has in his/her posession. These references will be modified in later programming assignments. | ||||
2.0 Requirements | ||||
The student shall define, develop, document, prototype, test, and modify as required the software system. | ||||
2.1 This software system shall define a class, called CharacterList (file names shall be CharacterList.h andCharacterList.cpp) which maintains a linked list of instances of a player character class called Character. The Character class, which was created in Programming Assignment 1 will be modified for use in this program. | ||||
2.1.1 The CharacterList class shall contain private variables as described below: | ||||
2.1.1.1 A pointer, called m_pHead to a Character class instance which shall point to the first character in the list. | ||||
2.1.2 The CharacterList class shall contain public functions as described below: | ||||
2.1.2.1 CharacterList() and ~CharacterList()--default constructor and destructor. | ||||
2.1.2.2 bool addCharacter(Character *newCharacter)--this function takes a single argument, a pointer to a Character class object. It will then add this character to the linked list of character objects. The list shall be an ordered linked list sorted by the name of the character. It will return true if the player was successfully added to the list or false if it failed to add the player. | ||||
2.1.2.3 Character *removeCharacter(const char *characterName)--this function takes a character name as it's only argument. It shall locate the character in the list of characters and remove this character from the linked list of Character objects. It will return a pointer to the character if it was successfully removed from the list or NULL if the character was not found. | ||||
2.1.2.4 bool addItem(const char *characterName, Item *newItem)--this function takes a player name, and a pointer to an Item structure. It will then locate this character in the list and add this item to the character's list of carried items. It will return true if the item was successfully added to the player's array of items or false if the player's array of items is full. In this implementation the item will merely be added to a temporary array. The full implementation of the item list will be part of programming assignment 3. | ||||
2.1.2.5 Item *getItem(const char *characterName, char *itemName)--this function takes a player name, and the name of an item. It will then locate this character in the list and search for the named item in the character's list of carried items. It will return a pointer to the item if found or NULL if not found. If the character was not found in the list it should also return NULL. | ||||
2.1.2.6 Item *dropItem(const char *characterName, char *itemName)--this function takes a character name, and an Item name. It will then locate this character in the list and remove the item from the character's list of carried items if the item exists. It will return a pointer to the item or null if the item was not found in the player's list of items. In this implementation the item will merely be removed from a temporary array. The full implementation of the item list will be part of programming assignment 3. | ||||
2.1.2.7 void printCharacters()--this function will call the print function in each of the Character objects in the list to print all data stored in the list. | ||||
2.2 This software system shall modify the player character class created in programming assignment 1 in the following ways. | ||||
2.2.1 The Character class shall have added a private array of 10 data structures called m_Items of type Item. The Item data structure is defined below. Note: This will be modified in programming assignment 3. | ||||
2.2.2 The Character class shall have added a public pointer, called m_pNext capable of holding the address of a Character object so that a linked list of Character objects can be constructed. | ||||
2.2.3 The Character class shall contain additional public functions as described below: | ||||
2.2.3.1 bool addItem(Item *item) - Add an item to the player's list of carried items. Return true if this item was successfully added, or false if the player's array of items is full and the new item cannot be added. | ||||
2.2.3.2 Item *getItem(char *itemName) - Locate the named item in the player's array of items, and return a pointer to it or NULL if not found. | ||||
2.2.3.3 Item *dropItem(char *itemName) - Locate the named item in the player's array of items, and make a copy of it to be returned. Clear the fields of that item in the array then return the pointer to the duplicate of the item. Return NULL if the item was not found.. | ||||
2.2.3.4 void printAll() - The printAll() function shall be modified so that it prints all information on the character as required in programming assignment 1 (Name, Class, Alignment, Hit points, all 6 character trait values) and the list of items carried with their weight and value. | ||||
2.2.4 All of the "get" functions shall be modified so that they function in the traditional way, i.e. return the requested value instead of copying the value into a function argument that is either a pointer or a reference. The get function prototypes shall be changed to the following: char *getName(); int getClass(); int getAlignment(); int getHitPoints(); int getStrength(); int getDexterity(); int getConstitution(); int getIntelligence(); int getWisdom(); int getCharisma(); For this assignment you may let the getName() function just return the m_sName variable instead of creating a duplicate to be returned. | ||||
2.3 This software system shall define a structure called Item in a file called Item.h which contains all information to define an item which might be carried by a player character. | ||||
2.3.1 The Item structure shall contain variables as described below: | ||||
2.3.1.1 A character array, called m_sItemName capable of holding strings of up to 64 characters. | ||||
2.3.1.2 An int called m_iType used to indicate the type of item (Treasure, Weapon, etc.). | ||||
2.3.1.3 Two doubles called m_dWeight and m_dValue used to hold the weight (in pounds) and value (in gold pieces) of an item. | ||||
2.4 The two class files and their associated header files and the Item.h file must be capable of being compiled and linked in with a driver program (main() in a separate file) for testing. Do not turn in your source file containing main. It will not be used for testing. | ||||
3.0 Deliverables | ||||
These products shall be delivered electronically via e-mail as specified below to the instructor. 3.1 Sprint Report -- The student shall provide filled out Sprint Report form for instructor approval NLT (Not Later Than) Thursday, February 21. 3.2 Program source files -- The student shall provide fully tested copies of the .cpp and .h files. These files must be submitted to the instructor via e-mail. The files shall be delivered NLT Thursday, February 21. | ||||
4.0 Period of Performance | ||||
The period of performance of this assignment is 21 days from the date of assignment. No deliverables be accepted after midnight of the DDD date posted in the Course Syllabus. | ||||
Header File Defining the Item Structure //------------------------------------------------------------------ // Item.h // // Define the Item structure for use in programming assignment 2 // Author: Dr. Rick Coleman //------------------------------------------------------------------ #ifndef ITEM_H #define ITEM_H struct Item { char m_sItemName[65]; int m_iType; double m_dValue; double m_dWeight; }; #endif Code Snippits Use the following functions in your Character class to handle adding, getting, and dropping an item from the list of carried items. These functions will be redone in programming assignment 3.//----------------------------------------------- // Add an item to the list of items //----------------------------------------------- bool Character::addItem(Item *item) { // See if there is an empty place to hold this item for(int i=0; i<10; i++) { if(strcmp(m_Items[i].m_sItemName, "---") == 0) { // Found an empty slot so copy this item here m_Items[i] = *item; return true; } } // If we get here then the Items array is full and we // can't add this item so return false to flag failure. return false; } //----------------------------------------------- // Get a pointer to an item in the list //----------------------------------------------- Item *Character::getItem(const char *itemName) { for(int i=0; i<10; i++) { if(strcmp(m_Items[i].m_sItemName, itemName) == 0) return &m_Items[i]; } return NULL; // Didn't find } //----------------------------------------------- // Delete an item from the list of items. Return // pointer to item if the item was found or NULL // if it was not found in the list. //----------------------------------------------- Item *Character::dropItem(const char *itemName) { // Search all items for(int i=0; i<10; i++) { // If this it the correct item name if(strcmp(m_Items[i].m_sItemName, itemName) == 0) { Item *newItem = new Item(); *newItem = m_Items[i]; // Set the name of the item to the "Empty" string strcpy(m_Items[i].m_sItemName, "---"); return newItem; } } return NULL; }Add the following code in the constructors for Character to initialize all of the items in the m_Items array to empty. Note: You will have to remove this in Program 3. // Initialize array to all empty places for(int i=0; i<10; i++) { strcpy(m_Items[i].m_sItemName, "---"); }Note: You are still responsible for writing the addItem(), getItem(), and deleteItem() functions in the CharacterList class as these are adaptations of the linked list Insert, Search, and Delete algorithms. The functions should search the list of characters until a character is found whose name matches the playerName argument and then call the addItem(), getItem(), or dropItem() function in the Character object. Note that the CharacterList::addItem(), CharacterList::getItem(), and the CharacterList::dropItem() functions must return whatever is returned by their respective calls to Character::addItem(), Character::getItem(), and Character::deleteItem(). |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started