Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2369 views
1
#include "alpacaFracasPack.h"
2
3
AlpacaFracasPack::AlpacaFracasPack()
4
{
5
packPower = 0;
6
}
7
8
9
AlpacaFracasPack::~AlpacaFracasPack()
10
{
11
//delete all the alpacas!
12
for(auto itr=alpacaList.begin(); itr != alpacaList.end(); itr++) {
13
delete *itr;
14
}
15
}
16
17
18
//Removes the first alpaca from the pack and returns it. Returns null if there is no alpaca
19
Alpaca *
20
AlpacaFracasPack::unload()
21
{
22
Alpaca *result;
23
24
//handle empty lists
25
if(alpacaList.size() == 0) return NULL;
26
27
//ok, we have an alpaca!
28
result = alpacaList[0];
29
alpacaList.erase(alpacaList.begin());
30
31
return result;
32
}
33
34
35
//Adds an alpaca to the pack returns true on success and false if this alpaca would push
36
//the points limit over the max points ormax alpacas
37
bool
38
AlpacaFracasPack::load(Alpaca *a)
39
{
40
//check legality
41
if(packPower + a->getPower() > MAX_POINTS) {
42
return false;
43
}
44
45
//welcome to the fold!
46
packPower += a->getPower();
47
alpacaList.push_back(a);
48
return true;
49
}
50
51
//returns the number of remaining alpacas
52
int
53
AlpacaFracasPack::packSize()
54
{
55
return alpacaList.size();
56
}
57
58
59
//returns the number of remaining power points in the pack
60
unsigned int
61
AlpacaFracasPack::power()
62
{
63
unsigned int result=0;
64
65
for(auto itr=alpacaList.begin(); itr!=alpacaList.end(); itr++) {
66
result += (*itr)->getPower();
67
}
68
69
return result;
70
}
71
72
73
//returns the power points at the time of pack creation
74
unsigned int
75
AlpacaFracasPack::originalPower()
76
{
77
return packPower;
78
}
79
80
81
//allows access to the alpacas in the pack note that this does not return by reference.
82
//you cannot change the alpacas around!
83
Alpaca *
84
AlpacaFracasPack::operator[](unsigned int i)
85
{
86
//handle the possibility of an invalid index
87
if(i < alpacaList.size()) {
88
return alpacaList[i];
89
}
90
91
//if we make it here, the index was invalid
92
return NULL;
93
}
94
95