简化的创建游戏角色

参考:https://blog.youkuaiyun.com/mall_lucy/article/details/70332764
一.题目分析
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
生命值=体力*20。
魔法值=(智力+智慧)*10。
职业限制
很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
种族/职业 狂战士 圣骑士 刺客 猎手 祭司 巫师
人类 允许 允许 允许 允许 允许 允许
精灵 不允许 不允许 允许 允许 允许 允许
兽人 允许 不允许 不允许 允许 允许 不允许
矮人 允许 允许 不允许 不允许 允许 不允许
元素 不允许 不允许 不允许 不允许 允许 允许
所以在要求用户选择职业时,输出信息里面只能有用户所选择种族可以就职的职业。
初始属性
本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。各职业初始属性的大致比例应遵从下表:
职业/属性 力量 敏捷 体力 智力 智慧
狂战士 40 20 30 5 5
圣骑士 25 15 30 20 10
刺客 20 35 20 15 10
猎手 15 40 15 10 20
祭司 15 20 15 35 15
巫师 10 20 10 20 40
例如,前面示意图中的祭司的初始属性,大致满足该比例,但是应该是随机的。
然后利用属性值计算生命值和魔法值。
显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。

二.类图设计
在这里插入图片描述
三.程序实现

#include<iostream>
#include<string>
#include<ctime>       //随机种子
#include<fstream>     //C++中当操作文件,即写入、读出的时候要用到这个头文件
#include<iomanip>     //主要是对cin,cout之类的一些操纵运算子,比如setfill,setw,setbase,setprecision等等。
using namespace std;
 
int occupation_choice;	//玩家所选择的职业的序号
 
//创立一个类,保存角色的姓名,性别
class Baseinformation
{
protected:
	char name[50];
	string sex;
	int sex_choice;
public:
	void getBaseinformation();
	friend class Output;	//友元类,用于输出角色信息
	friend class File;    //友元类,将角色信息保存到文档中
};
 
//Baseinformation类成员函数:输入角色名和性别
void Baseinformation::getBaseinformation()
{
	int i = 1;
	cout << "请选择您游戏角色的姓名:";
	cin >> name;
	while (i)
	{
		cout << "请选择您游戏角色的性别(0男性,1女性):";
		cin >> sex_choice;
		switch (sex_choice)
		{
		case 0:
			sex = "男性";
			i = 0;
			break;
		case 1:
			sex = "女性";
			i = 0;
			break;
		default:
			cout << "输入错误,请重新输入" << endl;
			break;
		}
	}
}
 
//创立一个类,记录角色的种族、职业
class Race
{
protected:
	char name[50];
	string sex;
	int sex_choice;
	string race;
	string occupation;
	int race_choice;
public:
	void getBaseinformation();
	friend class Output;	//友元类,用于输出角色信息
	friend class File;    //友元类,将角色信息保存到文档中
	void getRace();
	friend class Output;
	friend class File;
};
 
//Race类成员函数定义,选择种族和职业
void Race::getRace()
{
	int i = 1;
	while (i)
	{
		cout << "请选择您游戏角色的种族(0人类,1精灵,2兽人,3矮人,4元素):";
		cin >> race_choice;
		switch (race_choice)
		{
		case 0:
			race = "人类";
			i = 0;
			break;
		case 1:
			race = "精灵";
			i = 0;
			break;
		case 2:
			race = "兽人";
			i = 0;
			break;
		case 3:
			race = "矮人";
			i = 0;
			break;
		case 4:
			race = "元素";
			i = 0;
			break;
		default:
			cout << "输入错误,请重新输入" << endl;
			break;
		}
	}
	while (1)
	{
		cout << "请选择您的职业(0狂战士,1圣骑士,2刺客,3猎手,4祭司,5巫师):" << endl;
		switch (race_choice)
		{
		case 0:
			cout << "0狂战士  1圣骑士  2刺客  3猎手  4祭司  5巫师" << endl;
			break;
		case 1:
			cout << "2刺客  3猎手  4祭司  5巫师" << endl;
			break;
		case 2:
			cout << "0狂战士  3猎手  4祭司  " << endl;
			break;
		case 3:
			cout << "0狂战士  1圣骑士  4祭司 " << endl;
			break;
		case 4:
			cout << "4祭司  5巫师" << endl;
			break;
		}
		cin >> occupation_choice;
		if (race_choice == 0 && (occupation_choice >= 0 && occupation_choice <= 5))
			break;
		else if (race_choice == 1 && (occupation_choice>1 && occupation_choice<6))
			break;
		else if (race_choice == 2 && (occupation_choice == 0 || occupation_choice == 3 || occupation_choice == 4))
			break;
		else if (race_choice == 3 && (occupation_choice == 0 || occupation_choice == 1 || occupation_choice == 4))
			break;
		else if (race_choice == 4 && (occupation_choice>3 && occupation_choice<6))
			break;
		else
			cout << "输入错误,请重新输入" << endl;
	}
	if (occupation_choice == 0)
		occupation = "狂战士";
	if (occupation_choice == 1)
		occupation = "圣骑士";
	if (occupation_choice == 2)
		occupation = "刺客";
	if (occupation_choice == 3)
		occupation = "猎手";
	if (occupation_choice == 4)
		occupation = "祭司";
	if (occupation_choice == 5)
		occupation = "巫师";
}
 
//创立一个类,记录角色的属性
class Attribute
{
protected:
	char name[50];
	string sex;
	int sex_choice;
	string race;
	string occupation;
	int race_choice;
	int strength;	//力量
	int agility;	//敏捷
	int physics;	//体力
	int intelligence;	//智力
	int wisdom;	//智慧
	int HP;	//生命值
	int MP;	//法力值
public:
	void getBaseinformation();
	friend class Output;	//友元类,用于输出角色信息
	friend class File;    //友元类,将角色信息保存到文档中
	void getRace();
	friend class Output;
	friend class File;
	void getAttribute();
	void getRandom(int a, int b, int c, int d, int e);
	friend class Output;
	friend class File;
};
 
//Attribute类成员函数定义,随机生成每项属性的值,abcd为该属性的最小值,e为第五个属性的最大值
void Attribute::getRandom(int a, int b, int c, int d, int e)
{
	int sum;	//前4项属性之和
	srand((unsigned)time(NULL));
	do
	{
		strength = a + rand() % 10;
		agility = b + rand() % 10;
		physics = c + rand() % 10;
		intelligence = d + rand() % 10;
		wisdom=e+rand()%10;
		sum = strength + agility + physics + intelligence + wisdom;
	} while (sum<100);
	HP = physics * 20;
	MP = (wisdom + intelligence) * 10;
}
 
//Attribute类成员函数定义,根据选择的职业,向getRamdom传不同的最小值
void Attribute::getAttribute()
{
	if (occupation_choice == 0)
		getRandom(35, 15, 25, 0, 10);
	if (occupation_choice == 1)
		getRandom(20, 10, 25, 15, 15);
	if (occupation_choice == 2)
		getRandom(15, 30, 15, 10, 15);
	if (occupation_choice == 3)
		getRandom(10, 35, 10, 5, 25);
	if (occupation_choice == 4)
		getRandom(10, 25, 10, 30, 20);
	if (occupation_choice == 5)
		getRandom(5, 15, 5, 15, 45);
}
 
//创立一个类,将角色属性输出到显示器上
class Output
{
public:
	void show(Baseinformation &, Race &, Attribute &);
};
 
void Output::show(Baseinformation &t1, Race &t2, Attribute &t3)
{
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "姓名" << std::left << setw(15) << t1.name << endl;
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "性别" << std::left << setw(15) << t1.sex << endl;
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "种族" << std::left << setw(15) << t2.race << endl;
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "职业" << std::left << setw(15) << t2.occupation << endl;
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "力量" << std::left << setw(15) << t3.strength << endl;
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "敏捷" << std::left << setw(15) << t3.agility << endl;
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "体力" << std::left << setw(15) << t3.physics << endl;
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "智力" << std::left << setw(15) << t3.intelligence << endl;
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "智慧" << std::left << setw(15) << t3.wisdom << endl;
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "生命值" << std::left << setw(15) << t3.HP << endl;
	cout << "==============================" << endl;
	cout << std::left << setw(16) << "法力值" << std::left << setw(15) << t3.MP << endl;
	cout << "==============================" << endl;
}
 
//创立一个类,将角色信息保存到文档
class File
{
public:
	void file(Baseinformation &, Race &, Attribute &);
};
 
void File::file(Baseinformation &t1, Race &t2, Attribute &t3)
{
	ofstream outfile;
	outfile.open("data.txt", ios::out);
	outfile << "姓名:" << t1.name << endl;
	outfile << "性别:" << t1.sex << endl;
	outfile << "种族:" << t2.race << endl;
	outfile << "职业:" << t2.occupation << endl;
	outfile << "力量:" << t3.strength << endl;
	outfile << "敏捷:" << t3.agility << endl;
	outfile << "体力:" << t3.physics << endl;
	outfile << "智力:" << t3.intelligence << endl;
	outfile << "智慧:" << t3.wisdom << endl;
	outfile << "生命值:" << t3.HP << endl;
	outfile << "法力值:" << t3.MP << endl;
}
 
//主函数
int main()
{
	Baseinformation player;
	Race player_race;
	Attribute player_att;
	Output player_show;
	File save;
	int player_choice;
	do
	{
		player.getBaseinformation();
		player_race.getRace();
		player_att.getAttribute();
		player_show.show(player, player_race, player_att);
		cout << endl;
		cout << "是否继续生成游戏角色?" << endl;
		cout << "0.结束生成		1.继续生成" << endl;
		cin >> player_choice;
	} while (player_choice);
	save.file(player, player_race, player_att);
	return 0;
}

四.调试、测试及运行结果
调试:
在这里插入图片描述
测试:
在这里插入图片描述
运行结果:
在这里插入图片描述
在这里插入图片描述
五.经验归纳
在本次上机实验中,明白了一些头文件的含义,例如:#include是随机种子所需头文件,#include是C++中当 操作文件,即写入、读出的时候要用到的头文件,#include主要是对cin,cout之类的一些操纵运算,比如setfill,setw,setbase,setprecision等等。对于C++的类的操作是一个很好的复习,基础类派生类的用法和定义格式,怎么调用函数也是一个难点,程序中的变量很多,需要细心一些。

一、数据采集层:多源人脸数据获取 该层负责从不同设备 / 渠道采集人脸原始数据,为后续模型训练与识别提供基础样本,核心功能包括: 1. 多设备适配采集 实时摄像头采集: 调用计算机内置摄像头(或外接 USB 摄像头),通过OpenCV的VideoCapture接口实时捕获视频流,支持手动触发 “拍照”(按指定快捷键如Space)或自动定时采集(如每 2 秒采集 1 张),采集时自动框选人脸区域(通过Haar级联分类器初步定位),确保样本聚焦人脸。 支持采集参数配置:可设置采集分辨率(如 640×480、1280×720)、图像格式(JPG/PNG)、单用户采集数量(如默认采集 20 张,确保样本多样性),采集过程中实时显示 “已采集数量 / 目标数量”,避免样本不足。 本地图像 / 视频导入: 支持批量导入本地人脸图像文件(支持 JPG、PNG、BMP 格式),自动过滤非图像文件;导入视频文件(MP4、AVI 格式)时,可按 “固定帧间隔”(如每 10 帧提取 1 张图像)或 “手动选择帧” 提取人脸样本,适用于无实时摄像头场景。 数据集对接: 支持接入公开人脸数据集(如 LFW、ORL),通过预设脚本自动读取数据集目录结构(按 “用户 ID - 样本图像” 分类),快速构建训练样本库,无需手动采集,降低系统开发与测试成本。 2. 采集过程辅助功能 人脸有效性校验:采集时通过OpenCV的Haar级联分类器(或MTCNN轻量级模型)实时检测图像中是否包含人脸,若未检测到人脸(如遮挡、侧脸角度过大),则弹窗提示 “未识别到人脸,请调整姿态”,避免无效样本存入。 样本标签管理:采集时需为每个样本绑定 “用户标签”(如姓名、ID 号),支持手动输入标签或从 Excel 名单批量导入标签(按 “标签 - 采集数量” 对应),采集完成后自动按 “标签 - 序号” 命名文件(如 “张三
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值