Hello, I am writing a simple combat game and I need some help. I would like to keep my integers which I created in a couple classes from going below 0.
#include "stdafx.h"
#include <iostream>
usingnamespace std;
class Monster
{
public:
int health;
int strength;
int damage;
Monster ()
{
health = 20;
strength = 20;
}
public:
int attackp ()
{
damage = strength/2;
cout << "Monster attacked!\n You took " << damage << " damage!\n";
return damage;
}
};
class Fighter
{
public:
int health;
int strength;
int armor;
int experience;
int level;
public:
Fighter ()
{
health = 25;
strength = 30;
armor = 0;
experience = 0;
level = 1;
}
int stats()
{
cout << "Health is: " << health << endl;
cout << "Strength is: " << strength << endl;
cout << "Armor is: " << armor << endl;
cout << "Experience is: " << experience << endl;
cout << "Level is: " << level << endl;
char f;
cin >> f;
return 0;
}
int attack()
{
int givDamage;
givDamage = strength/2;
cout << "You struck the enemy for " << givDamage << " damage!\n";
return givDamage;
}
int getHealth ()
{
return health;
}
};
class shop
{
int potion1;
int potion2;
int dagger;
int club;
int sword;
int gsword;
};
void arena()
{
cout << "You have entered the Arena\nPrepare for battle!\n";
cout << "Your opponent has entered the Arena!/n" << endl;
Monster m;
Fighter a;
int action;
char quit;
do
{
cout << "(1) Attack\n";
cout << "(2) Block\n";
cout << "(3) Items\n";
cin >> action;
switch (action)
{
case (1) : m.health -= a.attack();
cout << "Enemy health is at: " << m.health << endl;
a.health -= m.attackp();
cout << "Your health is now at: " << a.health << endl;
break;
}
}
while ( (a.health >= 0) || (m.health >= 0) );
cout << "You have been defeated!\nReturn to the main menu?\nY/N\n";
cin >> quit;
switch (quit)
{
case'y' :
case'Y' : (a.health <= 0);
case'n' :
case'N' : return;
}
char f;
cin >> f;
return;
}
void exit()
{
}
int main()
{
Fighter a;
char menu;
cout << "Welcome to the Arena\n""\nWhere would you like to go?\n""(A) Type A to go to the shop\n""(B) Type B to view your stats\n""(C) Type C to go to the arena\n""(D) Type D to exit the game\n" << endl;
cin >> menu;
switch (menu)
{
case'a' :
case'A' : shop();
break;
case'b' :
case'B' : a.stats();
break;
case'c' :
case'C' : arena();
break;
case'd' :
case'D' : exit();
break;
default :
cout << "That is not a valid entry\n";
break;
}
char f;
cin >> f;
return 0;
}
There are classes for Monster and Fighter right now. Inside those classes is where you find the int a.health and m.health. I tried making them unsigned int's but that just caused them to roll back to their highest possible value when it went below zero.
I don't want the health to read out, "Your health is now at: -5" or whatever it may be. I want it to stick at 0 when it reaches less than, or equal to 0.
I'm also having a hard time trying to envision how to progressively make the enemy harder. I'm still very new to C++ and coding in general, so doing something like that is probably above my current knowledge.
I don't want the health to read out, "Your health is now at: -5" or whatever it may be. I want it to stick at 0 when it reaches less than, or equal to 0.
1 2 3
int health;
// ... something on health
health = (health<0)?0:health;
Thank you. Would you mind elaborating on this "health = (health<0)?0:health;"? I know that it is like a mini if, but I can't fully understand it. I understand what it does (something like, if health is less that 0, health = 0), but I don't really know how it works.
EDIT:
Wait, I took a second look at it. Is it,
If health is less than 0 make health 0 ( (health < 0)?0 ) else ( : ) keep it as health (health)?