玩家PK

问题 C: 玩家PK

问题 C: 玩家PK

时间限制: 1 Sec   内存限制: 128 MB
提交: 918   解决: 163
[ 提交][ 状态]

题目描述

在很多游戏中,两个玩家进行PK(Player Killing,游戏中玩家相互战斗的模式)是判断自己战力的重要方法。这里简化一下。定义一个类Role来表示游戏玩家。具有4个非负整数属性:血量(hp)、攻击力(ce)、防御力(de)和先攻值(fa),以及一个代表玩家昵称的string属性name。
PK采用回合制。假设有两个玩家A和B,当A与B进行PK时,先攻值高的一方首先发动攻击。如A的先攻值大于B,那么第一回合中,A首先攻击B,B防御;接着,如果B没有死亡,则攻击A,A处于防御状态。如果A没有死亡,则进入第二回合。一直重复进行下去,直到一方死亡为止。
当一方被攻击时,其血量变化遵循如下规则:(1)如果其防御力不小于攻击方的攻击力,那么其血量减少1。(2)如果其防御力小于攻击方的攻击力,那么其血量减少(对方的攻击力-自己的防御力)。同时,每一方被攻击一次,其防御力减少1。
比如:当A攻击B时,如果A的攻击力为10,B的防御力为4,则B的血量减少6,防御力减少1。如果A的攻击力为10,B的防御力大于等于10,则B的血量和防御力都减少1。
在Role中,定义两个方法:
(1)Role(string n, int h, int c, int d, int f):依次初始化昵称、血量、攻击力、防御力和先攻值。两个玩家的先攻值不同。
(2)void combat(Role &another):根据PK规则展示PK过程。假设PK的两个玩家分别是A和B,当A攻击B时,显示:
A attacks B:B hp=#,de=$
其中#和$分别是B被攻击后的剩余血量和防御力。当剩余血量小于0时,也显示为0,不能显示负数。
当B攻击A时,显示:
B attacks A:A hp=%,de=&
其中%和&分别是A被攻击后的剩余血量和防御力。当剩余血量小于0时,也显示为0,不能显示负数。
最后,当A的血量为0时,输出:
B wins.
当B的血量为0时,输出:
A wins.

输入

有两行,每行包括1个字符串(玩家昵称)、4个正整数(对应玩家的血量、攻击力、防御力和先攻值。

输出

如前所述,见样例。

样例输入

A 30 10 4 1 B 10 2 20 2

样例输出

B attacks A:A hp=29,de=3
A attacks B:B hp=9,de=19
B attacks A:A hp=28,de=2
A attacks B:B hp=8,de=18
B attacks A:A hp=27,de=1
A attacks B:B hp=7,de=17
B attacks A:A hp=26,de=0
A attacks B:B hp=6,de=16
B attacks A:A hp=24,de=0
A attacks B:B hp=5,de=15
B attacks A:A hp=22,de=0
A attacks B:B hp=4,de=14
B attacks A:A hp=20,de=0
A attacks B:B hp=3,de=13
B attacks A:A hp=18,de=0
A attacks B:B hp=2,de=12
B attacks A:A hp=16,de=0
A attacks B:B hp=1,de=11
B attacks A:A hp=14,de=0
A attacks B:B hp=0,de=10
A wins.

#include <iostream>
#include <string>
 
using namespace std;
 
 
class Role{
     
public:
    int hp_;  //血量
    int ce_;  //攻击力
    int de_; // 防御力
    int fa_; // 先攻值
    string name_; //名字
    bool flag;
 
    Role(string n , int h ,int c , int d , int f ):
    name_(n),hp_(h),ce_(c),de_(d),fa_(f)
    {
        flag = false;
    }
    void combat(Role & another){
        if(fa_ > another.fa_){
            flag = true;
            fa_ = another.fa_;
        }
        while(1){
             
         // 判断谁输谁赢
        if(hp_ ==0 ){
            cout<<another.name_<<" wins."<<endl;
            break;
        }
        if(another.hp_ == 0){
            cout<<name_<<" wins."<<endl;
            break;
        }
         
        if(flag){  //a攻击b
            cout<<name_<<" attacks "<<another.name_<<":"<<another.name_<<" ";
            if(ce_ > another.de_){
                another.hp_ -= (ce_-another.de_);
                another.de_--;
                if(another.hp_ < 0){
                    another.hp_ = 0;
                }
                if(another.de_ <0){
                    another.de_ = 0;
                }
            }else{
                another.hp_ --;
                another.de_ --;
                if(another.hp_ < 0){
                    another.hp_ = 0;
                }
                if(another.de_ <0){
                    another.de_ = 0;
                }
            }
            cout<<"hp="<<another.hp_<<",de="<<another.de_<<endl;
            flag = false;
        }else{
             
           cout<<another.name_<<" attacks "<<name_<<":"<<name_<<" ";
            if(another.ce_ > de_){
                hp_ -= (another.ce_-de_);
                de_--;
                if(hp_ < 0){
                    hp_ = 0;
                }
                if(de_ <0){
                    de_ = 0;
                }
            }else{
                hp_ --;
                de_ --;
                if(hp_ < 0){
                    hp_ = 0;
                }
                if(de_ <0){
                    de_ = 0;
                }
            }
            cout<<"hp="<<hp_<<",de="<<de_<<endl;
            flag = true;
        }
        }
    }
};
int main()
{
    int a, b, c, d;
    string name;
    cin>>name>>a>>b>>c>>d;
    Role one(name, a, b, c, d);
    cin>>name>>a>>b>>c>>d;
    Role two(name, a, b, c, d);
    one.combat(two);
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值