補充程序之遊戲系列— 1遊戲中的角色類 (1)



*Copyright(c) 2016.煙臺大學計算機與控制工程學院
*ALL rights  reserved.
  *文件名稱:main.cpp
   *作者:孫亞茹
*完成日期:2016年6月7日
*問題描述:補充完整需要的成員函數。
*//

#include <iostream>

using namespace std;

class Role
{
public:
    void setRole(string nam,int b);
    void show();
    void attack();
    void eat(int n);
    void beAttack();
    bool isLife();
private:
    string name;
    int blood;
    bool life;
};
bool Role::isLife()
{
    if(blood>0)
        return true;
    else
        return false;
}
void Role::setRole(string nam,int b)
{
    name=nam;
    blood=b;
}
void Role::attack()
{
    if(isLife())
    {
        blood++;
    }
}
void Role::eat(int n)
{
    if(isLife())
    {
        blood+=n;
    }
}
void Role::beAttack()
{
    if(isLife())
    {
        blood--;
    }
}
void Role::show()
{
    if(isLife())
    cout<<name<<"還有"<<blood<<"滴血"<<endl;
    else
        cout<<name<<"已經死了"<<endl;
}
int main()
{
    Role mary;
    mary.setRole("Mary",4);
    mary.show();
    int n;
    while(1)
    {
        cout<<" 1 attack  2 beAttack  3 eat  "<<endl;
        cout<<"請您選擇相應的動作:"<<endl;
        cin>>n;
        if(n==1)
        {
             mary.attack();
             mary.show();
        }

        else if(n==2)
        {

            mary.beAttack();
            mary.show();
        }
        else if(n==3)
           {
                mary.eat(2);
            mary.show();
           }
        else
            break;
    }
    return 0;
}

總結:

           學會了bool型函數的用法。

           做過後面的比較難的項目,再回過頭來做當時不會做的就感覺沒那麼難了。

           高考加油!!!


        

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章