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
|
#include <iostream>
#include <string>
using namespace std; //ez read
struct Item
{
int id;
string descript;
int effect;
};const Item EMPTY = {NULL,"EMPTY",NULL};
class Player
{
private:
Item *bp;
int bpsize;
int health;
int damage;
public:
Player(Item & item)
:bpsize(1), health(100), damage(20)
{
bp = new Item[bpsize];
bp[0] = item;
}
Player()
:bpsize(1), health(100), damage(20)
{
bp = new Item[bpsize];
bp[0] = EMPTY;
}
~Player()
{
delete [] bp;
}
void disbp()
{
if(bp[0].id==NULL)
{
cout << "Empty";
}
else
{
for(int i = 0;i<bpsize;i++)
{
cout << i+1 << ".";
cout << bp[i].descript << " ";
}
}
}
void use(int num)
{
num--; //for the cout << i+1 at line 44
if(num>=bpsize || num < 0) //error check
{
cout << "No such item exists.\n";
}
else
{
if(bp[num].id==1) //potion effect adds to health
{
this->health += bp[num].effect;
cout << "Health increased to: " << health << endl;
}
else if(bp[num].id==2)
{
this->damage += bp[num].effect;
cout << "Damage increased to: " << damage << endl;
}
else {
cout << "No such item\n";
}
if(bpsize<=1) //dont resize, just empty
{
bp[0] = EMPTY;
}
else
{
bpsize--; //resize
Item * newbackpack = new Item[bpsize]; //new backpack with new size
for(int i=num;i<bpsize;i++) {
newbackpack[i] = bp[i+1];
}
for(int i=0;i<num;i++) {
newbackpack[i] = bp[i];
}
delete [] bp;
bp = new Item[bpsize];
for(int i = 0;i<bpsize;i++)
{
bp[i] = newbackpack[i];
}
delete [] newbackpack;
}
}
}
void pickup(Item & item)
{
bpsize++;
Item * newbackpack = new Item[bpsize];
for(int i = 0;i<bpsize-1;i++) //old backpack size
{
newbackpack[i] = bp[i];
}
newbackpack[bpsize-1] = item;
delete [] bp; //destroy everything in here
bp = new Item[bpsize]; //reset with new size
for(int i = 0;i<bpsize;i++)
{
bp[i] = newbackpack[i];
}
delete [] newbackpack;
}
};
int main()
{
Item MPotion = {1,"Minor-Potion",15};
Item GPotion = {1,"Great-Potion",25};
Item Axe = {2,"Axe",10};
Item SStone = {2,"Sharp-Stone",5};
Item MaPotion = {1,"Major-Potion",35};
Player User(MPotion);
User.pickup(GPotion);
User.pickup(Axe);
User.pickup(SStone);
User.pickup(MaPotion);
User.disbp();
short int input = 0;
while(true)
{
cin >> input;
User.use(input);
cout << endl << endl;
User.disbp();
}
return 0;
}
|