stumped

Started by
4 comments, last by rip-off 12 years, 2 months ago
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?
Advertisement

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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]


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.

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

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.



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

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This is called a "class invariant". Googling this term could be enlightening.

This topic is closed to new replies.

Advertisement