凡人修仙传(利用重载)

简介: 凡人修仙传(利用重载)

Immortal.h

#pragma once
#include <string>
#include <vector>
#include "SpriteStone.h"
#include "Monster.h"
#include <iostream>
using namespace std;
typedef enum
{
  LIAN_QI,
  ZHU_JI,
  JIE_DAN,
  YUAN_YING,
  HUA_SHEN,
  LIAN_XU,
  HE_TI,
  DA_CHENG,
  DU_JIE
}ImmortalLevel;
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);
  //获取攻击力
  int getPower() const;
  //修仙者死亡后的处理
  void dead();
  //捕获妖兽
  void fight(const Monster& monster);
  friend ostream& operator<<(ostream& os, const Immortal& immortal);
  friend ostream& operator<<(ostream& os,  ImmortalLevel level);
private:
  string name;
  string menPai;
  vector<SpriteStone> stones; //拥有灵石数量
  vector<Monster> monsters; //拥有妖兽数量
  bool alive = true;
  ImmortalLevel level;
  //内部判断是否自己有指定的妖兽
  bool hadMonster(const Monster& monster);
  //移除指定的妖兽
  bool removeMonster(const Monster& monster);
};
ostream& operator<<(ostream & os,  ImmortalLevel level);
ostream& operator<<(ostream& os, const Immortal& immortal);

Immortal.cpp

#include "Immortal.h"
#include "SpriteStone.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;
}
void Immortal::mining()
{
  stones.push_back(SpriteStone(100, SpriteStoneLevel::PRIMARY_LEVEL));
}
bool Immortal::trade()
{
  if (alive == false)
  {
    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))
  {
    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;
  }
  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 false;
}
bool Immortal::trade(const Monster& monster, Immortal& other)
{
  if (alive == false || other.alive == false)
  {
    return false;
  }
  if (!hadMonster(monster))
  {
    cout << name << "没有" << monster << endl;
    return false;
  }
  SpriteStone otherStone;
  for (int i = 0; i < other.stones.size(); i++)
  {
    otherStone = otherStone + other.stones[i];
  }
  if (otherStone >= monster.getValue())
  {
    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;
  }
  return false;
}
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 = ret + stone.getCount() * SPRITE_FACTOR;
  for (int i = 0; i < monsters.size(); i++)
  {
    ret = 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)
{
  //定义一个迭代器(是一种特殊的指针,指向monsters中的第一个成员
  vector<Monster>::iterator it = monsters.begin();
  //vector<Monster>::iterator it = monsters.begin();
  while (it != monsters.end())
  {
    if (*it == monster)
    {
      it = monsters.erase(it);
      return true;
    }
    else
    {
      it++;
    }
  }
  return false;
}
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 << "\t妖兽数量:";
    if (immortal.monsters.size() == 0)
    {
      os << "无";
    }
    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_SHEN:
    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;
}

Monster.h

#pragma once
#include <string>
#include "SpriteStone.h"
using namespace std;
class Monster
{
public:
  Monster(int level = 0,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:
  int level;
  string category;
};
ostream& operator<<(ostream& os, const Monster& monster);
bool operator==(const Monster& one, const Monster& other);

Monster.cpp

#include "Monster.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,10000,20000,100000 };
  int len = stoneCount[level - 1];
  return SpriteStone(len, SpriteStoneLevel::PRIMARY_LEVEL);
}
int Monster::getPower() const
{
  int ret = MONSTER_LEVEL_FACTOR * level;
  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;
  }
}

SpriteStone.h

#pragma once
#include <string>
#include "SpriteStone.h"
using namespace std;
class Monster
{
public:
  Monster(int level = 0,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:
  int level;
  string category;
};
ostream& operator<<(ostream& os, const Monster& monster);
bool operator==(const Monster& one, const Monster& other);

Monster.cpp

#include "Monster.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,10000,20000,100000 };
  int len = stoneCount[level - 1];
  return SpriteStone(len, SpriteStoneLevel::PRIMARY_LEVEL);
}
int Monster::getPower() const
{
  int ret = MONSTER_LEVEL_FACTOR * level;
  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;
  }
}
相关文章
|
存储 编译器 C++
【C++从0到王者】第三站:类和对象(中)拷贝构造函数
【C++从0到王者】第三站:类和对象(中)拷贝构造函数
55 0
|
存储 Java 编译器
【C++杂货铺】运算符重载(二)
【C++杂货铺】运算符重载(二)
100 1
|
6月前
|
编译器 C++
【C++从练气到飞升】03---构造函数和析构函数
【C++从练气到飞升】03---构造函数和析构函数
|
编译器 C++
【C++从0到王者】第三站:类和对象(中)赋值运算符重载
【C++从0到王者】第三站:类和对象(中)赋值运算符重载
51 0
|
搜索推荐 编译器 C++
【C++从0到王者】第三站:类和对象(中)构造函数与析构函数
【C++从0到王者】第三站:类和对象(中)构造函数与析构函数
40 0
|
6月前
|
算法 编译器 程序员
成为C++重载大师:深入理解重载决议
成为C++重载大师:深入理解重载决议
78 0
|
编译器 Linux C语言
【C++精华铺】2.C++入门 缺省参数、函数重载
含有缺省参数的函数在声明的时候要注意声明的时候定义和声明如果是分开的话缺省参数必须写在声明里。
|
存储 编译器 C++
【C++从0到王者】第二十五站:多继承的虚表
【C++从0到王者】第二十五站:多继承的虚表
44 0
【C++从0到王者】第二十五站:多继承的虚表
|
编译器 C++
【C++从0到王者】第五站:类和对象(中)const和取地址运算符重载
【C++从0到王者】第五站:类和对象(中)const和取地址运算符重载
65 0
|
编译器 C++
【C++杂货铺】运算符重载(一)
【C++杂货铺】运算符重载(一)
62 0