C++Cmd仿修仙项目:灵石类 1.0.2

本文讨论了如何通过将类内方法按功能分类、使用多态设计transformStone和attributeStone类,以及派生不同级别的灵石类来增强代码可读性。同时提及了VS的自动补全功能和后续开发计划,如StoneBag类和属性灵石类的实现。
摘要由CSDN通过智能技术生成

PS:

具体实现代码仅在最后一次更新保留


2024 年 2 月 13 日 更新

Ideas:

  • 将类内方法 按照功能 简单分类, 可以提高一定可读性
  • test函数 进行分类调用 分类编写,更具可读性

能写出更具有可读性的的代码啦

VS Studying:

  • switch(enum_value)tab后, 按住【DOWN】键 VS会自动补全 case标签, 很方便

VS 很好用

目标:

  • 利用多态 编写transformStone(转换灵石)类,和 attributeStone(属性灵石)类
  • 实现和完善其他类功能
  • 添加:功法(派生灵技、神通等),灵宝等类

2024 年 2 月 14 日 更新

更新内容:

使用派生的手法,以灵石类为基类,派生了下、中、上、极品灵石 4 大类,并且依次重写 upStone 和 downStone 定义,返回兑换后的灵石;

计划下一步,实现修仙者类的灵石包(class StoneBag)功能,可以储存各种各样的灵石,定义一个方便添加指定灵石的接口,和实现 cout 灵石包内所有灵石的功能;

计划下一步,实现属性灵石(如:火、水、木等)(class AttributeStone public:SpriteStone)灵石类。

各种各样的灵石很有趣


All classes:

  •  SpriteStone        基类
  •  PrimaryStone    下品灵石
  •  MiddleStone      中品灵石
  •  AdvanceStone   上品灵石
  •  MonsterStone    极品灵石

接口:

virtual string str() const;

  • 作用:以字符串的方式返回 灵石的内容
  • 返回:包含灵石内容的 字符串

bool isEmpty() const;

  • 作用:判断灵石数量是否为0

int transForPri() const;

  • 作用:以整数形式返回灵石代表的价值

// actions //

virtual SpriteStone upStone();

  • 作用:向上兑换灵石
  • 返回:上一级灵石

virtual SpriteStone downStone();

  • 作用:向下兑换灵石
  • 返回:下一级灵石

SpriteStone.h

#pragma once
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#define STONE_EXCHANGE_RATE 10
using std::ostream;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

enum class SpriteStoneLevel
{
	PRIMARY_LEVEL,
	MIDDLE_LEVEL,
	ADVANCE_LEVEL,
	MONSTER_LEVLE,
	SPRITE_STONE_LEVEL_COUNT
};

class SpriteStone
{
public:
	// create //
	SpriteStone(
		int count = 0,
		SpriteStoneLevel level = SpriteStoneLevel::PRIMARY_LEVEL
	);

	// return values //
	virtual string str() const;
	bool isEmpty() const;

	// actions //
	virtual SpriteStone upStone();
	virtual SpriteStone downStone();

	// friends and operators //
	friend ostream& operator<<(
		ostream& cout,
		SpriteStone& stone
		);
	int transForPri() const;
	virtual SpriteStone operator+(const SpriteStone& stone);
protected:
	int m_count;
	SpriteStoneLevel m_level; 
};

ostream& operator<<(
	ostream& cout, 
	SpriteStone& stone
	);

SpriteStone.cpp 

#include "SpriteStone.h"

SpriteStone::SpriteStone(int count, SpriteStoneLevel level)
{
	this->m_level = level;
	this->m_count = count;
}

std::string SpriteStone::str() const
{
	stringstream ret;

	ret << "[";
	switch (m_level)
	{
	case SpriteStoneLevel::PRIMARY_LEVEL:
		ret << "下品灵石";
		break;
	case SpriteStoneLevel::MIDDLE_LEVEL:
		ret << "中品灵石";
		break;
	case SpriteStoneLevel::ADVANCE_LEVEL:
		ret << "上品灵石";
		break;
	case SpriteStoneLevel::MONSTER_LEVLE:
		ret << "极品灵石";
		break;
	default:
		ret << "未知灵石";
		break;
	}
	ret << "] ";

	ret << m_count << " 块 ";
	return ret.str();
}

bool SpriteStone::isEmpty() const
{
	return m_count <= 0;
}

SpriteStone SpriteStone::upStone()
{
	return SpriteStone();
}

SpriteStone SpriteStone::downStone()
{
	return SpriteStone();
}

int SpriteStone::transForPri() const
{
	int sum = 0;
	switch (m_level)
	{
	case SpriteStoneLevel::PRIMARY_LEVEL:
		sum += this->m_count;
		break;
	case SpriteStoneLevel::MIDDLE_LEVEL:
		sum += this->m_count * 10;
		break;
	case SpriteStoneLevel::ADVANCE_LEVEL:
		sum += this->m_count * 100;
		break;
	default:
		sum = 0;
		break;
	}
	return sum;
}

SpriteStone SpriteStone::operator+(const SpriteStone& stone)
{
	int sum = this->transForPri() + stone.transForPri();
	return SpriteStone(sum, SpriteStoneLevel::PRIMARY_LEVEL);
}

std::ostream& operator<<(ostream& cout, SpriteStone& stone)
{
	cout << stone.str();
	// TODO: 在此处插入 return 语句
	return cout;
}

DeriveStone.h

#pragma once
#include "SpriteStone.h"
class PrimaryStone :
    public SpriteStone
{
public:
    PrimaryStone(int count);
    SpriteStone upStone();
    SpriteStone downStone();
};
ostream& operator<<(ostream& cout, PrimaryStone ps);

class MiddleStone :
    public SpriteStone
{
public:
    MiddleStone(int count);
    SpriteStone upStone();
    SpriteStone downStone();
};

class AdvanceStone :
    public SpriteStone
{
public:
    AdvanceStone(int count);
    SpriteStone upStone();
    SpriteStone downStone();
};

class MonsterStone :
    public SpriteStone
{
public:
    MonsterStone(int count);
    SpriteStone upStone();
    SpriteStone downStone();
};

DeriveStone.cpp

#include "DeriveStone.h"

PrimaryStone::PrimaryStone(int count)
{
	this->m_count = count;
	m_level = SpriteStoneLevel::PRIMARY_LEVEL;
}

SpriteStone PrimaryStone::upStone()
{
	if (m_count < STONE_EXCHANGE_RATE)
	{
		cout << "兑换灵石: 下品灵石 数量不足 兑换 中品灵石" << endl;
		return PrimaryStone(0);
	}
	else
	{
		int num = m_count / STONE_EXCHANGE_RATE;
		if ((m_count % STONE_EXCHANGE_RATE) == 0)
		{
			m_count = 0;
		}
		else
		{
			m_count %= STONE_EXCHANGE_RATE;
		}
		cout << "兑换灵石: ";
		cout << num * STONE_EXCHANGE_RATE << " 枚 下品灵石 ";
		cout << "成功兑换 " << num << " 枚 中品灵石" << endl;
		return MiddleStone(num);
	}
}

SpriteStone PrimaryStone::downStone()
{
	cout << "兑换灵石: 下品灵石 是最劣质的灵石啦" << endl;
	return SpriteStone();
}

MiddleStone::MiddleStone(int count)
{
	this->m_count = count;
	this->m_level = SpriteStoneLevel::MIDDLE_LEVEL;
}

SpriteStone MiddleStone::upStone()
{
	if (m_count < STONE_EXCHANGE_RATE)
	{
		cout << "兑换灵石: 中品灵石 数量不足 兑换 上品灵石" << endl;
		return PrimaryStone(0);
	}
	else
	{
		int num = m_count / STONE_EXCHANGE_RATE;
		if ((m_count % STONE_EXCHANGE_RATE) == 0)
		{
			m_count = 0;
		}
		else
		{
			m_count %= STONE_EXCHANGE_RATE;
		}
		cout << "兑换灵石: ";
		cout << num * STONE_EXCHANGE_RATE << " 枚 中品灵石 ";
		cout << "成功兑换 " << num << " 枚 上品灵石" << endl;
		return AdvanceStone(num);
	}
}

SpriteStone MiddleStone::downStone()
{
	if (m_count == 0)
	{
		cout << "兑换灵石: 中品灵石 为空" << endl;
		return SpriteStone(0);
	}
	else
	{
		int num = m_count * STONE_EXCHANGE_RATE;
		m_count = 0;
		cout << "兑换灵石: ";
		cout << num / STONE_EXCHANGE_RATE << " 枚 中品灵石 ";
		cout << "成功兑换 " << num << " 枚 下品灵石" << endl;
		return PrimaryStone(num);
	}
}

ostream& operator<<(ostream& cout, PrimaryStone ps)
{
	cout << ps.str();
	return cout;
}

AdvanceStone::AdvanceStone(int count)
{
	this->m_count = count;
	this->m_level = SpriteStoneLevel::ADVANCE_LEVEL;
}

SpriteStone AdvanceStone::upStone()
{
	if (m_count < STONE_EXCHANGE_RATE)
	{
		cout << "兑换灵石: 上品灵石 数量不足 兑换 极品灵石" << endl;
		return PrimaryStone(0);
	}
	else
	{
		int num = m_count / STONE_EXCHANGE_RATE;
		if ((m_count % STONE_EXCHANGE_RATE) == 0)
		{
			m_count = 0;
		}
		else
		{
			m_count %= STONE_EXCHANGE_RATE;
		}
		cout << "兑换灵石: ";
		cout << num * STONE_EXCHANGE_RATE << " 枚 上品灵石 ";
		cout << "成功兑换 " << num << " 枚 极品灵石" << endl;
		return AdvanceStone(num);
	}
}

SpriteStone AdvanceStone::downStone()
{
	if (m_count == 0)
	{
		cout << "兑换灵石: 上品灵石 为空" << endl;
		return SpriteStone(0);
	}
	else
	{
		int num = m_count * STONE_EXCHANGE_RATE;
		m_count = 0;
		cout << "兑换灵石: ";
		cout << num / STONE_EXCHANGE_RATE << " 枚 上品灵石 ";
		cout << "成功兑换 " << num << " 枚 中品灵石" << endl;
		return PrimaryStone(num);
	}
}

MonsterStone::MonsterStone(int count)
{
	this->m_count = count;
	this->m_level = SpriteStoneLevel::MONSTER_LEVLE;
}

SpriteStone MonsterStone::upStone()
{
	cout << "兑换灵石: 极品灵石 是最高级的灵石啦" << endl;
	return SpriteStone(0);
}

SpriteStone MonsterStone::downStone()
{
	if (m_count == 0)
	{
		cout << "兑换灵石: 极品灵石 为空" << endl;
		return SpriteStone(0);
	}
	else
	{
		int num = m_count * STONE_EXCHANGE_RATE;
		m_count = 0;
		cout << "兑换灵石: ";
		cout << num / STONE_EXCHANGE_RATE << " 枚 极品灵石 ";
		cout << "成功兑换 " << num << " 枚 上品灵石" << endl;
		return PrimaryStone(num);
	}
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值