实验结论

宠物

源码

MachinePets.h
//
// Created by KOKODA on 2019/6/3.
//#ifndef SHIYAN5_MACHINEPETS_H
#define SHIYAN5_MACHINEPETS_H#include <string>using namespace std;class MachinePets {
protected:string nickname;
public:MachinePets(const string &nickname);virtual string talk()=0;
};#endif //SHIYAN5_MACHINEPETS_H
MachinePets.cpp
//
// Created by KOKODA on 2019/6/3.
//#include "MachinePets.h"MachinePets::MachinePets(const string &nickname) : nickname(nickname) {}
PetCats.h
//
// Created by KOKODA on 2019/6/3.
//#ifndef SHIYAN5_PETCATS_H
#define SHIYAN5_PETCATS_H#include "MachinePets.h"class PetCats: public MachinePets {
public:PetCats(const string &nickname);string talk();
};#endif //SHIYAN5_PETCATS_H
PetCats.cpp
//
// Created by KOKODA on 2019/6/3.
//#include "PetCats.h"PetCats::PetCats(const string &nickname) : MachinePets(nickname) {}string PetCats::talk() {return nickname+" says miao wu~";
}
PetDogs.h
//
// Created by KOKODA on 2019/6/3.
//#ifndef SHIYAN5_PETDOGS_H
#define SHIYAN5_PETDOGS_H#include "MachinePets.h"class PetDogs:public MachinePets{
public:PetDogs(const string &nickname);string talk();
};#endif //SHIYAN5_PETDOGS_H
PetDogs.cpp
//
// Created by KOKODA on 2019/6/3.
//#include "PetDogs.h"PetDogs::PetDogs(const string &nickname) : MachinePets(nickname) {}string PetDogs::talk() {return nickname+" says wang wang~";
}
main.cpp
#include <iostream>#include "PetCats.h"
#include "PetDogs.h"using namespace std;void play(MachinePets *p);int main() {PetCats cat("miku");PetDogs dog("da huang");play(&cat); // 按照play()形参,传递参数play(&dog); // 按照play()形参,传递参数return 0;
}void play(MachinePets *p) {cout<<(p->talk())<<endl;
}

运行截图

RPG游戏

源码

container.h
//=======================
//      container.h
//=======================// The so-called inventory of a player in RPG games
// contains two items, heal and magic water#ifndef _CONTAINER    // Conditional compilation
#define _CONTAINERclass container        // Inventory
{
protected:int numOfHeal;            // number of healint numOfMW;            // number of magic water
public:container();        // constuctorvoid set(int heal_n, int mw_n);    // set the items numbersint nOfHeal();    // get the number of healint nOfMW();            // get the number of magic watervoid display();            // display the items;bool useHeal();            // use healbool useMW();            // use magic water
};#endif
container.cpp
//=======================
//      container.cpp
//=======================// default constructor initialise the inventory as empty
#include "container.h"
#include <iostream>using namespace std;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
}
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 <string>
#include "container.h"using namespace std;enum job {sw, ar, mg
};    /* define 3 jobs by enumerate typesword 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 charactersstring name;    // character namejob role;        /* character's job, one of swordman, archer and mage,as defined by the enumerate type */container bag;    // character's inventorypublic:virtual bool attack(player &p) = 0;    // normal attackvirtual bool specialatt(player &p) = 0;    //special attackvirtual 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 resumebool death();        // report whether character is deadvoid isDead();        // check whether character is deadbool useHeal();        // consume heal, irrelevant to jobbool useMW();        // consume magic water, irrelevant to jobvoid transfer(player &p);    // possess opponent's items after victoryvoid showRole();    // display character's jobprivate:bool playerdeath;            // whether character is dead, doesn't need to be accessed or inherited
};#endif
player.cpp
//=======================
//      player.cpp
//=======================// character's HP and MP resume
#include "player.h"
#include <iostream>using namespace std;void player::reFill() {HP = HPmax;        // HP and MP fully recoveredMP = 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 valueHP = HPmax;        // so assign it to HPmax, if necessarycout << name << " used Heal, HP increased by 100." << endl;bag.useHeal();        // use healsystem("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() + p.bag.nOfHeal(), bag.nOfHeal() + p.bag.nOfHeal());// 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;
}
swordman.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 classThe definition of them will be given in this subclass. */void AI(player &p);                // Computer opponent
};
swordman.cpp
//=======================
//      swordsman.cpp
//=======================// constructor. default values don't need to be repeated here
#include "swordsman.h"
#include <iostream>using namespace std;swordsman::swordsman(int lv_in, string name_in) {role = sw;    // enumerate type of jobLV = lv_in;name = name_in;// Initialising the character's properties, based on his levelHPmax = 150 + 8 * (LV - 1);        // HP increases 8 point2 per levelHP = HPmax;MPmax = 75 + 2 * (LV - 1);        // MP increases 2 points per levelMP = MPmax;AP = 25 + 4 * (LV - 1);            // AP increases 4 points per levelDP = 25 + 4 * (LV - 1);            // DP increases 4 points per levelspeed = 25 + 2 * (LV - 1);        // speed increases 2 points per levelplayerdeath = 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 decrementdouble EXPtemp = 0;        // player obtained expdouble hit = 1;            // attach factor, probably give critical attacksrand((unsigned) time(NULL));        // generating random seed based on system time// If speed greater than opponent, you have some possibility to do double attackif ((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 chancecout << 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 evadeif ((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 attackif (rand() % 100 <= 10) {hit = 1.5;cout << "Critical attack: ";}// Normal attackHPtemp = (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 evadesif (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 DPEXPtemp = (int) (HPtemp * 1.5);        // special attack provides more experiencecout << 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 attackp.isDead();}}}
}

运行截图

转载于:https://www.cnblogs.com/KOKODA/p/10968928.html

实验5 类的继承、派生和多态相关推荐

  1. C++实验七——类的继承(1)

    实验报告 题目1 题目2 [实验名称] 实验七 类的继承(1) [实验内容] 题目1 以动物类为父类进行派生,设计可行的派生类,为派生类增加必要的成员,并对父类中的成员做适当调整,在主程序中对派生类的 ...

  2. 实验三 类的继承和多态性

    实验三 类的继承和多态性 1.(1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积.int getCircumference():获得图形的周长 ( ...

  3. C++实验八——类的继承(2)

    实验报告 题目1 题目2 [实验名称] 实验八 类的继承(2) [实验内容] 题目1 正确使用类的继承和组合进行类的设计,分别表示房间.休息室.教室.投影仪,沙发,为每个类设置适当的成员变量.成员函数 ...

  4. 类的继承关系,多态的体现,我的觉得题目还是有点欠缺

    ylbtech-.NET Framework: 类的继承关系,多态的体现,我的觉得题目还是有点欠缺 1.A,案例 类的继承关系,多态的体现,我的觉得题目还是有点欠缺.   1.B,解决方案 using ...

  5. 类的继承,派生,组合,菱形继承,多态与多态性

    类的继承 继承是一种新建类的方式,新建的类称为子类,被继承的类称为父类 继承的特性是:子类会遗传父类的属性 继承是类与类之间的关系 为什么用继承 使用继承可以减少代码的冗余 对象的继承 python中 ...

  6. java实验33 类的继承2_java实验2 类的继承性

    实验2 类的继承性 一.实验目的 掌握面向对象的继承性在Java中的实现方法:掌握super关键字的用法,体会抽象类和抽象方法的作用. 二.实验内容 1.程序理解: 1)类的继承 2)第4章课后编程第 ...

  7. java类接口实验_实验3_Java类的继承和接口的定义和使用

    本次有三题:学会Java类的继承.接口的定义和使用 // Ex3_1.java /** * 题目要求: * 修改例5.7(P95)实现在页面中拖动鼠标画出矩形,矩形的对角线为点击并拖动鼠标形成的直线线 ...

  8. 实验5 类的继承、派生和多态(2)

    1.设计并实现一个机器宠物类MachinePets. #include<iostream> #include<string> using namespace std; clas ...

  9. java人学生大学生类的继承,java实验报告7.doc

    java实验报告7.doc 实 验 报 告( 2014 / 2015学年 第2学期)课程名称JAVA程序设计实验名称 类的继承实验时间2015年4月30日指导单位计算机学院/软件学院软件工程系指导教师 ...

  10. Visual C++ 2008入门经典 第九章类的继承和虚函数

    // 第九章类的继承和虚函数 //.cpp: 主项目文件. //1 继承如何与面向对像的编程思想适应 //2 根据现有类定义新类 //3 使用protected关键字为类成员指定新的访问特性 //4 ...

最新文章

  1. 使用 IDEA 开发工具(版本为 IntelliJ IDEA 14.1.4)打可执行jar包的操作步骤
  2. 自考总结-2019-4-14
  3. iOS web与JS交互
  4. 使用过滤器监控网站流量
  5. Android 基础(二十四) EditText
  6. 传智C++课程笔记-1
  7. jvm默认的初始化参数_您是否应该信任JVM中的默认设置?
  8. mysql max嵌套select_使用嵌套select子式 解决mysql不能叠加使用如max(sum())的问题
  9. java 设置月份_java – 为什么Calendar.JUNE将月份设置为7月?
  10. 基于libmemcached为php扩展memcached服务
  11. c语言有结构体的200行代码,C语言——结构体(示例代码)
  12. emacs工程管理,cedet ede插件自动构建Make,Automake
  13. php pager,fleaphp常用方法分页之Pager使用方法
  14. 【免费】雪糕刺客小程序,天价雪糕查询表,简单小程序框架
  15. 全年日降雨数据下载与处理教程
  16. 高精度轻量级实时语义分割网络:2K视频分割可达24.3GFLOPS和36.5FPS
  17. vscode 移动到末尾并且换行快捷键
  18. 用批处理文件实现同步到个人时间服务器,局域网内时间同步net time的使用
  19. 【2022 CCF BDCI 文心大模型创意项目】DIY绘本
  20. 域渗透|NTLM 中继攻击

热门文章

  1. 斯坦福大学公开课 :Andrew Ng 机器学习课堂笔记之第一节(机器学习的动机与应用)
  2. poi根据模版导出多页word,带插入图片,并压缩下载
  3. Mac常用触摸板手势
  4. 我的世界正版租赁服务器,《我的世界》【PC版】租赁服务器正式版明日就要和大家见面啦~...
  5. Win10释放C盘空间的一些办法
  6. IBM内存三技术:Chipkill、MPX、MM
  7. android联想搜索不到wifi,联想笔记本ThinkPad E430 无法搜索到无线网络的解决办法...
  8. Hack the box靶机 Blunder
  9. stm32中断方式和DMA方式完成串口通信
  10. 计算机用word做贺卡,运用Word制作电子贺卡教学设计