Inventory system for a text based RPG

So I have been working on this as part of a school assignment that's worth almost the entire course (min. 14 credits to pass, there are a total of 24 I could get though). And I came across this problem with my battle system; every time I attack I deal about 7 million damage. This is when I draw the damage value from an item in the main hand weapon, add it to the off hand weapon and add a +1 for base damage.

There aren't too many comments, there wasn't much space left and it is pretty strait forward and logical but it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
void itemsmenu()
{
    for (int i = 1; i < itemcount; i++) //display all items in the bag
    {
        cout << " " << i << "] " << itemnames[playeritems[i][0]] << endl;
    }

    cout << endl;

    cin >> userinput; //ask the user to select an item
    itempicked = userinput;

    //display all info for item
    cout << "\nName: " << itemnames[playeritems[itempicked][0]] << endl; 

    switch (playeritems[itempicked][1])
    {
        case 1:
            cout << "Type: Helm\n";
            cout << "Defence: " << playeritems[itempicked][2] << endl;
            break;
        case 2:
        case 3:
            cout << "Type: Armour\n";
            cout << "Defence: " << playeritems[itempicked][2] << endl;
            break;
        case 4:
            cout << "Type: Boots\n";
            cout << "Defence: " << playeritems[itempicked][2] << endl;
            break;
        case 5:
            cout << "Type: Gloves\n";
            cout << "Defence: " << playeritems[itempicked][2] << endl;
            break;
        case 6:
            cout << "Type: Sheild\n";
            cout << "Defence: " << playeritems[itempicked][2] << endl;
            break;
        case 7:
            cout << "Type: Weapon\n";
            cout << "Damage: " << playeritems[itempicked][3] << endl;
            break;
        case 8:
            cout << "Type: Small Weapon\n";
            cout << "Defence: " << playeritems[itempicked][2] << endl;
            cout << "Damage:  " << playeritems[itempicked][3] << endl;
            break;
        case 9:
            cout << "Type: Potion\n";
            cout << "Health Restored: " << playeritems[itempicked][4] << endl;
        case 10:
            cout << "Type: Quest Item\n";
        default:
            cout << "Type: Miscelaneous Item\n";
    }

    cout << endl;

    if (playeritems[itempicked][1] <= 8)
    {
        cout << "Do you want to equip this item?\n";
        cout << " 1] Yes\n 2] No\n";

        cin >> userinput;

        cout << endl;

        if (userinput == 1)
        {
            if (playeritems[itempicked][1] < 8)
            {
                slotpicked = playeritems[itempicked][1];

                if (playerequipment[slotpicked][0] == 0)
                {
                       /*If the slot the item is being equiped to is empty then transfer the data normaly, removing the item from the inventory */
                }

                else if (playerequipment[slotpicked][0] > 0)
                {
                    /*Otherwise, temporarily store the previous item, equip the new item and then put the first item back into the inventory*/
                }
            }

            if (playeritems[itempicked][1] == 8) 
            {
                /*If the item is a knife type, ask if the player wants to put it in the main or off hand slots, then equip like before*/
            }
        }


    }
}

void battleinit()
{
    enemyhp = enemydata[enemy][1]; 
    battlerunning = true;
    turnstart();
}

void turnstart()
{
    while (battlerunning)
    {
        if (enemyhp < 1)
        {
            battlerunning = false;
            enemywin();
        }

        if (playerhp < 1)
        {
            battlerunning = false;
            enemywin();
        }

        cout << "\nYour Hp: " << playerhp;
        cout << endl;
        cout << enemynames[enemy] << "'s Hp: " << enemyhp;
        cout << "\n\n";
        cout << "What do you want to do?\n";
        cout << " 1] Attack\n 2] Defend\n 3] Use an item\n 4] Flee\n";

        cin >> userinput;

        cout << endl;

        switch (userinput)
        {
            case 1:
                defending = false;
                if (playerstats[4] >= enemydata[enemy][3])
                {
                    playerattack();
                    enemyattack();
                    turnstart();
                }
                else
                {
                    enemyattack();
                    playerattack();
                    turnstart();
                }
                break;
            case 2:
                defending = true;
                enemyattack();
                turnstart();
                break;
            case 3:
                defending = false;
                playeruseitem();
                enemyattack();
                turnstart();
                break;
            case 4:
                playerlocation = lastlocation;
                battlerunning = false;
                break;
        }
    }
}

void playerattack()
{
    while (battlerunning)
    {
        int playerdamage = playerequipment[6][3] + playerequipment[7][3] + 1; //this is where it stuffs up I think

        if ((playerdamage - enemydata[enemy][4]) < 1) //enemydata[][4] is defence
            enemyhp --;

        else
            enemyhp -= playerdamage;


        cout << "You strike for " << playerdamage << ", Enemy's armour absorbed " << enemydata[enemy][4] << endl;

        if (enemyhp < 1)
            playerwin();

        else
            cout << "";
    }
}

void playeruseitem()
{
    while (battlerunning)
    {
        /*Display items in inventory, prompt the player to select one, if it is a potion use it otherwise ask again (0 is to go to the turn start function)*/
    }
}

void enemyattack()
{
    while (battlerunning)
    {
        int playerdefence = playerequipment[1][2] + playerequipment[2][2] + playerequipment[3][2] + playerequipment[4][2] + playerequipment[5][2] + playerequipment[6][2];

        if (defending == true)
        {
            int enemydamage = enemydata[enemy][3];

            if (enemydamage - (playerdefence * 2) < 1)
            {
                enemydamage = 1;
                playerhp --;
            }

            else
                playerhp -= (enemydamage - playerdefence);

            cout << "The " << enemynames[enemy] << " strikes you for " << enemydamage << " damage!\n";
        }

        else
        {
            int enemydamage = enemydata[enemy][3] - playerdefence;

            if (enemydamage < 1)
            {
                enemydamage = 1;
                playerhp --;
            }

            else
                playerhp -= (enemydamage - playerdefence);

            cout << "The " << enemynames[enemy] << " strikes you for " << enemydamage << " damage!\n";
        }

        if (playerhp < 1)
            enemywin();

        else
            cout << "";
    }
}

void enemywin()
{
    cout << "You loose...\n\n";

    battlerunning = false;
    playerlocation = 0;

    main();
}

void playerwin()
{
    cout << "\nYou killed the " << enemynames[enemy] << "!\n\n";

    battlerunning = false;
    enemydefeated[playerlocation] = true;

    main();
}


I didn't put it all in there; just the inventory and battle system because it's about 2000 lines of code.
Yea; like I said it's worth half my course.

If you need any more info I can add to it if needed
Without a defintion of playeritems or playerequipment or a defintion of what the subscripts mean, it's difficult to tell what your code is doing.

A few observations:

1) If this class is supposed to be C++, I don't see any use of C++ in your program (except for cout).

2) At line 3, you're always going to miss an item in the bag. Consider what happens if itemcount is 1.

3) Lines 249 and 259: you should never call main.

4) If you're getting unreasonable damage values, you probably have uninitialized variables.
I have itemcout uinitialised as one so that it equips it in the first slot, also I have put different items in the inventory and all of them were displayed. (Picking up items is in a different function). Also I apoligise fot the lack of comments in the code.

As for not being C++, I'm not all that good at it and thats what I know of programming in general.

I removed the part where it calls main in the playerwin() and enemywin() functions, and now it doesn't crash.

For the damage problem; I'm not sure where that comes from; in all places where I assign or declare the damage it is always to playeritems[3]. That was why I posted it here.
There's a few things I see that is making your life hard right now.

1. You're using global variables instead of passing arguments to your function calls. This is a very dangerous practice in my opinion.

1
2
3
4
void somefunction (Player & cPlayer)
{

}


This is much, much safer than using global variables. Once you learn to pass by reference, you'll fall in love with it.

Don't want to allow the function to change the original values?

1
2
3
4
void somefunction (const Player & cPlayer)
{

}


2. I guarantee that you're doing 7million damage because you didn't initialize a variable properly. How do I know? Because it happened to me when I was working on my text based RPG. A couple values were in the -7394184 or whatever range, some quick debugging revealed to me that I didn't properly initialize some values (or rather, at all).

3. I don't know what compiler you use, but you should add a breakpoint when you initialize your player's damage, so you can check each variable individually by using the Debug -> Windows -> Watch window. You can type in a variable and it'll display its current value. You can also change the values while your program is running.
Last edited on
I changed a few things and now it works; can't remember what but it does now. Thanks to both of you, I will use your advice.
I rememer now; I was missing an inventory slot (put down 7 instead of the eight I had).
Topic archived. No new replies allowed.