欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

修仙类 小练习

程序员文章站 2022-04-05 17:09:04
...

修仙者类,灵石类,妖兽类。
修仙者之间,相互交易:
(买卖妖兽,得到相应灵石,交换妖兽,通过战斗力捕获妖兽)
(PK,生存者得到失败者,所有的财物,妖兽,灵石)
(修仙者进阶,成功等级+1,失败-1);

重载运算符

灵石类
SpriteStore.h

#pragma once
#include<string>
#include<fstream>

using namespace std;
//灵石等级
enum class SpriteStoneLevel {
 PRIMARY_LEVEL,
 MIDDLE_LEVEL,
 ADVANCE_LEVEL,
 SPRITE_STONE_LEVEL_COUNT
};

class SpriteStone {
public:
 SpriteStone(int count=0, SpriteStoneLevel level= SpriteStoneLevel::PRIMARY_LEVEL);
 string ret();
 friend ostream& operator<<(ostream &os, SpriteStone& stone);
 SpriteStone operator+(const SpriteStone& stone);
 SpriteStone operator-(const SpriteStone& stone);
 int getCount();
 bool operator>=(const SpriteStone& stone);
private:
 int count;
 SpriteStoneLevel level;
};
ostream& operator<<(ostream &os, SpriteStone& stone);

SpriteStore.cpp


#include<sstream>

SpriteStone::SpriteStone(int count, SpriteStoneLevel level)
{
 this->count = count;
 this->level = level;
}
string SpriteStone::ret()
{
 stringstream ret;
 ret << count << "块";
 switch (level)
 {
 case SpriteStoneLevel::PRIMARY_LEVEL:
  ret << "初级灵石";
  break;
 case SpriteStoneLevel::MIDDLE_LEVEL:
  ret << "中级灵石";
  break;
 case SpriteStoneLevel::ADVANCE_LEVEL:
  ret << "高级灵石";
  break;
 default:
  ret << "其他灵石";
  break;
 }
 return ret.str();
}

SpriteStone SpriteStone::operator+(const SpriteStone & stone)
{
 int sum = 0;
 if (stone.level == SpriteStoneLevel::PRIMARY_LEVEL) {
  sum += stone.count;
 }
 else if (stone.level == SpriteStoneLevel::MIDDLE_LEVEL) {
  sum += stone.count * 10;
 }
 else if (stone.level == SpriteStoneLevel::ADVANCE_LEVEL) {
  sum += stone.count * 100;
 }
 if (this->level == SpriteStoneLevel::PRIMARY_LEVEL) {
  sum += this->count;
 }
 else if (this->level == SpriteStoneLevel::MIDDLE_LEVEL) {
  sum += this->count * 10;
 }
 else if (this->level == SpriteStoneLevel::ADVANCE_LEVEL) {
  sum += this->count * 100;
 }
 return SpriteStone(sum, SpriteStoneLevel::PRIMARY_LEVEL);
}

SpriteStone SpriteStone::operator-(const SpriteStone & stone)
{
 int sum1 = 0;
 if (this->level == SpriteStoneLevel::PRIMARY_LEVEL) {
  sum1 += this->count;
 }
 else if (this->level == SpriteStoneLevel::MIDDLE_LEVEL) {
  sum1 += this->count * 10;
 }
 else if (this->level == SpriteStoneLevel::ADVANCE_LEVEL) {
  sum1 += this->count * 100;
 }
 int sum2 = 0;
 if (stone.level == SpriteStoneLevel::PRIMARY_LEVEL) {
  sum2 += stone.count;
 }
 else if (stone.level == SpriteStoneLevel::MIDDLE_LEVEL) {
  sum2 += stone.count * 10;
 }
 else if (stone.level == SpriteStoneLevel::ADVANCE_LEVEL) {
  sum2 += stone.count * 100;
 }
 return SpriteStone(sum1-sum2,SpriteStoneLevel::PRIMARY_LEVEL);
}

int SpriteStone::getCount()
{
 return count;
}

bool SpriteStone::operator>=(const SpriteStone & stone)
{
 int sum1 = 0;
 if (this->level == SpriteStoneLevel::PRIMARY_LEVEL) {
  sum1 = this->count;
 }
 else if (this->level == SpriteStoneLevel::MIDDLE_LEVEL) {
  sum1 = this->count * 10;
 }
 else if (this->level == SpriteStoneLevel::ADVANCE_LEVEL) {
  sum1 = this->count * 100;
 }
 int sum2 = 0;
 if (stone.level == SpriteStoneLevel::PRIMARY_LEVEL) {
  sum2 = stone.count;
 }
 else if (stone.level == SpriteStoneLevel::MIDDLE_LEVEL) {
  sum2 = stone.count * 10;
 }
 else if (stone.level == SpriteStoneLevel::ADVANCE_LEVEL) {
  sum2 = stone.count * 100;
 }
 return sum1 >= sum2;
}

ostream& operator<<(ostream &os, SpriteStone& stone) {
 os << stone.ret();
 return os;
}

妖兽类
Monster.h

#pragma once
#include<string>
#include<iostream>

class SpriteStone;
using namespace std;
//妖兽类
class Monster
{
public:
Monster(int level = 1,const string& category = "未知");
 SpriteStone getValue() const;

int getPower() const;//获得妖兽的战斗力

friend ostream& operator<<(ostream& os, const Monster& monster);
 
friend bool operator == (const Monster& one,
const Monster& other);
private:
 string category; //妖兽的种类
 int level;//1-9级
};
ostream& operator<<(ostream& os, const Monster& monster);
bool operator == (const Monster& one, const Monster& other);

Monster.cpp

#include "Monster.h"
#include"SpriteStore.h"
#define MONSTER_LEVEL_FACTOR 1000

Monster::Monster(int level, const string & category)
{
 this->level = level;
 this->category = category;
}

SpriteStone Monster::getValue() const 
{
 int stoneCount[] = { 100,200,500,1000,2000,5000,10000,20000,100000 };
 int count = stoneCount[level - 1];
 
 return SpriteStone(count,SpriteStoneLevel::PRIMARY_LEVEL);
}

int Monster::getPower() const
{
 int ret = level * MONSTER_LEVEL_FACTOR;
 return ret;
}

ostream & operator<<(ostream & os, const Monster & monster)
{
 os << monster.level << "级" << monster.category << "妖兽";
 return os;
}

bool operator ==(const Monster& one, const Monster& other) {
 if (one.category == other.category &&
  one.level == other.level) {
  return true;
 }
 else {
  return false;
 }
}

修仙者类
immortal.h

#pragma once
#include<string>
#include<vector>
#include<iostream>

class SpriteStone;
class Monster;

//修仙者级别
enum class ImmortalLevel {
 LIAN_QI,
 ZHU_JI,
 JIE_DAN,
 YUAN_YING,
 HUA_SHENG,
 LIAN_XU,
 HE_TI,
 DA_CHENG,
 DU_JIE
};
using namespace std;

//修仙者类
class Immortal
{
public:
 Immortal(const char *name,const char *menPai, ImmortalLevel level);
 //挖矿
 void mining();
 //卖所有妖兽
 bool trade();
 //卖指定妖兽
 bool trade(const Monster& monster);
 //用自己的灵石,购买其他修仙者的指定妖兽
 bool trade(Immortal& other, const Monster& monster);
 //用自己指定的妖兽,来和其他修仙者换。
 bool trade(const Monster& monsterSource, Immortal& other,
  const Monster& monsterDest);
 //把自己的妖兽,卖给其他修仙者,换灵石
 bool trade(const Monster& monster, Immortal& other);
 //修仙者比武
 void fight(Immortal& other);
 //修仙者决斗
 bool duel(Immortal& other);
 //进阶函数
 void advances_success();
 //降阶函数
 void Down_falst();
 //修仙者闭关修仙,进阶。
 void Cultivation();
 int getPower() const;
 //捕获妖兽
 void fight(const Monster& monster);
 friend ostream& operator<<(ostream& os, const Immortal& immortal);
 void dead();//修仙者死亡后的处理
private:
 string name;
 string menPai; //门派
 ImmortalLevel level;//修仙者级别
 vector<SpriteStone> stones;//灵石资产
 vector<Monster> monsters;  //妖兽资产
 bool alive;//生死状态
 bool hadMonster(const Monster& monster);//判断是否有指定妖兽
 bool removeMonster(const Monster& monster);//移除指定妖兽
};
ostream& operator<<(ostream& os, const Immortal& immortal);
ostream& operator<<(ostream& os, const ImmortalLevel level);

immortal.cpp


#include<time.h>
#include "immortal.h"
#include"SpriteStore.h"
#include"Monster.h"

#define IMMORTAL_LEVEL_FACTOR 1000
#define SPRITE_FACTOR         0.1
#define MONSTER_FACTOR        0.1

Immortal::Immortal(const char * name, const char * menPai, ImmortalLevel level)
{
 this->name = name;
 this->menPai = menPai;
 this->level = level;
 this->alive = true;
}

void Immortal::mining()
{
 stones.push_back(SpriteStone(100, SpriteStoneLevel::PRIMARY_LEVEL));
}

bool Immortal::trade()
{
 if (!alive) {
  return false;
 }
 SpriteStone stone;
 for (int i = 0;i < monsters.size();i++) {
  stone = stone + monsters[i].getValue();
 } stones.push_back(stone);
 //擦除妖兽
 monsters.erase(monsters.begin(),monsters.end());
 return false;
}

bool Immortal::trade(const Monster & monster)
{
 if (!alive) {
  return false;
 }
 //判断是否有指定的妖兽
 if (!hadMonster(monster)) {
  cout << name << "没有" << monster << endl;
  return false;
 }
  SpriteStone stone = monster.getValue();
 stones.push_back(stone);
 removeMonster(monster);
 return false;
}
bool Immortal::trade(Immortal & other, const Monster & monster)
{
 if (alive == false || other.alive == false) {
  return false;
 }
 if (!other.hadMonster(monster)) {
  cout << other.name << "没有" << monster << endl;
  return false;
 }
 //计算当前所有灵石总价
 SpriteStone stone;
 for (int i = 0;i < stones.size();i++) {
  stone = stone + stones[i];
 }
 if (stone >= monster.getValue()) {
  //购买
  SpriteStone valueStone = monster.getValue();
  stone = stone - valueStone;
  stones.clear();//清空袋子
  stones.push_back(stone);
  monsters.push_back(monster);
  other.removeMonster(monster);
  other.stones.push_back(valueStone);
  return true;
 }
 else {
  cout << "抱歉!" << name << "的灵石不够买" << monster << endl;
  return false;
 }
}

bool Immortal::trade(const Monster & monsterSource, Immortal & other, const Monster & monsterDest)
{
 if (alive == false || other.alive == false) {
  return false;
 }
 if(monsterSource == monsterDest || 
  !hadMonster(monsterSource)||
  !other.hadMonster(monsterDest)||
  !(monsterSource.getValue()>=monsterDest.getValue())){
  return false;
 }
 removeMonster(monsterSource);
 other.removeMonster(monsterDest);
 monsters.push_back(monsterDest);
 other.monsters.push_back(monsterSource);
 return true;
}

bool Immortal::trade(const Monster & monster, Immortal & other)
{
 if (alive == false || other.alive == false) {
  return false;
 }
 if (!hadMonster(monster)) {
  cout << name << "没有" << monster << endl;
 }
 SpriteStone otherStone;
 for (int i = 0;i < other.stones.size();i++) {
  otherStone = otherStone + other.stones[i];
 }
 if (!(otherStone >= monster.getValue())) {
  return false;
 }
 //对方出钱
 otherStone = otherStone - monster.getValue();
 other.stones.clear();
 other.stones.push_back(otherStone);
 //对方收货
 other.monsters.push_back(monster);
 //自己移除妖兽,收钱
 this->removeMonster(monster);
 this->stones.push_back(monster.getValue());
 return true;
}

void Immortal::fight(Immortal & other)
{
 if (alive == false || other.alive == false) {
  cout << "修仙者以亡,无法开始切磋!";
  exit(0);
 }
 cout << this->name << "和" << other.name << "比武中...   ";
 if (this->getPower() > other.getPower()) {
  cout << this->name << "的武力更加深厚" << endl;
 }
 else if (this->getPower() < other.getPower()) {
  cout << other.name << "的武力更加深厚" << endl;
 }
 else {
  cout << this->name << other.name <<"的武力相当" << endl;
 }
}

bool Immortal::duel(Immortal & other)
{
 if (alive == false || other.alive == false) {
  cout << "修仙者以亡,无法开始决斗!";
  return false;
 } 
 cout << this->name << "和" << other.name << "决斗中...   ";
 if (this->getPower() > other.getPower()) {
  cout << this->name << "杀死了!" << other.name<<
   "——并收刮了" << other.name << "所有宝物!" << endl;
  //计算死亡者灵石,加入胜利方
  SpriteStone stone;
  for (int i = 0;i < other.stones.size();i++) {
   this->stones.push_back(stones[i]);
  }
  //计算死亡者妖兽,加入胜利方
  Monster monster;
  for (int i = 0;i < other.monsters.size();i++) {
   monsters.push_back(monsters[i]);
  }
        //死掉
  other.dead();
 }
 else if (this->getPower() < other.getPower()) {
  cout << other.name << "杀死了!" << this->name <<
   "    并收刮了"<< this->name <<"所有宝物!"<<endl;
  //计算死亡者灵石,加入胜利方
  SpriteStone stone;
  for (int i = 0;i < stones.size();i++) {
   other.stones.push_back(stones[i]);
  }
  //计算死亡者妖兽,加入胜利方
  Monster monster;
  for (int i = 0;i < monsters.size();i++) {
   other.monsters.push_back(monsters[i]);
  }
  //死掉
  this->dead();
 }
 else {
  cout << this->name << other.name << "的武力相当,隔日在战!"<< endl;
 }
 return true;
}

void Immortal::Cultivation()
{
 //初级进阶
 cout <<this->name<<"在闭关修炼中"<< endl;
 if ((int)(this->level) <= 2) {
  cout << "初级进阶成功率为%80,需要消耗100灵石,祝您进阶成功!";
  srand((unsigned)time(NULL));
  if (rand()%10 <= 8) {
   advances_success();
   cout << "恭喜您进阶成功!等级加 +1" << endl;
  }else {
   cout << "进阶失败! 等级减 -1 " << endl;
   Down_falst();
  }
  SpriteStone stone;
  for (int i = 0;i < stones.size();i++) {
   stone = stone + stones[i];
  }
  stones.clear();
  stone = stone - 100;
  stones.push_back(stone);
 }
  //中级进阶
 if (2<(int)(this->level)&& (int)(this->level)<=6) {
  cout << "中级进阶成功率为%50,需要消耗200灵石,祝您进阶成功!";
  if (rand()%10 <= 4) {
   advances_success();
   cout << "恭喜您进阶成功!等级加 +1" << endl;
  }
  else {
   cout << "进阶失败! 等级减 -1 " << endl;
    Down_falst();
  }
  SpriteStone stone;
  for (int i = 0;i < stones.size();i++) {
   stone = stone + stones[i];
  }
  stones.clear();
  stone = stone - 200;
  stones.push_back(stone);
 }
 //高级进阶
 if ((int)(this->level)>6&& (int)(this->level)<9) {
  cout << "高级进阶成功率为%30,需要消耗300灵石,祝您进阶成功!";
  if (rand() % 10 <= 2) {
   void advances_success();
   cout << "恭喜您进阶成功!等级加 +1" << endl;
  }
  else {
   cout << "进阶失败! 等级减 -1" << endl;
      Down_falst();
  }
  cout << (int)(this->level) << endl;
  SpriteStone stone;
  for (int i = 0;i < stones.size();i++) {
   stone = stone + stones[i];
  }
  stones.clear();
  stone = stone - 300;
  stones.push_back(stone);
 }
}

void Immortal::advances_success()
{
 switch (this->level)
 {
 case ImmortalLevel::LIAN_QI:
   this->level = ImmortalLevel::ZHU_JI;
   break;
 case ImmortalLevel::ZHU_JI:
  this->level = ImmortalLevel::JIE_DAN;
  break;
 case ImmortalLevel::JIE_DAN:
  this->level = ImmortalLevel::YUAN_YING;
  break;
 case ImmortalLevel::YUAN_YING:
  this->level = ImmortalLevel::HUA_SHENG;
  break;
 case ImmortalLevel::HUA_SHENG:
  this->level = ImmortalLevel::LIAN_XU;
  break;
 case ImmortalLevel::LIAN_XU:
  this->level = ImmortalLevel::HE_TI;
  break;
 case ImmortalLevel::HE_TI:
  this->level = ImmortalLevel::DA_CHENG;
  break;
 case ImmortalLevel::DA_CHENG:
  this->level = ImmortalLevel::DU_JIE;
  break;
 }
}

void Immortal::Down_falst()
{
 switch (this->level)
 {
 case ImmortalLevel::DU_JIE:
  this->level = ImmortalLevel::DA_CHENG;
  break;
 case ImmortalLevel::DA_CHENG:
  this->level = ImmortalLevel::HE_TI;
  break;
 case ImmortalLevel::HE_TI:
  this->level = ImmortalLevel::LIAN_XU;
  break;
 case ImmortalLevel::LIAN_XU:
  this->level = ImmortalLevel::HUA_SHENG;
  break;
 case ImmortalLevel::HUA_SHENG:
  this->level = ImmortalLevel::YUAN_YING;
  break;
 case ImmortalLevel::YUAN_YING:
  this->level = ImmortalLevel::JIE_DAN;
  break;
 case ImmortalLevel::JIE_DAN:
  this->level = ImmortalLevel::ZHU_JI;
  break;
 case ImmortalLevel::ZHU_JI:
  this->level = ImmortalLevel::LIAN_QI;
  break;
 }
}

int Immortal::getPower() const
{
 //计算该级别的战斗力
 int ret = ((int)level + 1)* IMMORTAL_LEVEL_FACTOR;
 //计算灵石助攻的战斗力
 //
 SpriteStone stone;
 for (int i = 0;i < stones.size();i++) {
  stone = stone + stones[i];
 }
 ret += stone.getCount()* SPRITE_FACTOR;
 //计算所有妖兽的助攻
 for (int i = 0;i < monsters.size();i++) {
  ret += monsters[i].getPower() * MONSTER_FACTOR;
 }
 return ret;
}

void Immortal::fight(const Monster & monster)
{
 int selfPower = getPower();
 int monsterPower = monster.getPower();
 if (selfPower > monsterPower) {
  monsters.push_back(monster);
 }else if(selfPower < monsterPower){
  dead();
 }
}

void Immortal::dead()
{
 alive = false;
 stones.erase(stones.begin(), stones.end());
 monsters.erase(monsters.begin(),monsters.end());
}

bool Immortal::hadMonster(const Monster & monster)
{
 for (int i = 0;i < monsters.size();i++) {
  if (monster == monsters[i]) {
   return true;
  }
 }
 return false;
}

bool Immortal::removeMonster(const Monster & monster)
{
 //迭代器 特殊指针
 vector<Monster>::iterator it = monsters.begin();
 while (it != monsters.end()) {
  if (*it == monster) {
   it = monsters.erase(it);//移除指定妖兽 返回下一个妖兽
   return true;
  }
  else {
   it++;      //迭代器 自加
  }
 }
 return true;
}

ostream & operator<<(ostream & os, const Immortal & immortal)
{
 os << "[修仙者]" << immortal.name << (immortal.alive ? "[在修]" : "[以亡]")
  << "\t门派:" << immortal.menPai
  << "\t级别:" << immortal.level;
 SpriteStone stone;
 for (int i = 0;i < immortal.stones.size();i++) {
  stone = stone + immortal.stones[i];
 }
 os << "\t灵石:折合" << stone;
 os << "\t妖兽:";
 if (immortal.monsters.size() == 0) {
  os << "无";
 }
 else {
  for (int i = 0;i < immortal.monsters.size();i++) {
   os << immortal.monsters[i] << " ";
  }
 }
 return os;
}

ostream & operator<<(ostream & os, const ImmortalLevel level)
{
 switch (level)
 {
 case ImmortalLevel::LIAN_QI:
  os << "练气期";
  break;
 case ImmortalLevel::ZHU_JI:
  os << "筑基期";
  break;
 case ImmortalLevel::JIE_DAN:
  os << "结丹期";
  break;
 case ImmortalLevel::YUAN_YING:
  os << "元婴期";
  break;
 case ImmortalLevel::HUA_SHENG:
  os << "化神期";
  break;
 case ImmortalLevel::LIAN_XU:
  os << "炼虚期";
  break;
 case ImmortalLevel::HE_TI:
  os << "合体期";
  break;
 case ImmortalLevel::DA_CHENG:
  os << "大乘期";
  break;
 case ImmortalLevel::DU_JIE:
  os << "渡劫期";
  break;
 }
 return os;
}

main.cpp
测试

#include "SpriteStore.h"
#include"Monster.h"
#include"immortal.h"
#include<iostream>

void testSpriteStore() {
 SpriteStone stone(100, SpriteStoneLevel::ADVANCE_LEVEL);
 cout << stone << endl;
}

void testMonster() {
 Monster monster(5, "蛟龙");
 monster.getValue();
 cout << monster << endl;
}

void testImmortal() {
 Immortal rock("Rock", "奇牛", ImmortalLevel::LIAN_QI);
 cout << rock << endl;
 for (int i = 0;i < 50;i++) {
  rock.mining();
 }
 cout << rock << endl;
 Monster monster(1, "蛟龙");
 rock.fight(monster);
 cout << "捕获" << monster << "之后: "<<endl;
 cout << rock << endl;
 
 Monster monster2(1, "蜘蛛");
 rock.fight(monster2);
 Monster monster3(1, "壁虎");
 rock.fight(monster3);
 cout << "捕获:" << monster2 << monster3 <<"之后"<<endl;
 cout << rock << endl;
 rock.trade(monster2);
 cout << "售卖" << monster2 << "之后" << endl;
 cout << rock << endl;
 rock.trade();
 cout << "售卖所有妖兽之后" << endl;
 cout << rock << endl;
 
 Immortal hanni("韩立", "黄枫谷", ImmortalLevel::JIE_DAN);
 cout << hanni << endl;

Monster monster4(2,"蛟龙");
 hanni.fight(monster4);
 cout << hanni << endl;
 //买妖兽
 rock.trade(hanni, monster4);
 cout << "贸易之后" << endl;
 cout << rock << endl;
 cout << hanni << endl;
 //捕获妖兽
 Monster monster5(1, "天蚕");
 hanni.fight(monster5);
 cout << "韩立捕获了" << monster5 << endl;
 cout << hanni << endl;
 //换妖兽
 cout << "Rock使用" << monster4 << "和韩立交换" << monster5 << endl;
 rock.trade(monster4, hanni, monster5);
 cout << rock << endl;
 cout << hanni << endl;
 //卖妖兽
 hanni.trade(monster4, rock);
 cout << "韩立卖" << monster4 << "给Rock" << endl;
 cout << rock << endl;
 cout << hanni << endl;
 //友谊比武
 rock.fight(hanni);
 //生死决斗
 rock.duel(hanni);
 cout << rock << endl;
 cout << hanni << endl;
 //进阶
 hanni.Cultivation();
 cout << hanni << endl;
}

int main(void) {
 testSpriteStore();
 testMonster();
 testImmortal();
 system("pause");
}

运行结果:
修仙类 小练习

相关标签: 笔记