I have been working on a simple 'fight' program where 2 players fight each other and one comes out victorious. step by step i have been adding things and now i want to add a time delay between each output so that it doesn't all pop up at once. (i know my code is bad, im new and will learn over time how to make it better, hopefully)
Long story short: I want a delay between each output. I've tried Sleep() but it sometimes will just stop the program and output "Time limit Exceeded". any advice is appreciated!
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int ROLL_DICE(int sides);
class Player{
private:
int hp;
string name;
public:
Player(string myname, int myhp){
name = myname;
hp = myhp;
}
Player(){
name = "blank";
hp = 10;
}
string GetName() const {
return name;
}
int GetHp() const {
return hp;
}
void GetHurt(){
int damage;
damage = ROLL_DICE(6);
hp = hp - damage;
}
};
int ROLL_DICE(int sides){
int roll = 1 + (rand()%sides);
if (roll == sides)
cout << "CRITICAL!\n";
return roll;
}
int main() {
srand(time(0)); // using the current time to generate a random seed to ensure a true random number
int damage;
Player Josh("Josh",20);
Player Curie("Curie", 20);
cout << "Welcome to the arena " << Josh.GetName() << " and " << Curie.GetName() << "!" << endl;
cout << Curie.GetName() << ", your HP is " << Curie.GetHp() << "." << endl;
cout << Josh.GetName() << ", your HP is " << Josh.GetHp() << "." << endl;
cout << "Fight!\n";
while (Josh.GetHp() > 0 && Curie.GetHp() > 0){
Josh.GetHurt();
cout << Josh.GetName() << "'s" << " new HP is " << Josh.GetHp() << endl;
Curie.GetHurt();
cout << Curie.GetName() << "'s" << " new HP is " << Curie.GetHp() << endl;
}
if (Josh.GetHp() > 0)
cout << "Josh Won!";
else
cout << "Curie Won!";
return 0;
}