|
| 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