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

修仙游戏(重载运算符练习)

程序员文章站 2024-03-15 17:45:36
...

某仙侠游戏中,有以下需求:


这个游戏世界中,有妖兽,灵石,修仙者

妖兽有自己的种类。
妖兽分为不同的等级:
1级妖兽:价值 100:初阶灵石
2级妖兽:价值 200初阶灵石
3级妖兽:价值 500初阶灵石
4级妖兽:价值 1000初阶灵石
5级妖兽:价值 2000初阶灵石
6级妖兽:价值 5000初阶灵石
7级妖兽:价值 10000初阶灵石
8级妖兽:价值 20000初阶灵石
9级妖兽:价值 100000初阶灵石

灵石:是一种类似黄金的硬货币。
灵石有级别:
初阶灵石:
1中阶灵石: 10初阶灵石
1高阶灵石: 10中阶灵石

修仙者
姓名
门派:(黄枫谷,鬼灵门,落云宗,掩月宗,古剑门,御灵宗, 散修)
级别(练气期, 筑基期, 结丹期,元婴期, 化神期,炼虚期,合体期,大成期,渡劫期)
资产(不定数量的妖兽,不定数量的灵石)
状态:生,死

1.修仙者之间常常进行贸易活动,用灵石购买妖兽,或者用妖兽换取灵石。
2.修仙者可以通过采矿来获取灵石,每次只能获取100个初阶灵石。
3.修仙者可以捕获妖兽,能否捕获妖兽,取决于自己的战斗力是否大于妖兽
4.修仙者之间可以进行切磋,实力更强大的胜利。
5.修仙者之间还可能进行争斗,实力更强大的生,另一方亡,生者夺取亡者的所有资产。
6.修仙者闭关进阶,自己的级别加1


项目实现:


首先定义灵石类:作为一个基本类,根据需求有高级,中级,初级,因此
定义了一个c++类型的枚举.
头文件:`:

#pragma once
#include<string>
#include<iostream>
using namespace std;
enum class SpriteStoneLevel { //统计灵石的数量
	PRIMARY_LEVEL,
	MIDDLE_LEVEL,
	ADVANCED_LEVEL,
	SPRITESTONE_LEVEL_COUNT
};
class SpriteStone
{
public:
	SpriteStone(int count = 0, 
		SpriteStoneLevel level = SpriteStoneLevel::PRIMARY_LEVEL);
	string str()const ;
	friend ostream& operator<<(ostream& os, const  SpriteStone &stone);
	SpriteStone operator+(const SpriteStone& stone);
	int getcount()const;
	bool operator>=(const SpriteStone& stone);
	SpriteStone operator-(const SpriteStone& stone);
	bool operator==(const SpriteStone& stone);
private:
	int count;//灵石的块数
	SpriteStoneLevel level;
};
ostream& operator<<(ostream&os,const  SpriteStone &stone);

源文件:

#include "SpriteStone.h"
#include<iostream>
#include<sstream>
SpriteStone::SpriteStone(int count, SpriteStoneLevel level)
{
	this->count = count;
	this->level = level;
}
string SpriteStone::str()const
{
	stringstream ret;
	ret<<count<<"块";
	switch (level)
	{
	case SpriteStoneLevel::PRIMARY_LEVEL:
		ret << "初级灵石" << endl;
		break;
	case SpriteStoneLevel::MIDDLE_LEVEL:
		ret << "中级灵石" << endl;
		break;
	case SpriteStoneLevel::ADVANCED_LEVEL:
		ret << "高级灵石" << endl;
		break;
	default:
		ret << "未知灵石" << endl;
		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::ADVANCED_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::ADVANCED_LEVEL) {
		sum += this->count * 100;
	}
	return SpriteStone(sum, SpriteStoneLevel::PRIMARY_LEVEL);
}
int SpriteStone::getcount() const
{
	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::ADVANCED_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::ADVANCED_LEVEL) {
		sum2 += stone.count * 100;
	}
	return sum1>=sum2;
}
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::ADVANCED_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::ADVANCED_LEVEL) {
		sum2 += stone.count * 100;
	}
	return SpriteStone(sum1-sum2,SpriteStoneLevel::PRIMARY_LEVEL);
}
bool SpriteStone::operator==(const SpriteStone& stone)
{
	if (this->count == stone.count && this->level == stone.level) {
		return true;
	}
	else {
		return false;
	}
}
ostream& operator<<(ostream& os, const SpriteStone &stone)
{
	os << stone.str();
	return os;
}

再定义灵兽:灵兽有等级和名称
头文件:

#pragma once
#include<string>
#include<iostream>
using namespace std;
class SpriteStone;
class Monster
{
public:
	Monster(int level = 0,const string &category ="未知魂兽");
	SpriteStone getvalue()const ;
	friend ostream& operator<<(ostream& os, const  Monster& monster);
	friend bool operator==(const Monster& monster, const Monster& other);
	int getpower()const;
private:
	int level;
	string category;
};
ostream& operator<<(ostream& os,const Monster& monster);
bool operator==(const Monster &monster,const Monster &other);

源文件:

#include "Monster.h"
#include"SpriteStone.h"
#define MONSTER_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 = 0;
	ret = level * MONSTER_FACTOR ;
	return ret;
}
ostream& operator<<(ostream& os,const  Monster& monster)
{
	os << monster.level << "级,"
	<< monster.category << "魂兽" << endl;
	return os;
}
bool operator==(const Monster& monster, const Monster& other)
{
	if (monster.category == other.category &&
		monster.level == other.level) {
		return  true;
	}
	return false;
}

接下里是最重要的人物了:
人物主要有一些属性和活动:

#pragma once
#include<string>
#include<vector>
#include<iostream>
class Monster;
class SpriteStone;
using namespace std;
enum class Immortallevel {
	LIAN_QI,
	ZHU_JI,
	JIE_DAN,
	YUAN_YING,
	HUA_SHENG,
	LIAN_XU,
	HE_TI,
	DA_CHENG,
	DU_JIE
};
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( Immortal& other, const Monster& monsterSource,
		const Monster& monsterDest);
	//把自己的灵兽卖给其他修仙者
	bool trade(const Monster&monster,  Immortal& other);
	//捕获妖兽
	bool fight(const Monster& monster);
	//两个角色进行比武
	bool Contest(const Immortal &other);
	//两个角色用生命对决 
	bool risk( Immortal& other);
	//获取战斗力
	int getpower()const; 
	bool biguan() ;
	friend ostream& operator<<(ostream& os, const Immortal& immortal);
	friend ostream& operator<<(ostream& os, const Immortallevel& level);
private:
	string name;			//修仙者姓名
	string menpai;			//修仙者门派
	Immortallevel level;    //等级
	vector<SpriteStone> stones;//灵石资产
	vector<Monster>monsters;    //灵兽资产 
	bool alive;           //是否还活着
	bool Hadmonstr(const Monster &monster);  
	bool removemonster(const Monster &monster);
	void dead();
};
ostream& operator<<(ostream& os, const Immortal& immortal);
ostream& operator<<(ostream& os, const Immortallevel& level);

源文件:

#include "Immortal.h"
#include"Monster.h"
#include"SpriteStone.h"
#define IMMORTAL_GRADE_FACTOR 1000
#define SPRITESTONE_FACTOR 0.1
#define MONSTER_FACTOR1 0.5
using namespace std;
Immortal::Immortal(const char* name, const char* menpai, Immortallevel level)
{
	this->name = name;
	this->menpai = menpai;
	this->level = level;
	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 true;
}
bool Immortal::trade(const Monster& monster)
{
	if (alive == false) {
		return false;
	}
	if (!Hadmonstr(monster)) {
		cout << "没有找到该魂兽!" << endl;
		return false;
	}
	SpriteStone stone;
	stone = monster.getvalue();
	stones.push_back(stone);
	if (removemonster(monster)) {
		cout << "销售成功" << endl;
	}
	return true;
}
bool Immortal::trade(Immortal& other, const Monster& monster)
{//用自己的灵石,购买其他修仙者的灵兽
	if (alive == false) {
		return false;
	}
	if (!other.Hadmonstr(monster)) {
		cout << "没有这个灵兽" << endl;
		return  false;
	}
	SpriteStone stone;
	for (int i = 0;i < stones.size();i++) {
		stone = stone + stones[i];
	}
	if (stone >= monster.getvalue()) {
		stone = stone - monster.getvalue();
		stones.clear();
		stones.push_back(stone);
		other.stones.push_back(monster.getvalue());
		monsters.push_back(monster);
		other.removemonster(monster);
		return true;
	}else{
		return false;
	}
	
}
bool Immortal::trade(Immortal& other, const Monster& monsterSource, const Monster& monsterDest)
{
	//用自己指定的灵兽和其他修仙者的指定的灵兽交换
	if (other.alive == false || other.alive == false) {
		return false;
	}
	if (!Hadmonstr(monsterSource)) {
		return false;
		cout << "无";
	}
	if (!other.Hadmonstr(monsterDest)) {
		return false;
		cout << "无";
	}
	if (monsterSource.getvalue() == monsterDest.getvalue()) {
		cout << "交易成功" << endl;
		monsters.push_back(monsterDest);
		other.removemonster(monsterDest);
		other.monsters.push_back(monsterSource);
		removemonster(monsterSource);
		return true;
	}
	cout << "交易失败" << endl;
	return false;
}
bool Immortal::trade(const Monster& monster, Immortal& other)
{//把自己的灵兽卖给其他修仙者
	if (!Hadmonstr(monster)) {
		return false;
	}
	SpriteStone stone;
	for (int i = 0;i < other.stones.size();i++) {
		stone = stone + other.stones[i];
	}
	if (stone >= monster.getvalue()) {
		stone = stone - monster.getvalue();
		other.stones.clear();
		other.stones.push_back(stone);
		stones.push_back(monster.getvalue());
		other.monsters.push_back(monster);
		removemonster(monster);
		return true;
	}
	return false;
}
bool Immortal::fight(const Monster& monster)
{
	if (getpower() > monster.getpower()) {
		cout << "捕获成功" << endl;
		monsters.push_back(monster);
	}
	else {
		
	}
	return false;
}
bool Immortal::Contest(const Immortal& other)
{
	if (getpower() > other.getpower()) {
		cout << "比试成功" << endl;
		return true;
	}else if(getpower() == other.getpower()){
		cout << "比试战平" << endl;
		return true;
	}
	else {
		cout << "比试失败" << endl;
		return false;
	}

}
bool Immortal::risk(Immortal& other)
{
	if (getpower() > other.getpower()) {
		cout << "比试成功" << endl;
		for (int i = 0;i < other.stones.size();i++) {
			stones.push_back(other.stones[i]);
		}
		for (int i = 0;i < other.monsters.size();i++) {
			monsters.push_back(other.monsters[i]);
		}
		other.dead();
		return true;
	}
	else if (getpower() == other.getpower()) {
		cout << "比试战平" << endl;
		return true;
	}
	else {
		cout << "比试失败" << endl;
		for (int i = 0;i <stones.size();i++) {
			other.stones.push_back(stones[i]);
		}
		for (int i = 0;i <monsters.size();i++) {
			other.monsters.push_back(monsters[i]);
		}
		dead();
		return false;
	}
}
int Immortal::getpower() const
{
	int ret = 0;
	//根据人物的等级来判断战斗力
	ret = ((int)level + 1) * IMMORTAL_GRADE_FACTOR;
	//根据人物的灵石来增加战斗力
	SpriteStone stone;
	for (int i = 0;i < stones.size();i++) {
		stone = stone + stones[i];
	}
	ret += stone.getcount() * SPRITESTONE_FACTOR;
	//根据灵兽来增加战斗力
	for (int i = 0;i < monsters.size();i++) {
		ret += monsters[i].getpower() * MONSTER_FACTOR1;;
	}
	return ret;
}
bool Immortal::biguan() 
{
	level = (Immortallevel)((int)level + 1);
	return true;
}
bool Immortal::Hadmonstr(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 (monster == *it) {
			it = monsters.erase(it);
			return true;
		}
		else {
			it++;
		}
	}
	return false;
}
void Immortal::dead()
{
	alive = false;
	monsters.erase(monsters.begin(),monsters.end());
	stones.erase(stones.begin(),stones.end());
}
ostream& operator<<(ostream& os, const Immortal& immortal) {//你这里的immortal 加了const 限定,后面也必须有const 限定
	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 << "拥有的灵兽:";
	if (immortal.monsters.size() == 0) {
		os << "无" << endl;
	}
	else {
		for (int i = 0;i < immortal.monsters.size();i++) {
			os << immortal.monsters[i];//这个monsters[i] 就是const 的常量
		}
	}
	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;
}

最后可以开始测试了:

#include<iostream>
#include"SpriteStone.h"
#include"Monster.h"
#include"Immortal.h"
#include<Windows.h>
using namespace std;
void testImmortal() {
	Immortal immortal("唐三","唐门", Immortallevel::HE_TI);
	cout << immortal;
	for (int i = 0;i < 50;i++) {
		immortal.mining();
	}
	cout << immortal;

	Monster monster1(1,"擎天巨蟒");
	immortal.fight(monster1);
	cout << "捕获后的情况:" << endl;
	cout << immortal;

	Monster monster2(1,"泰坦巨猿");
	cout<<monster2;
	immortal.fight(monster2);
	Monster monster3(1,"柔骨兔");
	immortal.fight(monster3);
	cout << "捕获后的情况:" << endl;
	cout << immortal;

	//交易特定的
	immortal.trade(monster1);
	cout << "交易后的情况:" << endl;
	cout << immortal;
	//全部交易
	immortal.trade();
	cout << "清仓大甩卖" << endl;
	cout << immortal;
}
void testtrade() {
	Immortal tangsan("唐三", "唐门", Immortallevel::HE_TI);
	for (int i = 0;i < 50;i++) {
		tangsan.mining();
	}
	Monster monster1(1,"蓝银妖兽");
	tangsan.fight(monster1);
	cout << tangsan;
	Immortal huoyuhao("霍雨浩", "昊天宗", Immortallevel::DA_CHENG);
	for (int i = 0;i < 50;i++) {
		huoyuhao.mining();
	}
	Monster monster2(1,"天梦冰蚕");
	huoyuhao.fight(monster2);
	cout << huoyuhao;
	cout << "-----------------------------------------------" << endl;;
	tangsan.trade( monster1,huoyuhao);
	cout << tangsan;
	cout << huoyuhao;
}
void testContest() {
	Immortal tangsan("唐三", "唐门", Immortallevel::HE_TI);
	for (int i = 0;i < 50;i++) {
		tangsan.mining();
	}
	cout << tangsan;
	Immortal huoyuhao("霍雨浩", "昊天宗", Immortallevel::DA_CHENG);
	for (int i = 0;i < 50;i++) {
		huoyuhao.mining();
	}
	cout << huoyuhao;
	huoyuhao.risk(tangsan);
	cout << tangsan;
	cout << huoyuhao;
}
void testup() {
	Immortal tangsan("唐三", "唐门", Immortallevel::HE_TI);
	cout << tangsan;
	tangsan.biguan();
	cout << tangsan;
}
void tsetmining() {//挖矿
	Immortal ts("唐三", "唐门", Immortallevel::HE_TI);
	cout << ts;
	ts.mining();
	cout << ts;
}
int main() {
	cout << "------------测试挖矿--------------------" << endl;
	cout << "------------------------------------" << endl;
	tsetmining();
	cout << "------------测试交易--------------------" << endl;
	cout << "------------------------------------" << endl;
	testtrade();
	cout << "------------测试斗争--------------------" << endl;
	cout << "------------------------------------" << endl;
	testContest();
	system("pause");
	return 0;
}

修仙游戏(重载运算符练习)

相关标签: c++