どこかの宿題
似たような課題をどこかで見ました。
・・・
あ、うちの大学だったな。
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;
class Player {
private:
string name;
int max_hp;
int hp;
int mp; //※magic power
int strength;
public:
Player(string n = "Knight") {
name = n;
max_hp = hp = 100 + rand()%201;
mp = rand()%25+1;
strength = 50 - mp;
}
int command() {
int c;
cout << "Attack(1) or Recovery(2) ? ->";
cin >> c;
if (c == 2)
return 2;
else
return 1;
}
void recovery() {
if (mp < 10)
cout << "mp < 10" <<endl ;
else {
mp -= 10;
int r = 30 + rand()%71;
hp = (hp+r > max_hp) ? max_hp : hp+r;
cout << "Recovery " << r << " HP " << hp << endl;
}
}
int attack() {
return strength;
}
void damage(int dmg) {
hp = (hp-dmg < 0) ? 0 : hp-dmg;
cout << name << " Damage " << dmg << " HP " << hp <<endl;
}
void show() {
cout << name << " HP " << hp << " MP " << mp << " STR " << strength << endl;
}
int is_die() { return (hp <= 0); }
};
class Monster {
private:
string name;
int hp;
int strength;
public:
Monster(string n = "Evil") {
name = n;
hp = 300 + rand()%201;
strength = 25 + rand()%26;
}
int attack(){ return strength; }
void damage(int dmg) {
hp = (hp-dmg < 0) ? 0 : hp-dmg;
cout << name << " Damage " << dmg << " HP " << hp <<endl;
}
void show() {
cout << name << " HP " << hp << " STR " << strength << endl;
}
int is_die() { return (hp <= 0); }
};
int main()
{
srand((unsigned int)time(NULL));
Player player;
Monster monster;
while (1) {
player.show();
monster.show();
if (player.command() == 1)
monster.damage(player.attack());
else
player.recovery();
if (monster.is_die()) {
cout << "You won!!" << endl;
break;
}
player.damage(monster.attack());
if (player.is_die()) {
cout << "You lose..." << endl;
break;
}
cout << endl;
}
return 0;
}