#include <iostream>
#include <cstdlib>
#include <ctime>
class Battle {
private:
int my_hp;
int my_max;
int monster_hp;
int monster_max;
bool game_over = false;
public:
Battle(int my, int monster)
: my_hp(my), my_max(my),
monster_hp(monster), monster_max(monster) {}
void MyAttack() {
if (game_over) return;
int damage = rand() % 10 + 1;
std::cout << "You attack! Damage: " << damage << std::endl;
monster_hp -= damage;
if (monster_hp <= 0) {
monster_hp = 0;
game_over = true;
std::cout << "You win!" << std::endl;
}
}
void MonsterAttack() {
if (game_over) return;
int damage = rand() % 10 + 1;
std::cout << "Monster attacks! Damage: " << damage << std::endl;
my_hp -= damage;
if (my_hp <= 0) {
my_hp = 0;
game_over = true;
std::cout << "Monster wins!" << std::endl;
}
}
void Status() const {
std::cout << "My HP: " << my_hp
<< " | Monster HP: " << monster_hp
<< std::endl;
}
bool IsGameOver() const {
return game_over;
}
};
int main() {
srand(time(nullptr));
Battle game(50, 40);
while (!game.IsGameOver()) {
game.Status();
std::cout << "1: Attack 2: Do nothing > ";
int choice;
std::cin >> choice;
if (choice == 1)
game.MyAttack();
if (!game.IsGameOver())
game.MonsterAttack();
std::cout << "------------------" << std::endl;
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: