直達快車:
二、類圖設計三、代碼實現
四、運行截圖
一、題目分析
【功能總述】
1.創建角色,屬性爲:名字、性別、種族、職業、力量、敏捷、體力、智力、智慧、生命值和魔法值。
2.指定了性別、種族、職業(有限制),由其餘屬性根據給定條件計算出生命值和魔法值。
3.每個職業的初始屬性被限定在一個範圍之內,生成角色屬性平均分配。
4.生成角色,輸出角色並保存進文件(加時間戳)。
5.設置最佳職業與種族,玩家可選擇初始技能。(自己新加的)
以下是原本的介紹,可跳過
1.遊戲角色應有的屬性
本題目要求的遊戲角色應有以下屬性:名字、性別、種族、職業、力量、敏捷、體力、智力、智慧、生命值和魔法值。
名字:不超過50個字符。
性別:可以選擇男性和女性。
種族:一共可選五個種族,人類、精靈、獸人、矮人和元素。
職業:可選六種職業,狂戰士、聖騎士、刺客、獵手、祭司和巫師。
其餘屬性均爲整數。
本題目要求首先用戶輸入角色姓名,然後由用戶選擇角色性別,然後由用戶選擇種族,然後選擇職業,然後自動分配力量、敏捷、體力、智力和智慧屬性,並計算生命值和魔法值。
生命值=體力*20。
魔法值=(智力+智慧)*10。
2.職業限制
很多職業會限制某些種族選擇,例如獸人不能就職聖騎士等等,種族和職業的限制表如下:
種族/職業 | 狂戰士 | 聖騎士 | 刺客 | 獵手 | 祭司 | 巫師 |
---|---|---|---|---|---|---|
人類 | 允許 | 允許 | 允許 | 允許 | 允許 | 允許 |
精靈 | 不允許 | 不允許 | 允許 | 允許 | 允許 | 允許 |
獸人 | 允許 | 不允許 | 不允許 | 允許 | 允許 | 不允許 |
矮人 | 允許 | 允許 | 不允許 | 不允許 | 允許 | 不允許 |
元素 | 不允許 | 不允許 | 不允許 | 不允許 | 允許 | 允許 |
3.初始屬性
本題目要求力量、敏捷、體力、智力和智慧要求是隨機值(利用隨機數函數來取得隨機數),但是五項屬性的總和應該是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 |
例如,前面示意圖中的祭司的初始屬性,大致滿足該比例,但是應該是隨機的。
然後利用屬性值計算生命值和魔法值。
4.顯示信息
最後向用戶顯示該角色的所有信息,然後詢問用戶是否滿意,如用戶不滿意則重新創建,若用戶滿意則程序結束,並將用戶創建角色的相關信息寫入文件保存。
二、類圖設計
三、代碼實現
/****
@Author:Innocence
@OS:win10
@IDE:vc++ 6.0
@Time:2019/4/21
2019/4/22
2019/4/23
@Edition:1.0
2.0
3.0
@Description:除了基礎功能以外增加:1.如果選擇的種族和職業爲最佳配比,則增加一定的初始屬性
2.用戶可以自己選擇初始技能
3.保存文件增加保存時間
4.幫助菜單
****/
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
#include <windows.h>
using namespace std;
int occupation_choice; //玩家所選擇的職業的序號
int lucky=0; //玩家所選是否爲最佳配比
class Baseinformation //基礎類,保存角色的姓名,性別
{
protected:
char name[50]; //姓名
string sex; //性別
int sex_choice; //性別選項
public:
void getBaseinformation();
friend class Output; //友元類,用於輸出角色信息
friend class File; //友元類,將角色信息保存到文檔中
};
//輸入角色名和性別
void Baseinformation::getBaseinformation()
{
int i = 1;
cout << "輸入角色姓名:";
cin >> name;
while (i)
{
cout << "選擇角色性別:";
cout << "1.男 2.女" << endl;
cin >> sex_choice;
switch (sex_choice)
{
case 1:sex = "男"; i = 0; break;
case 2:sex = "女"; i = 0; break;
default:cout << "輸入錯誤,請重新輸入" << endl; break;
}
}
}
class Race :public Baseinformation //派生類,記錄角色的種族、職業
{
protected:
string race; //定義種族變量
string occupation; //定義職業變量
int race_choice;
string skill; //定義初始技能
int skill_choice;
public:
void getRace();
friend class Output;
friend class File;
void choiceSkill();
};
//選擇種族和職業
void Race::getRace()
{
int i= 1;
while (i)
{
cout << "請選擇種族:" << endl;
cout << "1.人類 2.精靈 3.獸人 4.矮人 5.元素" << endl;
cin >> race_choice;
switch (race_choice)
{
case 1:race = "人類"; i = 0; break; //輸入選項符合要求,將i置爲0,跳出while循環
case 2:race = "精靈"; i = 0; break;
case 3:race = "獸人"; i = 0; break;
case 4:race = "矮人"; i = 0; break;
case 5:race = "元素"; i = 0; break;
default:cout << "輸入錯誤,請重新輸入!" << endl<<endl; break;
}
}
while (1)
{
cout << "可以使用的職業:" << endl;
switch (race_choice)
{
case 1: cout << "1.狂戰士 2.聖騎士 3.刺客 4.獵手 5.祭司 6.巫師" << endl; break;
case 2: cout << "3.刺客 4.獵手 5.祭司 6.巫師" << endl; break;
case 3: cout << "1.狂戰士 4.獵手 5.祭司 " << endl; break;
case 4: cout << "1.狂戰士 2.聖騎士 5.祭司 " << endl; break;
case 5: cout << "5.祭司 6.巫師" << endl; break;
}
cin >> occupation_choice;
if (race_choice == 1 && (occupation_choice >= 1 && occupation_choice <= 6)) break; //判斷對應的種族下是否有對應的職業
else if (race_choice == 2 && (occupation_choice > 2 && occupation_choice < 7)) break; //以及限制不同的種族下可選擇的職業
else if (race_choice == 3 && (occupation_choice == 1 || occupation_choice == 4 || occupation_choice == 5)) break;
else if (race_choice == 4 && (occupation_choice == 1 || occupation_choice == 2 || occupation_choice == 5)) break;
else if (race_choice == 5 && (occupation_choice > 4 && occupation_choice < 7)) break;
else cout << "輸入錯誤,請重新輸入" << endl;
}
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 = "祭司";
if (occupation_choice == 6) occupation = "巫師";
if (occupation_choice==1&&race_choice==3) lucky=1;
if (occupation_choice==2&&race_choice==1) lucky=2;
if (occupation_choice==3&&race_choice==1) lucky=3;
if (occupation_choice==4&&race_choice==4) lucky=4;
if (occupation_choice==5&&race_choice==2) lucky=5;
if (occupation_choice==6&&race_choice==5) lucky=6;
}
void Race::choiceSkill()
{
int i= 1;
while (i)
{
cout << "請選擇初始技能:" << endl;
cout << "1.治療 2.短時間飛行 3.短時間隱身 4.小範圍瞬移 5.反彈" << endl;
cin >> skill_choice;
switch (skill_choice)
{
case 1:skill = "治療"; i = 0; break; //輸入選項符合要求,將i置爲0,跳出while循環
case 2:race = "短時間飛行"; i = 0; break;
case 3:race = "短時間隱身"; i = 0; break;
case 4:race = "小範圍瞬移"; i = 0; break;
case 5:race = "反彈"; i = 0; break;
default:cout << "輸入錯誤,請重新輸入!" << endl<<endl; break;
}
}
}
class Attribute :public Race //派生類,記錄角色的屬性
{
protected:
int strength; //力量
int agility; //敏捷
int physical; //體力
int intelligence; //智力
int wisdom; //智慧
int HP; //生命值
int MP; //法力值
public:
void getAttribute();
void getRandom(int a, int b, int c, int d, int e);
friend class Output;
friend class File;
};
// 隨機生成每項屬性的值,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; //rand()%10獲得範圍0-9的一個隨機數
agility = b + rand() % 10;
physical = c + rand() % 10;
intelligence = d + rand() % 10;
sum = strength + agility + physical + intelligence;
} while (sum>99); //限制智力這個屬性值得最小值爲1
wisdom = 100 - sum;
HP = physical * 20;
MP = (wisdom + intelligence) * 10;
if (lucky==1) //獸人狂戰士
{
cout<<"你選擇了最佳職業種族配比"<<endl;
cout<<"獲得屬性加成:力量+20,敏捷+5,體力+20"<<endl;
strength+=20;
agility+=5;
physical+=20;
}
if (lucky==2) //人類聖騎士
{
cout<<"你選擇了最佳職業種族配比"<<endl;
cout<<"獲得屬性加成:智慧+20,生命值+10,敏捷+5"<<endl;
wisdom+=20;
agility+=5;
HP+=10;
}
if (lucky==3) //人類刺客
{
cout<<"你選擇了最佳職業種族配比"<<endl;
cout<<"獲得屬性加成:智慧+20,敏捷+30"<<endl;
wisdom+=20;
agility+=30;
}
if (lucky==4) //矮人獵手
{
cout<<"你選擇了最佳職業種族配比"<<endl;
cout<<"獲得屬性加成:力量+10,敏捷+20,生命值+5"<<endl;
strength+=10;
wisdom+=20;
agility+=20;
}
if (lucky==5) //精靈祭司
{
cout<<"你選擇了最佳職業種族配比"<<endl;
cout<<"獲得屬性加成:智慧+20,法力值+20"<<endl;
wisdom+=20;
MP+=20;
}
if (lucky==6) //元素巫師
{
cout<<"你選擇了最佳職業種族配比"<<endl;
cout<<"獲得屬性加成:智慧+20,法力值+20"<<endl;
wisdom+=20;
MP+=20;
}
}
//根據選擇的職業,向getRamdom傳不同的最小值
void Attribute::getAttribute()
{
if (occupation_choice == 1) getRandom(35, 15, 25, 0, 10);
if (occupation_choice == 2) getRandom(20, 10, 25, 15, 15);
if (occupation_choice == 3) getRandom(15, 30, 15, 10, 15);
if (occupation_choice == 4) getRandom(10, 35, 10, 5, 25);
if (occupation_choice == 5) getRandom(10, 25, 10, 30, 20);
if (occupation_choice == 6) 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 << "-----------------------------" << endl;
cout << "姓名:" << t1.name << endl;
cout << "性別:" << t1.sex << endl;
cout << "種族:" << t2.race << endl;
cout << "職業:" << t2.occupation << endl;
cout << "力量:" << t3.strength << endl;
cout << "敏捷:" << t3.agility << endl;
cout << "體力:" << t3.physical << endl;
cout << "智力:" << t3.intelligence << endl;
cout << "智慧:" << t3.wisdom << endl;
cout << "生命值:" << t3.HP << endl;
cout << "法力值:" << t3.MP << endl;
cout << "初始技能:"<< t2.skill <<endl;
cout << "-----------------------------" << endl;
}
class File //將角色信息保存到文檔
{
public:
void file(Baseinformation &, Race &, Attribute &);
};
void File::file(Baseinformation &t1, Race &t2, Attribute &t3)
{
ofstream f;
f.open("角色表.txt",ios::app);
if(!f){
cout<<"打開文件失敗!"<<endl;
exit(0);
}
SYSTEMTIME sys;
GetLocalTime( &sys );
f<<"————————"<<sys.wYear<<"年"<<sys.wMonth<<"月"<<sys.wDay<<"日"<<sys.wHour<<"時"<<sys.wMinute<<"分"<<sys.wSecond<<"秒"<<"——————————————"<<endl;
f << "姓名:" << t1.name << endl;
f << "性別:" << t1.sex << endl;
f << "種族:" << t2.race << endl;
f << "職業:" << t2.occupation << endl;
f << "力量:" << t3.strength << endl;
f << "敏捷:" << t3.agility << endl;
f << "體力:" << t3.physical << endl;
f << "智力:" << t3.intelligence << endl;
f << "智慧:" << t3.wisdom << endl;
f << "生命值:" << t3.HP << endl;
f << "法力值:" << t3.MP << endl;
f << "初始技能:"<< t2.skill <<endl;
}
void help()
{
cout<<endl<<"歡迎來到龍之谷,以下是創建角色規則-----------------------------"<<endl<<endl;
cout<<"種族/職業 狂戰士 聖騎士 刺客 獵手 祭司 巫師"<<endl;
cout<<" 人類 允許 允許 允許 允許 允許 允許"<<endl;
cout<<" 精靈 不允許 不允許 允許 允許 允許 允許"<<endl;
cout<<" 獸人 允許 不允許 不允許 允許 允許 不允許"<<endl;
cout<<" 矮人 允許 允許 不允許 不允許 允許 不允許"<<endl;
cout<<" 元素 不允許 不允許 不允許 不允許 允許 允許"<<endl;
cout<<endl<<"以下是對應種族最適合職業:------------------------------"<<endl<<endl;
cout<<"種族/職業 狂戰士 聖騎士 刺客 獵手 祭司 巫師"<<endl;
cout<<" 獸人 人類 人類 矮人 精靈 元素"<<endl;
cout<<"如果你選擇的種族和職業特別匹配,那麼你將獲得額外的屬性加成"<<endl<<endl;
cout<<"狂戰士:力量+20,敏捷+5,體力+20"<<endl;
cout<<"聖騎士:智慧+20,生命值+10,敏捷+5"<<endl;
cout<<"刺客:智慧+20,敏捷+30"<<endl;
cout<<"獵手:力量+10,敏捷+20,生命值+5"<<endl;
cout<<"祭師:智慧+20,法力值+20"<<endl;
cout<<"巫師:智慧+20,法力值+20"<<endl;
}
int main()
{
cout<<" 歡迎來到遊戲龍之谷"<<endl;
cout<<"請按照提示完成遊戲角色的創建"<<endl<<endl;
Baseinformation player;
Race player_race;
Attribute player_att;
Output player_show;
File save;
int player_choice;
int choice;
while(1){
cout<<endl<<"請選擇功能:1.創建角色 2.使用說明"<<endl;
cin>>choice;
switch(choice){
case 1:
do
{
player.getBaseinformation();
player_race.getRace();
player_race.choiceSkill();
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);
cout<<"恭喜您,角色已經成功建立!"<<endl;
exit(-1);
case 2:
help();
break;
}
}
return 0;
}
四、結果截圖
小聲bb:第一次用markdown編輯寫的,決定以後寫博客也都這樣了,順便練習自己的html