Skip to content

Commit 2196e1f

Browse files
committed
Added Resources class
1 parent c3023f1 commit 2196e1f

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

include/Resources.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef RESOURCES_H
2+
#define RESOURCES_H
3+
4+
class Resources {
5+
public:
6+
typedef unsigned short rtype;
7+
private:
8+
rtype wood;
9+
rtype sheep;
10+
rtype grain;
11+
rtype ore;
12+
rtype brick;
13+
public:
14+
Resources();
15+
Resources(rtype wood, rtype sheep, rtype grain, rtype ore, rtype brick);
16+
Resources(const Resources&);
17+
~Resources();
18+
Resources& operator=(const Resources&);
19+
20+
rtype getWood() const;
21+
void setWood(rtype newWood);
22+
23+
rtype getSheep() const;
24+
void setSheep(rtype newSheep);
25+
26+
rtype getGrain() const;
27+
void setGrain(rtype newGrain);
28+
29+
rtype getOre() const;
30+
void setOre(rtype newOre);
31+
32+
rtype getBrick() const;
33+
void setBrick(rtype newBrick);
34+
35+
Resources operator+(const Resources&) const;
36+
Resources& operator+=(const Resources&);
37+
38+
Resources operator-(const Resources&) const;
39+
Resources& operator-=(const Resources&);
40+
41+
bool operator<(const Resources&) const;
42+
bool operator>(const Resources&) const;
43+
};
44+
45+
#endif

src/Resources.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include "Resources.h"
2+
3+
#include <stdexcept>
4+
5+
using std::runtime_error;
6+
7+
Resources::Resources() : wood(0), sheep(0), grain(0), ore(0), brick(0) {
8+
9+
}
10+
11+
Resources::Resources(rtype wood, rtype sheep, rtype grain, rtype ore, rtype brick) :
12+
wood(wood), sheep(sheep), grain(grain), ore(ore), brick(brick) {
13+
14+
}
15+
16+
Resources::Resources(const Resources& other) {
17+
*this = other;
18+
}
19+
20+
Resources::~Resources() {
21+
22+
}
23+
24+
Resources& Resources::operator=(const Resources& other) {
25+
wood = other.getWood();
26+
sheep = other.getSheep();
27+
grain = other.getGrain();
28+
ore = other.getOre();
29+
brick = other.getBrick();
30+
return *this;
31+
}
32+
33+
Resources::rtype Resources::getWood() const {
34+
return wood;
35+
}
36+
37+
void Resources::setWood(rtype newWood) {
38+
wood = newWood;
39+
}
40+
41+
Resources::rtype Resources::getSheep() const {
42+
return sheep;
43+
}
44+
45+
void Resources::setSheep(rtype newSheep) {
46+
sheep = newSheep;
47+
}
48+
49+
Resources::rtype Resources::getGrain() const {
50+
return grain;
51+
}
52+
53+
void Resources::setGrain(rtype newGrain) {
54+
grain = newGrain;
55+
}
56+
57+
Resources::rtype Resources::getOre() const {
58+
return ore;
59+
}
60+
61+
void Resources::setOre(rtype newOre) {
62+
ore = newOre;
63+
}
64+
65+
Resources::rtype Resources::getBrick() const {
66+
return brick;
67+
}
68+
69+
void Resources::setBrick(rtype newBrick) {
70+
brick = newBrick;
71+
}
72+
73+
/**
74+
* Makes a new resource object by adding together the components of the two added objects.
75+
*/
76+
Resources Resources::operator+(const Resources& other) const {
77+
return Resources(wood + other.getWood(),
78+
sheep + other.getSheep(),
79+
grain + other.getGrain(),
80+
ore + other.getOre(),
81+
brick + other.getBrick());
82+
}
83+
84+
/**
85+
* Adds the components of the passed in resource object to this object;
86+
*/
87+
Resources& Resources::operator+=(const Resources& other) {
88+
*this = *this + other;
89+
return *this;
90+
}
91+
92+
/**
93+
* Subtracts the other resources from this one. Throws an exception if that
94+
* would cause the resources in the resulting object to be negative.
95+
*/
96+
Resources Resources::operator-(const Resources& other) const {
97+
if(*this < other) {
98+
throw std::runtime_error("Insufficient resources to subtract.");
99+
}
100+
return Resources(wood - other.getWood(),
101+
sheep - other.getSheep(),
102+
grain - other.getGrain(),
103+
ore - other.getOre(),
104+
brick - other.getBrick());
105+
}
106+
107+
/**
108+
* Subtracts the components of the passed in resource object to this object.
109+
* Throws an exception if that would cause this object to have negative components.
110+
*/
111+
Resources& Resources::operator-=(const Resources& other) {
112+
*this = *this - other;
113+
return *this;
114+
}
115+
116+
/**
117+
* True if any of the individual resources is less than the corresponding resource in the
118+
* other object. Useful for testing if a resource cost can be paid.
119+
* For example, if playerResources < cost, then the item cannot be bought;
120+
*/
121+
bool Resources::operator<(const Resources& other) const {
122+
return wood < other.getWood() ||
123+
sheep < other.getSheep() ||
124+
grain < other.getGrain() ||
125+
sheep < other.getSheep() ||
126+
ore < other.getOre() ||
127+
brick < other.getBrick();
128+
}
129+
130+
/**
131+
* True if all of the individual resources are greater than
132+
*/
133+
bool Resources::operator>(const Resources& other) const {
134+
return other < *this;
135+
}
136+

0 commit comments

Comments
 (0)