-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBag.cpp
130 lines (114 loc) · 2.37 KB
/
Bag.cpp
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
#include "Bag.h"
using std::iterator;
using std::endl;
using std::cout;
/*Bag::Bag(map<Item, int> oneBag)
{
for (map<Item, int>::iterator current = oneBag.begin(); current!=oneBag.end(); current++)
{
string tempName = current->first();
if (Bag.find(tempName) != Bag.end())
{
}
this->Bag;
}
}
*/
/*
Bag::Bag(map<Item, int> oneBag)
{
}
*/
Bag::Bag()
{
used = 0;
}
size_t Bag::GetUsed()
{
return this->used;
}
bool Bag::IsFull()
{
return used >= MAX_BAG_CAPACITY;
}
bool Bag::UseItem(int itemNum, Hero &h, Monster &m)
{
if (itemNum<used)
{
if (this->bag[itemNum].Use(h,m))
{
if (bag[itemNum].GetCount() == 0)
{
this->RemoveFromBag(itemNum,0);
}
return true;
}
}
return false;
}
int Bag::FindIndex(Item item)
{
int index = 0;
for (int i = 0; (i < used)&&(bag[i].GetName() != item.GetName()&&(bag[i].GetName()!="...s")); i++)
{
index = i;
}
if (index < used)
return index;
return -1;
}
void Bag::UpdateCount(int index, int amount)
{
this->bag[index].IncrCount(amount);
}
bool Bag::PutInBag(Item &newItem)
{
int index = 0;
if (!this->IsFull())
{
index = this->FindIndex(newItem);
if (index == -1)
{
if (used <= 0)
index = 0;
this->bag[index] = newItem;
cout << "I have put " << newItem.GetName() << " into your bag, Master." << endl;
used++;
}
else
{
this->UpdateCount(index, newItem.GetCount() + bag[index].GetCount());
cout << "You now have " << bag[index].GetCount() << " " << bag[index].GetName() << "s, Master." << endl;
}
return true;
}
return false;
}
bool Bag::RemoveFromBag(int index, int amount)
{
if (index >= used)
return false;
else // does contain, start removal
{
if (bag[index].GetCount() == amount)
{
bag[index] = bag[used-1];
bag[used-1] = Item();
}
else
{
bag[index].IncrCount(-amount);
}
used--;
return true;
}
}
string Bag::ToString() const
{
string out = "";
for (int i = 0; i < used; i++)
{
out+=this->bag[i].ToString();
}
return out;
}