一个简单的角色扮演文字小游戏(练练手)
闲着在老师给了一个简单的练习后自己拓展出来的,代码量不大,也很简单,所以拿出来分享分享
一、 简单说明:
通过创建角色,生成角色。创建角色:输入角色的一些关键信息,使角色生成。
可以通过自己生成一些朋友,或者相亲对象,相亲对象符合基本要求可以结尾伴侣。
二、类创建
1、角色类(role)
2、朋友类(pal)
3、伴侣类(spouse)
三、代码区
1、角色类:
#pragma once
#include<string>
#include<vector>
//主角类
class Spouse;
class Pal;
class Role
{
public:
Role();
~Role();
void setInfo(); //创建角色
std::string getName() const;
int getAge() const;
std::string getSex() const;
std::string description()const;
void marrySpouse(Spouse& spouse); //结婚
void divorceSpouse(Spouse& spouse); //离婚
void makePal(Pal& pal); //交朋友
void palList(Pal& pal); //朋友列表
void showPallist(); //查看朋友列表
void severPal(Pal& pal); //断绝关系
bool isAsking(const char* str); //是否询问问题
private:
std:: string name;
int age;
int sex;
Spouse* spouse; //伴侣
Pal *pal; //朋友
std::vector<Pal>pals;
static int palCount;
};
.cpp
#include<iostream>
#include<sstream>
#include "Role.h"
#include"Pal.h"
#include"Spouse.h"
using namespace std;
int Role::palCount = 0;
Role::Role(){
name = "unknow";
age = 0;
sex = 0;
spouse = NULL;
pal = NULL;
}
Role::~Role(){
}
void Role::setInfo() {
system("cls");
string name;
int age;
int sex;
cout << endl;
cout << "——————进入角色创建模式——————" << endl;
cout << endl;
cout << "请输入你的名字:";
cin >> name;
cout << "请输入你的年龄:";
cin >> age;
cout << "请输入你的性别:【输入 1 为 ♂;输入 2 为 ♀】";
cin >> sex;
cout << "创建成功!!" << endl;
this->name = name;
this->age = age;
this->sex = sex;
}
string Role::getName() const{
return name;
}
int Role::getAge() const{
return age;
}
string Role::getSex() const{
if (sex == 1) {
return "♂";
}else if (sex == 2) {
return "♀";
}
}
string Role::description() const{
string condition_mari;
if (spouse) {
condition_mari = "已婚";
}else {
condition_mari = "未婚";