# stumped

This topic is 2807 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>

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

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

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 18
• 15
• 9
• 9