示例#1
0
//Turn is the function that is called to simulate a battle. At the moment, it only takes 1 Player and 1 Monster. This means no multi-enemy fights for now.
//However, that functionality might be added in later.
void turn(Player& Tim, Monster creature)
{
    srand(time(0));//semi-random generation for chances at critical hits or fumbles.
    int choice, Tim_damage, creature_damage;
    bool flee = false;
    cout << "Tim sees a creature! Tim has poor vision, so the identity of the creature is unknown." << endl;
    do
    {
        int r = rand(), chance1 = (r % 10) + 1, chance2 = (r % 10) + 1;
        display_battle(Tim.get_health(), creature.get_health());
        choice = battle_menu();//battle_menu just gets the answer for what the Player wants to do on their turn.
        if(choice == 1)//If they chose to attack, then:
        {
            Tim_damage = Tim.get_damage();
            creature_damage = creature.get_damage();
            if (chance1 == 1 || chance1 == 2)//First determine how the attack went
            {
                creature.hurt((Tim_damage * 3) / 2);//And apply the damage.
                cout << "Tim hits the creature with a critical hit! The creature was hurt for " << (Tim_damage * 3) / 2 << " damage!" << endl;
            }
            else if (chance1 == 9 || chance1 == 10)
            {
                creature.hurt(Tim_damage / 2);
                cout << "Tim fumbles with the weapon and the creature seems undazed. The creature was hurt for " << Tim_damage / 2 << " damage!" << endl;
            }
            else if (chance1 != 1 && chance1 != 2 && chance1 != 9 && chance1 != 10)
            {
                creature.hurt(Tim_damage);
                cout << "The creature was hurt for " << Tim_damage << " damage!" << endl;
            }
            if(creature.check_life())//If creature is still alive, they get to retaliate.
            {
                if (chance2 == 1)
                {
                    Tim.hurt(creature_damage * 2);
                    cout << "The creature lands a critical hit! Tim was hurt for " << creature_damage * 2 << " damage!" << endl;
                }
                else if (chance2 == 2 || chance2 == 3)
                {
                    Tim.hurt(creature_damage / 2);
                    cout << "The creature trips and falls on its face! Tim was hurt for " << creature_damage / 2 << " damage!" << endl;
                }
                else if (chance2 != 1 && chance2 != 2 && chance2 != 3)
                {
                    Tim.hurt(creature_damage);
                    cout << "Tim was hurt for " << creature_damage << " damage!" << endl;
                }
            }
            else
            {
                creature.kill();//Otherwise, the creature is dead.
            }
        }
        else if(choice == 2)//Or the Player might choose to flee the battle.
        {
            cout << "Tim flees from the creature like a coward!" << endl;
            cout << "The creature is unable to keep up with Tim's cowardly speed." << endl;
            flee = true;
        }
    } while(Tim.check_life() and creature.check_life() and !flee);//The battle will continue until someone is dead or the Player fled the battle.
    if(!creature.check_life())//If the creature is the dead one, display the battle results.
    {
        cout << "The battle has ended. Tim has slain the creature with a mighty finishing blow!" << endl;
        cout << "Tim gains " << creature.get_exp_reward() << " experience from slaying the creature!" << endl;
        Tim.add_exp(creature.get_exp_reward());
        cout << "Tim has " << Tim.get_max_exp() - Tim.get_exp() << " experience until he levels up!" << endl;
        cout << "Tim finds some gold pieces on the ground near the creature." << endl;
        Tim.add_gold(creature.get_gold_reward());
        cout << "Tim has " << Tim.get_gold() << " gold pieces." << endl;
        cout << "Feeling empowered, Timothy returns to the village." << endl;
    }
}