动态链接列表RPG库存程序 - 帮助(如何链接到类?)

时间:2015-04-17 00:04:21

标签: c++ class dynamic linked-list inventory

我使用两个类创建基于类的库存系统:一个用于字符,一个用于项目。

  

Ryan Newell编码 - NEW14136796 Uclan学生   (需要把它放在如此庸俗的监视器中,因为复制我自己的作品而不能标记我)

这是我到目前为止所得到的。

    ///Code by Ryan Newell - NEW14136796 Runshaw College - Uclan
#include<iostream> //Base C++ required #
#include<string> //Allows input of strings
#include<iomanip>  //Base C++ required #
#include <conio.h> //Getchar error workaround
#include <cstdlib>  // Provides EXIT_SUCCESS
#include <fstream> // Allows output of txt files
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <utility>
#include <Windows.h>//ConsoleSleep

using namespace std;

class itemspell{
    string itemname; //Name ((( what if items are the same name ? make difficult for finding statements ? maybe more char with same name item ?
    string itemtype; // Spell/Weapon/Armour/Magic Misc?
    string itemact; // What type of action does this item do ?
    string itemdesc; // Some random ingame description
    itemspell* inext; //The next pointer location


    bool isspell;   //Is Spell Boolean
    bool isweapon;  // Weapon
    bool isarmour; // So on so forth
    bool ismisc; // Misc
    bool offensiveitem; //Offensive item
    bool defensiveitem; // defensive item
    bool passiveitem; // Passive effect

    int damage;
    int armour;
    int magicbonus;
    int magicresistance;
    int cost;


    void item_spellsearch(itemspell* ihead, itemspell* &ipast, itemspell* &icurrent)
    {
        string search;
        if (ihead==NULL)
        {
            cout<<"There are no items on the list to search"<<endl;
        }
        else
        {
            cout<<"Enter the type item you are searching for :"<<endl;
            getline(cin,search);
            icurrent=ihead; //current pointer goes to -> header
            cout<<icurrent<<" "<<search<<" "<<icurrent->itemtype<<endl; 
            while(icurrent!=NULL && search>icurrent->itemname)
            {
                cout<<"Item Type:"<<icurrent->itemtype<<" "<<cout<<icurrent->itemname<<endl;
                ipast=icurrent;  //Past = new current pointer
                icurrent=icurrent->inext; //current pointer = next pointer

            }
        }
    }
public:
    itemspell()//Building a new item // -- Cannot put byref / by values in here remember, needs to be default constructor for some reason ??
    {
        cout<<"Enter Item Type: "<<endl;
        getline(cin,itemtype);
        cout<<"Enter Item Name:  "<<endl;
        getline(cin,itemname);
        cout<<"Enter Item Description: "<<endl;
        getline(cin,itemdesc);
        cout<<"Enter Item Action :"<<endl;
        getline(cin,itemact);
    }

    ~itemspell()//Delete record output
    {
        cout<<"Item being deleted"<<endl;
        Sleep(500);
        cout<<"Item deleted"<<endl;
        getch();
    }


        void additemspell(itemspell* &ihead) // Add record code
        {
            itemspell *icurrent;
            itemspell *ipast;
            itemspell *newitem;

            newitem = new itemspell;  // New itemspell
            if (ihead == NULL || newitem->itemname<ihead->itemname) //If no header or itemname is null;
            {
                newitem->inext = ihead;
                ihead = newitem;
            }
            else
            {
                icurrent= ihead;
                while(icurrent !=NULL&&icurrent->itemname<newitem->itemname) // While the current char Does not equal NULL/"Nothing and charcurrent itemname = newitemitemname"
                {
                    ipast=icurrent;
                    icurrent=icurrent->inext;
                }
                newitem->inext=icurrent; // Sets the next char to current and the past char to next to add in new record.
                ipast->inext=newitem;
            }
        }

        void deleteitemspell(itemspell* &ihead) /// Getting the address of the itemspell not the itemspell itself
        {
            itemspell *ipast=NULL, *icurrent=NULL;
            item_spellsearch(ihead, ipast, icurrent);
            if(ihead!=NULL) 

            if(icurrent==NULL)
                {
                    cout<<"ERROR: No itemspell Found"<<endl;
                    Sleep(500);
                    cout<<"returning to menu... press enter"<<endl;
                    getch();
                }
            else
            {
                if (icurrent == ihead)
                {
                    ihead = ihead->inext;
                }
                else
                {
                    ipast->inext=icurrent->inext; //Resets the pointer and header's back to previous positions....

                    delete icurrent;// Calls the deletion of the itemspell entry.
                    cout<<"itemspell found was deleted press enter to confirm"<<endl;
                    getch();
                }
            }
        }


class character
{
    string forename;
    string surname;
    string nickname;
    string race;
    string alignment;
    string alignment_type;
    string character_class;
    int Strength; //Strength
    int Intelligence; //Magick 2
    int Willpower; //Magick 1
    int Endurance; //Health
    int Agility; //Agility
    int StartingWealth; //StartingWealth       
    character* char_itemp; // Character - Item pointer - Adress location of the characters items
    character* char_next; // Next Pointer Value- Points to the location of the next person in the Dynamic Linked List

我尝试做的是当我创建一个新项目时,我希望能够将该项目提供给要使用的角色。

我知道我需要在字符类中创建指针值以指向itemspell类,但我不确定该做什么,可以做一些帮助。

基本上,当项类中的构造函数调用item类时,我想让字符类指向item类。

如果可以的话,请不要只提供代码。相反,请强调我应该更改的区域并提供建议示例。我的学位课程对抄袭很严格。


伪代码

When newitemspell created 
get input charactername ()
: 
if no current itemspell created for that character

  add item to 
 character->charactername
else      
 add item to next pointer character->charactername      

1 个答案:

答案 0 :(得分:1)

通过查看您的代码并查看您尝试完成的内容的描述符,为什么不创建一个Inventory类来处理用户项?它应该能够处理你想要做的99%的任何事情。如果这个例子与您尝试做的事情类似,请告诉我。

说实话,我无法确定你在代码中尝试做什么。正如之前的用户所提到的,&#34;链接&#34;两个类很简单,只需要为基类提供所需类的变量即可。例如:

#include <vector> //I prefer to use vectors for just about any list of objects.
                  //It's just more convenient for me.
using std::vector;

class Inventory //A handler for all of your character's stuff.
{
private:
    vector<object_class> objs; //To actually store an array of whatever objects
                                //you need to.

//Just some example functions to make the class useful.
public:
   int MAX = -1; //The size limit for the inventory. This can be used as a useful
                 //statistic in the specific inventory, depending on how it's being
                 //used. My example shows the value at -1, which, at least for me,
                 //means that any maximum size checks being done won't bother it
                 //because I usually have a setting of -1 as being unlimited.

   getItem(int slot); //Return item
   removeItem(int slot) //Removing items
   addItem(object_class object) //Adding items
}

这是一个非常简单的编程。您需要做的就是创建类型为Inventory的变量。对object_class的任何引用只是您希望Inventory处理的任何对象的类类型的通用填充。当你使用衍生品时,这很可能是一个基类。

class Character //The character class with all kinds of stuff to needs to be handled
{
public:
   Inventory items; //A regular inventory bag for items.
   Inventory equip; //The items the character is currently using.
   Inventory spells; //An alternate use for Inventory to be used as a handler for
                     //spell lists and other cool stuff.
}

从这里你应该能够理解到底发生了什么。库存不是&#34;链接&#34;对于角色。 Character有三个类型为Inventory的变量,Inventory类本身将处理其向量中所有项目的添加,删除,排序和检索。

现在你可以让一切都像这样:

int main(int argc, char *argv[])
{
   Character player;
   Armor shield = new Armor(); //A dummy object
   player.items.addItem(shield); //Assuming the Inventory is setup to handle Armor items.
                                //This will add the shield to the player's inventory.
   player.items.MAX = 10; //Setting the maximum capacity of items to 10, obviously.
   Armor A = player.items.getItem(0); //returns the piece of armor in the first spot in the vector.
}

我通常会为这样的事情编写更多的模块化代码,例如将Inventory作为模板类,但我离题了。这只是为了向您展示如何将一个类对象附加到另一个类对象。

我希望这与你所寻找的一样远。如果没有,请尝试更具体地重写您的问题。