首页 > 其他分享 >实验五

实验五

时间:2022-11-25 23:24:39浏览次数:25  
标签:string void player 实验 include HP name

Task4:

pets.hpp

#pragma ocne
#include<iostream>
#include<string>
using namespace std;
class MachinePets
{
    string nickname;
    public:
        MachinePets(const string s): nickname{s}{ };
        string get_nickname() const;
        virtual string talk()=0;
};
string MachinePets::get_nickname() const
{
    return nickname;
}
class PetCats:public MachinePets
{
    public:
        PetCats(const string s) : MachinePets(s) {};
        string talk()
        {
            string s={"miao wu~"};
            return s;
        }
};
class PetDogs:public MachinePets
{
    public:
        PetDogs(const string s) : MachinePets(s) {};
        string talk()
        {
            string s={"wang wu~"};
            return s;
        }
};

task4

#include <iostream>
#include "pets.hpp"

void play(MachinePets &obj) {
    std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
}

void test() {
    PetCats cat("miku");
    PetDogs dog("da huang");

    play( cat );
    play( dog );
}

int main() {
    test();
}

运行结果

Task5:

 Person.hpp

#pragma once
#include<iostream>
#include<string>
#define MAX 1000
using namespace std;
class Person
{
    string name;
    string telephone;
    string email;
    public:
        Person() 
        {
            name={""};
            telephone={""};
            email={""};
        };
        Person(string x1,string x2) : name{x1},telephone{x2}{ };
        Person(const Person &T)
        {
            name=T.name;
            telephone=T.telephone;
            email=T.email;
        };
        void update_telephone();
        void update_email();
        friend ostream& operator<<(ostream &v,const Person &T);
        friend istream& operator>>(istream &v,Person &T);
        friend bool operator==(const Person &T,const Person &L);
};
void Person::update_telephone()
{
    string s;
    cout<<"Enter the telephone number:";
    cin>>s;
    telephone=s;
    cout<<"telephone number has been updated"<<endl;
}
void Person::update_email()
{
    string s;
    cout<<"Enter the email address:"; 
    cin>>s;
    email=s;
    cout<<"email address has been updated"<<endl; 
}
ostream& operator<<(ostream &v,const Person &T)
{
    v<<T.name<<" "<<T.telephone<<" "<<T.email<<endl;
    return v; 
}
istream& operator>>(istream &v,Person &T)
{
    getline(v,T.name);
    getline(v,T.telephone);
    getline(v,T.email);
cin.ignore(9999,'\n'); return v; } bool operator==(const Person &T,const Person &L) { return ((T.name==L.name)&&(T.telephone==L.telephone)&&(T.email==L.email)); }

task5

#include <iostream>
#include <fstream>
#include <vector>
#include "Person.hpp"

void test() {
    using namespace std;

    vector<Person> phone_book;
    Person p;

    cout << "Enter person's contact until press Ctrl + Z" << endl;
    while(cin >> p)
        phone_book.push_back(p);
    cin.clear();  //新增的一个操作 
    cout << "\nupdate someone's contact: \n";
    phone_book.at(0).update_telephone();
    phone_book.at(0).update_email();    
    
    cout << "\ndisplay all contacts' info\n";
    for(auto &phone: phone_book)
        cout << phone << endl;
    
    cout << "\ntest whether the same contact\n";
    cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl;
}

int main() {
    test();
}

运行结果:

Task6:

player.h

//=======================
//        player.h
//=======================

// The base class of player
// including the general properties and methods related to a character

#ifndef _PLAYER
#define _PLAYER

#include <iomanip>        // use for setting field width
#include <time.h>        // use for generating random factor
#include "container.h"

enum job {sw, ar, mg};    /* define 3 jobs by enumerate type
                               sword man, archer, mage */
class player
{
    friend void showinfo(player &p1, player &p2);
    friend class swordsman;

protected:
    int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
    // General properties of all characters
    string name;    // character name
    job role;        /* character's job, one of swordman, archer and mage,
                       as defined by the enumerate type */
    container bag;    // character's inventory

public:
    virtual bool attack(player &p)=0;    // normal attack
    virtual bool specialatt(player &p)=0;    //special attack
    virtual void isLevelUp()=0;            // level up judgement
    /* Attention!
    These three methods are called "Pure virtual functions".
    They have only declaration, but no definition.
    The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects. 
    The detailed definition of these pure virtual functions will be given in subclasses. */

    void reFill();        // character's HP and MP resume
    bool death();        // report whether character is dead
    void isDead();        // check whether character is dead
    bool useHeal();        // consume heal, irrelevant to job
    bool useMW();        // consume magic water, irrelevant to job
    void transfer(player &p);    // possess opponent's items after victory
    void showRole();    // display character's job
    
private:
    bool playerdeath;            // whether character is dead, doesn't need to be accessed or inherited
};

player.cpp

//=======================
//        player.cpp
//=======================


// character's HP and MP resume
void player::reFill()
{
    HP=HPmax;        // HP and MP fully recovered
    MP=MPmax;
}

// report whether character is dead
bool player::death()
{
    return playerdeath;
}

// check whether character is dead
void player::isDead()
{
    if(HP<=0)        // HP less than 0, character is dead
    {
        cout<<name<<" is Dead." <<endl;
        system("pause");
        playerdeath=1;    // give the label of death value 1
    }
}

// consume heal, irrelevant to job
bool player::useHeal()
{
    if(bag.nOfHeal()>0)
    {
        HP=HP+100;
        if(HP>HPmax)        // HP cannot be larger than maximum value
            HP=HPmax;        // so assign it to HPmax, if necessary
        cout<<name<<" used Heal, HP increased by 100."<<endl;
        bag.useHeal();        // use heal
        system("pause");
        return 1;    // usage of heal succeed
    }
    else                // If no more heal in bag, cannot use
    {
        cout<<"Sorry, you don't have heal to use."<<endl;
        system("pause");
        return 0;    // usage of heal failed
    }
}

// consume magic water, irrelevant to job
bool player::useMW()
{
    if(bag.nOfMW()>0)
    {
        MP=MP+100;
        if(MP>MPmax)
            MP=MPmax;
        cout<<name<<" used Magic Water, MP increased by 100."<<endl;
        bag.useMW();
        system("pause");
        return 1;    // usage of magic water succeed
    }
    else
    {
        cout<<"Sorry, you don't have magic water to use."<<endl;
        system("pause");
        return 0;    // usage of magic water failed
    }
}

// possess opponent's items after victory
void player::transfer(player &p)
{
    cout<<name<<" got"<<p.bag.nOfHeal()<<" Heal, and "<<p.bag.nOfMW()<<" Magic Water."<<endl;
    system("pause");
    bag.set(bag.nOfHeal(),bag.nOfMW()+p.bag.nOfMW());
    // set the character's bag, get opponent's items
}

// display character's job
void player::showRole()
{
    switch(role)
    {
    case sw:
        cout<<"Swordsman";
        break;
    case ar:
        cout<<"Archer";
        break;
    case mg:
        cout<<"Mage";
        break;
    default:
        break;
    }
}


// display character's job
void showinfo(player &p1, player &p2)
{
    system("cls");
    cout<<"##############################################################"<<endl;
    cout<<"# Player"<<setw(10)<<p1.name<<"   LV. "<<setw(3) <<p1.LV
        <<"  # Opponent"<<setw(10)<<p2.name<<"   LV. "<<setw(3) <<p2.LV<<" #"<<endl;
    cout<<"# HP "<<setw(3)<<(p1.HP<=999?p1.HP:999)<<'/'<<setw(3)<<(p1.HPmax<=999?p1.HPmax:999)
        <<" | MP "<<setw(3)<<(p1.MP<=999?p1.MP:999)<<'/'<<setw(3)<<(p1.MPmax<=999?p1.MPmax:999)
        <<"     # HP "<<setw(3)<<(p2.HP<=999?p2.HP:999)<<'/'<<setw(3)<<(p2.HPmax<=999?p2.HPmax:999)
        <<" | MP "<<setw(3)<<(p2.MP<=999?p2.MP:999)<<'/'<<setw(3)<<(p2.MPmax<=999?p2.MPmax:999)<<"      #"<<endl;
    cout<<"# AP "<<setw(3)<<(p1.AP<=999?p1.AP:999)
        <<" | DP "<<setw(3)<<(p1.DP<=999?p1.DP:999)
        <<" | speed "<<setw(3)<<(p1.speed<=999?p1.speed:999)
        <<" # AP "<<setw(3)<<(p2.AP<=999?p2.AP:999)
        <<" | DP "<<setw(3)<<(p2.DP<=999?p2.DP:999)
        <<" | speed "<<setw(3)<<(p2.speed<=999?p2.speed:999)<<"  #"<<endl;
    cout<<"# EXP"<<setw(7)<<p1.EXP<<" Job: "<<setw(7);
    p1.showRole();
    cout<<"   # EXP"<<setw(7)<<p2.EXP<<" Job: "<<setw(7);
    p2.showRole();
    cout<<"    #"<<endl;
    cout<<"--------------------------------------------------------------"<<endl;
    p1.bag.display();
    cout<<"##############################################################"<<endl;
}

#endif

swordsman.h


//=======================
// swordsman.h
//=======================


// Derived from base class player
// For the job Swordsman


#include "player.h"
class swordsman : public player // subclass swordsman publicly inherited from base player
{
public:
swordsman(int lv_in=1, string name_in="Not Given");
// constructor with default level of 1 and name of "Not given"
void isLevelUp();
bool attack (player &p);
bool specialatt(player &p);
/* These three are derived from the pure virtual functions of base class
The definition of them will be given in this subclass. */
void AI(player &p); // Computer opponent
};

 

swordsman.cpp

//=======================
//        swordsman.cpp
//=======================

// constructor. default values don't need to be repeated here
swordsman::swordsman(int lv_in, string name_in)
{
    role=sw;    // enumerate type of job
    LV=lv_in;
    name=name_in;
    
    // Initialising the character's properties, based on his level
    HPmax=150+8*(LV-1);        // HP increases 8 point2 per level
    HP=HPmax;
    MPmax=75+2*(LV-1);        // MP increases 2 points per level
    MP=MPmax;
    AP=25+4*(LV-1);            // AP increases 4 points per level
    DP=25+4*(LV-1);            // DP increases 4 points per level
    speed=25+2*(LV-1);        // speed increases 2 points per level
    
    playerdeath=0;
    EXP=LV*LV*75;
    bag.set(lv_in, lv_in);
}

void swordsman::isLevelUp()
{
    if(EXP>=LV*LV*75)
    {
        LV++;
        AP+=4;
        DP+=4;
        HPmax+=8;
        MPmax+=2;
        speed+=2;
        cout<<name<<" Level UP!"<<endl;
        cout<<"HP improved 8 points to "<<HPmax<<endl;
        cout<<"MP improved 2 points to "<<MPmax<<endl;
        cout<<"Speed improved 2 points to "<<speed<<endl;
        cout<<"AP improved 4 points to "<<AP<<endl;
        cout<<"DP improved 5 points to "<<DP<<endl;
        system("pause");
        isLevelUp();    // recursively call this function, so the character can level up multiple times if got enough exp
    }
}

bool swordsman::attack(player &p)
{
    double HPtemp=0;        // opponent's HP decrement
    double EXPtemp=0;        // player obtained exp
    double hit=1;            // attach factor, probably give critical attack
    srand((unsigned)time(NULL));        // generating random seed based on system time

    // If speed greater than opponent, you have some possibility to do double attack
    if ((speed>p.speed) && (rand()%100<(speed-p.speed)))        // rand()%100 means generates a number no greater than 100
    {
        HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10));        // opponent's HP decrement calculated based their AP/DP, and uncertain chance
        cout<<name<<"'s quick strike hit "<<p.name<<", "<<p.name<<"'s HP decreased "<<HPtemp<<endl;
        p.HP=int(p.HP-HPtemp);
        EXPtemp=(int)(HPtemp*1.2);
    }

    // If speed smaller than opponent, the opponent has possibility to evade
    if ((speed<p.speed) && (rand()%50<1))
    {
        cout<<name<<"'s attack has been evaded by "<<p.name<<endl;
        system("pause");
        return 1;
    }

    // 10% chance give critical attack
    if (rand()%100<=10)
    {
        hit=1.5;
        cout<<"Critical attack: ";
    }

    // Normal attack
    HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10));
    cout<<name<<" uses bash, "<<p.name<<"'s HP decreases "<<HPtemp<<endl;
    EXPtemp=(int)(EXPtemp+HPtemp*1.2);
    p.HP=(int)(p.HP-HPtemp);
    cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl;
    EXP=(int)(EXP+EXPtemp);
    system("pause");
    return 1;        // Attack success
}

bool swordsman::specialatt(player &p)
{
    if(MP<40)
    {
        cout<<"You don't have enough magic points!"<<endl;
        system("pause");
        return 0;        // Attack failed
    }
    else
    {
        MP-=40;            // consume 40 MP to do special attack
        
        //10% chance opponent evades
        if(rand()%100<=10)
        {
            cout<<name<<"'s leap attack has been evaded by "<<p.name<<endl;
            system("pause");
            return 1;
        }
        
        double HPtemp=0;        
        double EXPtemp=0;        
        //double hit=1;            
        //srand(time(NULL));        
        HPtemp=(int)(AP*1.2+20);        // not related to opponent's DP
        EXPtemp=(int)(HPtemp*1.5);        // special attack provides more experience
        cout<<name<<" uses leap attack, "<<p.name<<"'s HP decreases "<<HPtemp<<endl;
        cout<<name<<" obtained "<<EXPtemp<<" experience."<<endl;
        p.HP=(int)(p.HP-HPtemp);
        EXP=(int)(EXP+EXPtemp);
        system("pause");
    }
    return 1;    // special attack succeed
}

// Computer opponent
void swordsman::AI(player &p)
{
    if ((HP<(int)((1.0*p.AP/DP)*p.AP*1.5))&&(HP+100<=1.1*HPmax)&&(bag.nOfHeal()>0)&&(HP>(int)((1.0*p.AP/DP)*p.AP*0.5)))
        // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
    {
        useHeal();
    }
    else
    {
        if(MP>=40 && HP>0.5*HPmax && rand()%100<=30)
            // AI has enough MP, it has 30% to make special attack
        {
            specialatt(p);
            p.isDead();        // check whether player is dead
        }
        else
        {
            if (MP<40 && HP>0.5*HPmax && bag.nOfMW())
                // Not enough MP && HP is safe && still has magic water
            {
                useMW();
            }
            else
            {
                attack(p);    // normal attack
                p.isDead();
            }
        }
    }
}

container.h

//=======================
//        container.h
//=======================
#include<iostream>
using namespace std;
// The so-called inventory of a player in RPG games
// contains two items, heal and magic water

#ifndef _CONTAINER    // Conditional compilation
#define _CONTAINER

class container        // Inventory
{
protected:
    int numOfHeal;            // number of heal
    int numOfMW;            // number of magic water
public:
    container();            // constuctor
    void set(int heal_n, int mw_n);    // set the items numbers
    int nOfHeal();            // get the number of heal
    int nOfMW();            // get the number of magic water
    void display();            // display the items;
    bool useHeal();            // use heal
    bool useMW();            // use magic water
};

container.cpp

// default constructor initialise the inventory as empty
container::container()
{
    set(0,0);
}

// set the item numbers
void container::set(int heal_n, int mw_n)
{
    numOfHeal=heal_n;
    numOfMW=mw_n;
}

// get the number of heal
int container::nOfHeal()
{
    return numOfHeal;
}

// get the number of magic water
int container::nOfMW()
{
    return numOfMW;
}

// display the items;
void container::display()
{
    cout<<"Your bag contains: "<<endl;
    cout<<"Heal(HP+100): "<<numOfHeal<<endl;
    cout<<"Magic Water (MP+80): "<<numOfMW<<endl;
}

//use heal
bool container::useHeal()
{
    numOfHeal--;
    return 1;        // use heal successfully
}

//use magic water
bool container::useMW()
{
    numOfMW--;
    return 1;        // use magic water successfully
}
#endif

 

标签:string,void,player,实验,include,HP,name
From: https://www.cnblogs.com/oRIng/p/16918665.html

相关文章

  • 实验五
    1.task4.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings):nickname(s){}......
  • 【汇编语言】实验3 编程、编译、链接、跟踪
    【汇编语言】实验3编程、编译、链接、跟踪实验内容编写程序DEBUG程序查看初始状态和指令可以发现CS=DS+10H依次执行查看PSP内容首先回顾一下PSP是什么所以我们查看SA:0......
  • 【汇编语言】实验2 用机器指令和汇编指令编程
    ​【汇编语言】实验2用机器指令和汇编指令编程文章目录​​【汇编语言】实验2用机器指令和汇编指令编程​​​​一、预备知识,debug的使用​​​​debug中段寄存器使用​​......
  • 【汇编语言】实验1 查看CPU和内存,用机器指令和汇编指令编程
    ​【汇编语言】实验1查看CPU和内存,用机器指令和汇编指令编程文章目录​​【汇编语言】实验1查看CPU和内存,用机器指令和汇编指令编程​​​​一、配置环境​​​​二、熟悉......
  • Python第十章实验
    实例一:创建并打开记录蚂蚁庄园动态的文件实验代码:print("\n","="*10,"蚂蚁庄园动态","="*10)file=open('message.txt','w')print("\n即将显示……\n")实验结果:......
  • 实验五
     Pets.h#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststring&s=""):nickname(s)......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX一、实验目的能够理解POX控制器的工作原理;通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;够运......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......
  • 实验五:全连接神经网络手写数字
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架......
  • 实验3:OpenFlow协议分析实践
    一、实验目的能够运用wireshark对OpenFlow协议数据交互过程进行抓包;能够借助包解析工具,分析与解释OpenFlow协议的数据包交互过程与机制。二、实验环境Ubuntu20......