# stumped

This topic is 2500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

im making a simple game to test my abilities. Lets use the following code as an example:

class player
{
int hp;
};

this is my theoretical player class and lets make int hp equal to 80. i want to know how i make it to where 80 is the max hp can be and will not be able to go over. like for example if i use a potion and potion in my game revives 60 hp and i already have 50 how do i make it to where it tops off at 80 and doesn't rise to 110?

##### Share on other sites
 class Player { int hp; // set it to some value in the constructor (maybe the same as maxHp at first) int maxHp; // set it to some value in the constructor (80 in your example) public: void applyHp(int amount) { hp += amount; if (hp < 0) hp = 0; // if amount is negative (i.e. damage), don't let an invalid amount of hp be assigned if (hp > maxHp) hp = maxHp; // if amount is positive (i.e. drank a potion), clamp the hp to the max } }; 

Maybe not the best function name or the best way to abstract applying damage/health bonuses, but that's the idea behind how you clamp a value.

##### Share on other sites

 class Player { int hp; // set it to some value in the constructor (maybe the same as maxHp at first) int maxHp; // set it to some value in the constructor (80 in your example) public: void applyHp(int amount) { hp += amount; if (hp < 0) hp = 0; // if amount is negative (i.e. damage), don't let an invalid amount of hp be assigned if (hp > maxHp) hp = maxHp; // if amount is positive (i.e. drank a potion), clamp the hp to the max } }; 

Maybe not the best function name or the best way to abstract applying damage/health bonuses, but that's the idea behind how you clamp a value.

Thank you very much sir.

##### Share on other sites
 #include <iostream> #include <string> namespace terribad { template<class T, T MinValue, T MaxValue> struct bounding_trait_check_always { static T set(T const& value) { if(value < MinValue) return MinValue; if(value > MaxValue) return MaxValue; return value; } static T get(T const& value) { if(value < MinValue) return MinValue; if(value > MaxValue) return MaxValue; return value; } }; template<class T, T MinValue, T MaxValue> struct bounding_trait_check_set { static T set(T const& value) { if(value < MinValue) return MinValue; if(value > MaxValue) return MaxValue; return value; } static T get(T const& value) { return value; } }; template<class T, T MinValue, T MaxValue> struct bounding_trait_check_get { static T set(T const& value) { return value; } static T get(T const& value) { if(value < MinValue) return MinValue; if(value > MaxValue) return MaxValue; return value; } }; template<class T, T MinValue, T MaxValue, class BoundingTrait = bounding_trait_check_set<T, MinValue, MaxValue> > struct bounded { bounded(T const& newVal = T()) : value(BoundingTrait::set(newVal)) { } operator T () const { return BoundingTrait::get(value); } void operator=(T const& newValue) { value = BoundingTrait::set(newValue); } void operator+=(T const& newValue) { value = BoundingTrait::set(value + newValue); } void operator-=(T const& newValue) { value = BoundingTrait::set(value - newValue); } private: T value; }; } class Player { terribad::bounded<int, 0, 80> hp; public: void applyHp(int amount) { hp += amount; } int getCurrentHp() { return hp; } }; int main() { Player p = Player(); p.applyHp(1000); std::cout<<p.getCurrentHp()<<std::endl; } 

Because somebody had to post it.

##### Share on other sites

 #include <iostream> #include <string> namespace terribad { template<class T, T MinValue, T MaxValue> struct bounding_trait_check_always { static T set(T const& value) { if(value < MinValue) return MinValue; if(value > MaxValue) return MaxValue; return value; } static T get(T const& value) { if(value < MinValue) return MinValue; if(value > MaxValue) return MaxValue; return value; } }; template<class T, T MinValue, T MaxValue> struct bounding_trait_check_set { static T set(T const& value) { if(value < MinValue) return MinValue; if(value > MaxValue) return MaxValue; return value; } static T get(T const& value) { return value; } }; template<class T, T MinValue, T MaxValue> struct bounding_trait_check_get { static T set(T const& value) { return value; } static T get(T const& value) { if(value < MinValue) return MinValue; if(value > MaxValue) return MaxValue; return value; } }; template<class T, T MinValue, T MaxValue, class BoundingTrait = bounding_trait_check_set<T, MinValue, MaxValue> > struct bounded { bounded(T const& newVal = T()) : value(BoundingTrait::set(newVal)) { } operator T () const { return BoundingTrait::get(value); } void operator=(T const& newValue) { value = BoundingTrait::set(newValue); } void operator+=(T const& newValue) { value = BoundingTrait::set(value + newValue); } void operator-=(T const& newValue) { value = BoundingTrait::set(value - newValue); } private: T value; }; } class Player { terribad::bounded<int, 0, 80> hp; public: void applyHp(int amount) { hp += amount; } int getCurrentHp() { return hp; } }; int main() { Player p = Player(); p.applyHp(1000); std::cout<<p.getCurrentHp()<<std::endl; } 

Because somebody had to post it.

I realize your post was tongue-in-cheek (namespace terribad, and all), but this guy is an obvious beginner, and there's a good chance he's now going to try and understand what you've done and add it to his game. We all know how smart you are, but this is a bit over-the-top IMO.

##### Share on other sites
This is called a "class invariant". Googling this term could be enlightening.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633728
• Total Posts
3013575
×