Cultivation game-black and white interface

Cultivation game-black and white interface, there is no practical meaning, practice by yourself

  • In the game

    • Monster

    • Lingshi

    • Cultivator

Let's first implement the Lingshi class: Ling_shi

  • First of all, there are the number and level in the spirit stone
  • Levels are beginner, intermediate, and advanced
  • 1 intermediate spirit stone equals 10 elementary spirit stones
  • 1 high-level spiritual stone is equal to 10 intermediate spiritual stones

Below is the code

  • Ling_shi.h
#pragma once
#include <iostream>
#include <string>


enum class Ling_Shi_Level {
	PRIMARY,					//初阶
	SCALA_MEDIA,				//中阶
	HIGH_ORDER					//高阶
};

class Ling_Shi
{
public:
	Ling_Shi(const int count = 0, const Ling_Shi_Level level = Ling_Shi_Level::PRIMARY);
	//描述
	std::string str()const;
	//运算重载
	Ling_Shi& operator+(Ling_Shi& ling_shi);
	bool operator>=(Ling_Shi& ling_shi);
	Ling_Shi& operator-(Ling_Shi& ling_shi);
	//友元
	friend std::ostream& operator<<(std::ostream& os, Ling_Shi ling_shi);
private:
	int count;				//数量
	Ling_Shi_Level level;	//等级
};


std::ostream& operator<<(std::ostream& os, Ling_Shi ling_shi);
  • Ling_shi.cpp
#include "Ling_Shi.h"
#include <sstream>

Ling_Shi::Ling_Shi(const int count, const Ling_Shi_Level level) {
	this->count = count;
	this->level = level;
}

std::string Ling_Shi::str()const {
	std::stringstream ret;
	ret << "灵石的数量为:" << count;
	switch (level) {
	case Ling_Shi_Level::PRIMARY:
		ret << "\t初阶灵石" ;
		break;
	case Ling_Shi_Level::SCALA_MEDIA:
		ret << "\t中阶灵石" ;
		break;
	case Ling_Shi_Level::HIGH_ORDER:
		ret << "\t高阶灵石" ;
		break;
	default:
		ret << "\t未知灵石" ;
	}
	return ret.str();
}

Ling_Shi& Ling_Shi::operator+(Ling_Shi& ling_shi)
{
	int count = 0;
	switch (ling_shi.level) {
	case Ling_Shi_Level::PRIMARY:
		count = count + ling_shi.count;
		break;
	case Ling_Shi_Level::SCALA_MEDIA:
		count = count + (ling_shi.count * 10);
		break;
	case Ling_Shi_Level::HIGH_ORDER:
		count = count + (ling_shi.count * 100);
		break;
	}

	switch (this->level) {
	case Ling_Shi_Level::PRIMARY:
		count = count + this->count;
		break;
	case Ling_Shi_Level::SCALA_MEDIA:
		count = count + (this->count * 10);
		break;
	case Ling_Shi_Level::HIGH_ORDER:
		count = count + (this->count * 100);
		break;
	}

	this->count = count;
	this->level = Ling_Shi_Level::PRIMARY;
	return *this;
}

bool Ling_Shi::operator>=(Ling_Shi& ling_shi)
{
	int count = 0;
	switch (ling_shi.level) {
	case Ling_Shi_Level::PRIMARY:
		count = count + ling_shi.count;
		break;
	case Ling_Shi_Level::SCALA_MEDIA:
		count = count + (ling_shi.count * 10);
		break;
	case Ling_Shi_Level::HIGH_ORDER:
		count = count + (ling_shi.count * 100);
		break;
	}
	int count1 = 0;
	switch (this->level) {
	case Ling_Shi_Level::PRIMARY:
		count1 = count1 + this->count;
		break;
	case Ling_Shi_Level::SCALA_MEDIA:
		count1 = count1 + (this->count * 10);
		break;
	case Ling_Shi_Level::HIGH_ORDER:
		count1 = count1 + (this->count * 100);
		break;
	}
	return count1 > count;
}

Ling_Shi& Ling_Shi::operator-(Ling_Shi& ling_shi)
{
	this->count = count - ling_shi.count;
	return *this;
}

std::ostream& operator<<(std::ostream& os, Ling_Shi ling_shi) {
	os << ling_shi.str();
	return os;
}

Then implement the monster class: Monster

  • Monster beasts are also hierarchical

    Level 1 monster: worth 100 elementary spirit stones

    Level 2 monster: worth 200 elementary spirit stones

    Level 3 monster: worth 500 elementary spirit stones

    Level 4 monster: worth 1000 elementary spirit stones

    Level 5 Monster: Value of 2000 Elementary Spirit Stone

    Level 6 Monster: Value 5000 Elementary Spirit Stone

    7th-level monster: worth 10,000 primary spirit stones

    8th level monster: worth 20,000 primary spirit stones

    9th-level monster: worth 100,000 primary spirit stones

Real code below

  • Monster.h
#include <string>
#include <iostream>
#include "Ling_Shi.h"

//class Ling_Shi;



class Monster
{
public:
	Monster(const int level, const std::string kind);
	//获取妖兽所对应的灵石
	Ling_Shi get_viual();
	//友元
	friend std::ostream& operator<<(std::ostream& os, Monster& monster);
	//运算重载
	bool operator==(Monster& monster);
	bool operator>(Monster& monster);
	//获取妖兽的战斗力
	int get_power() const;
private:
	int level;				//等级
	std::string kind;		//种类
};

std::ostream& operator<<(std::ostream& os, Monster& monster);

Monster.cpp

#include "Monster.h"
#include "Ling_Shi.h"
//系数
#define MONSTER_POWER_FACTOR	1000

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

Ling_Shi Monster::get_viual() {
	int ling_shi[] = { 100 , 200 , 500 , 1000 , 2000 , 5000 , 10000 ,20000 , 100000 };
	int count = ling_shi[level - 1];
	return Ling_Shi(count, Ling_Shi_Level::PRIMARY);
}

bool Monster::operator==(Monster& monster)
{
	if (level == monster.level && monster.kind == kind) {
		return true;
	}
	else {
		return false;
	}
	
}

bool Monster::operator>(Monster& monster)
{

	return this->level > monster.level;
}

int Monster::get_power() const
{
	
	return level * MONSTER_POWER_FACTOR;
}

std::ostream& operator<<(std::ostream& os, Monster& monster) {
	os <<"\t" << monster.kind << "等级:" << monster.level;
	return os;
}

Finally, implement the immortal cultivator's class: Immortal

  • Let’s analyze what the cultivators have

    Cultivator

    Name

    School: (Huangfeng Valley, Ghost Sect, Fallen Cloud Sect, Covering Moon Sect, Ancient Sword Sect, Yuling Sect, Sanxiu)

    Level (Qi training period, foundation building period, pill formation period, Nascent infant period, spiritual transformation period, deficiency refining period, integrative period, great growth period, and tribulation period)

    Assets (variable number of monsters, variable number of spirit stones)

    Status: life, death

  • Cultivators can also trade and other activities

    Immortal cultivators often carry out trade activities, using spirit stones to buy monsters, or exchange monsters for spirit stones.

    Cultivators can obtain spirit stones through mining, and only 100 elementary spirit stones can be obtained each time.

    The cultivator can capture monsters, and whether or not he can capture monsters depends on whether his combat power is greater than that of monsters.

Let's implement the code below

Immortal.h

#pragma once
#include <string>
#include <vector>
#include <iostream>
#include "Ling_Shi.h"
#include "Monster.h"

//练气期, 筑基期, 结丹期,元婴期, 化神期,炼虚期,合体期,大成期,渡劫期
enum class Immortal_Level {
	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* men_pai, Immortal_Level level = Immortal_Level::LIAN_QI);
	//获取全部灵石
	Ling_Shi get_ling_shi_count();
	//获取战斗力
	int get_power()const;
	//挖矿
	void mining();		
	//获取妖兽
	bool capture(Monster& monster);

	//出售全部妖兽
	bool sell();
	//出售指定妖兽
	bool sell(Monster& monster);
	//用自己的灵石,来购买其他修仙者的指定妖兽
	bool sell(Immortal& other, Monster& monster);
	//用自己指定的妖兽来换取别人的妖兽
	bool sell(Monster& monster, Immortal& other, Monster& other_monster);
	//用自己的妖兽售卖给其他修仙者来换取灵石
	bool sell(Monster& monster, Immortal& other);



	friend std::ostream& operator<<(std::ostream& os, Immortal& immortal);
private:
	std::string name;				//姓名
	std::string men_pai;			//门派
	Immortal_Level	level;			//等级
	std::vector<Ling_Shi> lins_shis; //灵石
	std::vector<Monster>  Monsters;	 //妖兽
	bool life;						//生命

	//死亡
	void dead();
	//查找自己是否有妖兽
	bool find_monster(Monster& monster);
	//删除指定妖兽
	bool remove_monster(Monster& monster);
};

std::ostream& operator<<(std::ostream& os, Immortal& immortal);

Immortal.cpp

#include "Immortal.h"

#define IMMORTAL_POWER_FACTOR 1000

Immortal::Immortal(const char* name, const char* men_pai, Immortal_Level level)
{
	this->name = name;
	this->men_pai = men_pai;
	this->level = level;
	this->life = true;
}

Ling_Shi Immortal::get_ling_shi_count()
{
	Ling_Shi count;
	for (int i = 0; i < lins_shis.size(); i++) {
		count = count + lins_shis[i];
	}

	return count;
}

int Immortal::get_power() const
{
	int power = ((int)level + 1) * IMMORTAL_POWER_FACTOR;
	
	return power;
}

void Immortal::mining()
{
	lins_shis.push_back(Ling_Shi(100, Ling_Shi_Level::PRIMARY));
}

bool Immortal::capture(Monster& monster)
{
	int me_power = this->get_power();
	int monster_power = monster.get_power();
	if (me_power > monster_power) {
		Monsters.push_back(monster);
		return true;
	}
	else {
		this->dead();
	}
	return false;
}


std::ostream& operator<<(std::ostream& os, Immortal& immortal) {
	os << "姓名:" << immortal.name << (immortal.life ? "[在修]" : "\t[死亡]") <<"门派:" << immortal.men_pai << " 等级:" ;
	
	switch (immortal.level) {
	case Immortal_Level::LIAN_QI:
		os << "练气期";
		break;
	case Immortal_Level::ZHU_JI:
		os << "筑基期";
		break;
	case Immortal_Level::JIE_DAN:
		os << "结丹期";
		break;
	case Immortal_Level::YUAN_YING:
		os << "元婴期";
		break;
	case Immortal_Level::HUA_SHENG:
		os << "化神期";
		break;
	case Immortal_Level::LIAN_XU:
		os << "炼虚期";
		break;
	case Immortal_Level::HE_TI:
		os << "合体期";
		break;
	case Immortal_Level::DA_CHENG:
		os << "大成期";
		break;
	case Immortal_Level::DU_JIE:
		os << "渡劫期";
		break;

	}

	if (!immortal.lins_shis.size()) {
		os << "\t无灵石";
	}
	else {
		Ling_Shi temp = immortal.get_ling_shi_count();
		os << temp.str();
	}
	
	if (!immortal.Monsters.size()) {
		os << "\t无妖兽";
	}
	else {
		for (int i = 0; i < immortal.Monsters.size(); i++) {
			os << immortal.Monsters[i] << "\t";
		}
	}

	return os;
}


bool Immortal::sell() {
	if (!this->life) {
		return false;
	}
	//置换妖兽为灵石
	for (int i = 0; i < Monsters.size(); i++) {
		lins_shis.push_back(Monsters[i].get_viual());
	}
	//删除全部妖兽
	Monsters.erase(Monsters.begin(), Monsters.end());
	return true;
}

bool Immortal::sell(Monster& monster)
{
	if (!life) {
		return false;
	}


	if (find_monster(monster)) {
		lins_shis.push_back(monster.get_viual());
		remove_monster(monster);
		return true;
	}
	else {
		std::cout << "没有找到指定妖兽,售卖失败" << std::endl;
		return false;
	}

	
}

bool Immortal::sell(Immortal& other, Monster& monster)
{
	//死了就无法交易了
	if (!this->life || !other.life) {
		return false;
	}

	if (!other.find_monster(monster)) {
		std::cout << "没有找到妖兽" << std::endl;
		return false;
	}

	Ling_Shi me_ling_shi;
	for (int i = 0; i < this->lins_shis.size(); i++) {
		me_ling_shi = me_ling_shi + lins_shis[i];
	}
	
	Ling_Shi monster_ling_shi = monster.get_viual();

	if (me_ling_shi >= monster_ling_shi) {
		this->Monsters.push_back(monster);
		other.remove_monster(monster);
		other.lins_shis.push_back(monster.get_viual());
		
		me_ling_shi = me_ling_shi - monster_ling_shi;
		this->lins_shis.clear();
		lins_shis.push_back(me_ling_shi);
		return true;
	}
	else {
		std::cout << "没有足够的灵石,无法购买" << std::endl;
		return false;
	}
}

bool Immortal::sell(Monster& monster, Immortal& other, Monster& other_monster)
{
	if (!this->life || !other.life) {
		return false;
	}

	if (!other.find_monster(other_monster)) {
		std::cout << "对方没有指定妖兽,无法交换" << std::endl;
		return false;
	}

	if (!(monster > other_monster)) {
		std::cout << "同等级的妖兽不换,要求比我高一等级的才换" << std::endl;
		return false;
	}

	this->remove_monster(monster);
	other.remove_monster(other_monster);
	this->Monsters.push_back(other_monster);
	other.Monsters.push_back(monster);

	return true;
}

bool Immortal::sell(Monster& monster, Immortal& other)
{
	if (!this->life || !other.life) {
		return false;
	}

	if (other.find_monster(monster)) {
		std::cout << "对方已经有了此妖兽,不想再购买" << std::endl;
		return false;
	}

	Ling_Shi other_ling_shi;
	for (int i = 0; i < other.lins_shis.size(); i++) {
		other_ling_shi = other_ling_shi + other.lins_shis[i];
	}
	Ling_Shi monster_ling_shi = monster.get_viual();
	if (!(other_ling_shi >= monster_ling_shi)) {
		std::cout << "对方没有足够的灵石来购买此妖兽" << std::endl;
		return false;
	}
	else {
		this->remove_monster(monster);
		this->lins_shis.push_back(monster.get_viual());
		other.Monsters.push_back(monster);

		other_ling_shi = other_ling_shi - monster_ling_shi;
		other.lins_shis.clear();
		other.lins_shis.push_back(other_ling_shi);
		return true;
	}
}

void Immortal::dead()
{
	life = false;
	lins_shis.erase(lins_shis.begin(), lins_shis.end());
	Monsters.erase(Monsters.begin(), Monsters.end());
}

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

bool Immortal::remove_monster(Monster& monster)
{
	for (auto it = Monsters.begin(); it != Monsters.end();) {
		if (*it == monster) {
			it = Monsters.erase(it);
			return true;
		}
		else {
			it++;
		}
	}
	return false;
}


The code in the above .cpp does not have many comments. In the .h file, the function of each function has been commented, and the above functions are not complete

Guess you like

Origin blog.csdn.net/qq_44695317/article/details/112397790