首页 > 其他分享 >实验五

实验五

时间:2022-11-29 15:24:57浏览次数:30  
标签:int void character player 实验 MP HP

#include<iostream>
#include<string>
using namespace std;
class MachinePets {
   public:
       MachinePets( string s=" ") { nickname = s; }
       virtual string talk() = 0;
       string get_nickname() { return nickname; }
private:
       string nickname;
};
class PetCats :public MachinePets {
   public:
       PetCats(string a) : MachinePets(a) {};

       string talk() { return "miao wu~"; }
};
class PetDogs :public MachinePets {
public:
    PetDogs(string b) :MachinePets(b) {};
    string talk() { return "wang wang~"; }
};



void play(MachinePets& obj) {
    std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl;
}
void test() {
    PetCats cat("mao");
    PetDogs dog("dog");
    play(cat);
    play(dog);
}
int main() {
    test();
}

#pragma once
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Person {
  public:
      Person(){}
      Person(string e , string n, string t=" ") { name = n; telephone = t; email = e; }
      Person(const Person& str) {
          name = str.name;
          telephone = str.telephone;
          email = str.email;
      }
     
      void update_telephone() { cout << "Enter the telepher the email adresss:"; cin.clear(); cin >> telephone;cout << "telephone number has been updated..." << endl; }
      void update_email() { cout << "Enter teh eamil adress:"; cin.clear(); cin >> email;cout<<"email adress has been updated..." << endl; }
      string ret_name() { return name; }
      string ret_telephone() { return telephone; }
      string ret_email() { return email; }

    /*  friend std::ostream& operator<<(std::ostream& out, Person & p)
      {
          out << left << setw(10) << p.name;
          out << left << setw(10) << p.telephone;
             out << left << setw(10) << p.email;
             out << endl;
          return out;
      }*/friend std::ostream& operator<<(std::ostream& out, const Person& c) {
          out << left << setw(20) << c.name;
          out << left << setw(20) << c.telephone;
          out << left << setw(20) << c.email;
          out << endl;
          return out;
      }

      friend std::istream & operator>>(std::istream &in,  Person& p)
      {
          in >> p.name >> p.email >> p.telephone;
          return in;
      }


      friend bool operator==( Person& q,  Person& p)
      {
          if ((p.ret_telephone() == q.ret_telephone()) && (p.ret_email() == q.ret_email()))
              return true;
          else
              return false;
      }
private:
    string name,telephone,email;

};
#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);
    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();
}

 

//=======================
//        container.h
//=======================

// 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
};

#endif
//=======================
//        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
}
#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
};

#endif
//=======================
//        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");
HP+=p.bag.nOfHeal();MP+=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;
}
//=======================
//        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");
HP+=p.bag.nOfHeal();MP+=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;
}
//=======================
//        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
};
//=======================
//        main.cpp
//=======================

// main function for the RPG style game

#include <iostream>
#include <string>
using namespace std;

#include "swordsman.h"


int main()
{
    string tempName;
    bool success=0;        //flag for storing whether operation is successful
    cout <<"Please input player's name: ";
    cin >>tempName;        // get player's name from keyboard input
    player *human;        // use pointer of base class, convenience for polymorphism
    int tempJob;        // temp choice for job selection
    do
    {
        cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl;
        cin>>tempJob;
        system("cls");        // clear the screen
        switch(tempJob)
        {
        case 1:
            human=new swordsman(1,tempName);    // create the character with user inputted name and job
            success=1;        // operation succeed
            break;
        default:
            break;                // In this case, success=0, character creation failed
        }
    }while(success!=1);        // so the loop will ask user to re-create a character

    int tempCom;            // temp command inputted by user
    int nOpp=0;                // the Nth opponent
    for(int i=1;nOpp<5;i+=2)    // i is opponent's level
    {
        nOpp++;
        system("cls");
        cout<<"STAGE" <<nOpp<<endl;
        cout<<"Your opponent, a Level "<<i<<" Swordsman."<<endl;
        system("pause");
        swordsman enemy(i, "Warrior");    // Initialise an opponent, level i, name "Junior"
        human->reFill();                // get HP/MP refill before start fight
        
        while(!human->death() && !enemy.death())    // no died
        {
            success=0;
            while (success!=1)
            {
                showinfo(*human,enemy);                // show fighter's information
                cout<<"Please give command: "<<endl;
                cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl;
                cin>>tempCom;
                switch(tempCom)
                {
                case 0:
                    cout<<"Are you sure to exit? Y/N"<<endl;
                    char temp;
                    cin>>temp;
                    if(temp=='Y'||temp=='y')
                        return 0;
                    else
                        break;
                case 1:
                    success=human->attack(enemy);
                    human->isLevelUp();
                    enemy.isDead();
                    break;
                case 2:
                    success=human->specialatt(enemy);
                    human->isLevelUp();
                    enemy.isDead();
                    break;
                case 3:
                    success=human->useHeal();
                    break;
                case 4:
                    success=human->useMW();
                    break;
                default:
                    break;
                }
            }
            if(!enemy.death())        // If AI still alive
                enemy.AI(*human);
            else                            // AI died
            {
                cout<<"YOU WIN"<<endl;
                human->transfer(enemy);        // player got all AI's items
            }
            if (human->death())
            {
                system("cls");
                cout<<endl<<setw(50)<<"GAME OVER"<<endl;
            delete human;        // player is dead, program is getting to its end, what should we do here?
                system("pause");
                return 0;
            }
        }
    }
delete human;        // You win, program is getting to its end, what should we do here?
    system("cls");
    cout<<"Congratulations! You defeated all opponents!!"<<endl;
    system("pause");
    return 0;
}
        

 

标签:int,void,character,player,实验,MP,HP
From: https://www.cnblogs.com/jh025/p/16935452.html

相关文章

  • 汇编实验:基于BIOS调用(10H)的多窗口输出程序
    汇编实验报告-屏幕窗口程序实验1.题目要求:自行编写一个键盘输入并且在屏幕输出的程序,它可以完成键盘读入并且在屏幕显示出来。具体要求:2.运行环境:Windows11+MASM3.......
  • 汇编实验:电话簿(1)实现一个电话簿
    汇编实验报告-电话簿1.题目要求:编写一个电话簿,要求至少有三条以上信息,每条信息包括人名与电话号码.对于改电话簿用户可以自己初始化信息,并且根据姓名查找对应的电话号码......
  • 汇编实验:电话簿(2)电话簿的增删改查操作
    汇编实验报告-电话簿1.题目要求:对上周写的电话簿增加对通讯录的增加、删除、更改操作。2.运行环境:Windows11+MASM3.题目分析:题目要求我们在上一周电话簿的基础上,增......
  • 实验五:全连接神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框架,......
  • 实验五 多态和继承
    实验内容:1.1.MachinePets.hpp:#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(){}MachinePets(cons......
  • 实验五:全连接神经网络手写数字识别实验
    博客班级班级链接作业要求作业链接学号181613146【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全......
  • 实验五:全连接神经网络手写数字识别实验
    【实验目的】理解神经网络原理,掌握神经网络前向推理和后向传播方法;掌握使用pytorch框架训练和推理全连接神经网络模型的编程实现方法。【实验内容】1.使用pytorch框......
  • STM32f103Zet6 跑马灯实验
    一、硬件  LED0(DS0)和LED1(DS1)分别接在PB5和PE5上,低电平LED亮。 在CubeMX中,将PB5,PE5设为GPIO_Output. 二、软件HAL库:HAL_GPIO_WritePin(GPIOB,G......
  • AWS上DevOps实验(二)--- 使用Terraform创建VPC网络
    从本文档起,作者计划在AWS上做一系列DevOps/IaC相关实验,本文是第二篇,使用Terraform创建VPC网络。本次实验架构图Terraform代码执行主文件main.tf#terraformcodetod......
  • 实验5 继承和多态
    实验任务四:pets.hpp:#pragmaonce#include<iostream>#include<string>std::string;usingnamespacestd;classMachinePets{public:MachinePets(constst......