C语言版RPG角色生成器
1.功能描述
? ? 幾乎所有的RPG游戲(一種源自《龍與地下城》的游戲類型)在進入游戲時都會讓用戶自己來創(chuàng)建自己喜歡的角色,要求編寫一個簡化的創(chuàng)建游戲角色的程序。
?
?
2.游戲角色應(yīng)有的屬性
?
? ? 本題目要求的游戲角色應(yīng)有以下屬性:名字、性別、種族、職業(yè)、力量、敏捷、體力、智力、智慧、生命值和魔法值。
? ? 名字:不超過50個字符。
? ? 性別:可以選擇男性和女性。
? ? 種族:一共可選五個種族,人類、精靈、獸人、矮人和元素。
? ? 職業(yè):可選六種職業(yè),狂戰(zhàn)士、圣騎士、刺客、獵手、祭司和巫師。
? ? 其余屬性均為整數(shù)。
? ? 本題目要求首先用戶輸入角色姓名,然后由用戶選擇角色性別,然后由用戶選擇種族,然后選擇職業(yè),然后自動分配力量、敏捷、體力、智力和智慧屬性,并計算生命值和魔法值。
? ? 生命值=體力*20。
? ? 魔法值=(智力+智慧)*10。
?
3.職業(yè)限制
? ? 很多職業(yè)會限制某些種族選擇,例如獸人不能就職圣騎士等等,種族和職業(yè)的限制表如下:
?
?
? ? 所以在要求用戶選擇職業(yè)時,輸出信息里面只能有用戶所選擇種族可以就職的職業(yè)。
?
4.初始屬性
? ? 本題目要求力量、敏捷、體力、智力和智慧要求是隨機值(利用隨機數(shù)函數(shù)來取得隨機數(shù)),但是五項屬性的總和應(yīng)該是100,并且應(yīng)該和職業(yè)相關(guān)。例如狂戰(zhàn)士的體力和力量就要比較高,而巫師需要較高的智力,而祭司則需要較高的智慧。各職業(yè)初始屬性的大致比例應(yīng)遵從下表:
?
?
? ? 例如,前面示意圖中的祭司的初始屬性,大致滿足該比例,但是應(yīng)該是隨機的。然后利用屬性值計算生命值和魔法值。
?
5.顯示信息
? ? 最后向用戶顯示該角色的所有信息,然后詢問用戶是否滿意,如用戶不滿意則重新創(chuàng)建,若用戶滿意則程序結(jié)束,并將用戶創(chuàng)建角色的相關(guān)信息寫入文件保存。
?
?
6.源代碼
// RPG.cpp : 定義控制臺應(yīng)用程序的入口點。 //功能:編寫一個簡化的創(chuàng)建游戲角色的程序 //作者:馬露露 //編譯器:VS2015 //時間:2017.04.21 //修改:2017.05.02#include "stdafx.h"#include<iostream> #include<string> #include<ctime> #include<fstream> #include<iomanip> using namespace std;int occupation_choice; //玩家所選擇的職業(yè)的序號//基礎(chǔ)類,保存角色的姓名,性別 class Baseinfoamation { protected:char name[50];string sex;int sex_choice; public:void getBaseinfoamation();friend class Output; //友元類,用于輸出角色信息friend class File; //友元類,將角色信息保存到文檔中 };//輸入角色名和性別 void Baseinfoamation::getBaseinfoamation() {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;}} }//基類,記錄角色的種族、職業(yè) class Race { protected:char name[50];string sex;int sex_choice;string race;string occupation;int race_choice; public:void getBaseinfoamation();friend class Output; //友元類,用于輸出角色信息friend class File; //友元類,將角色信息保存到文檔中void getRace();friend class Output;friend class File; };//選擇種族和職業(yè) 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 << "請選擇您的職業(yè)(0狂戰(zhàn)士,1圣騎士,2刺客,3獵手,4祭司,5巫師):" << endl;switch (race_choice){case 0:cout << "0狂戰(zhàn)士 1圣騎士 2刺客 3獵手 4祭司 5巫師" << endl;break;case 1:cout << "2刺客 3獵手 4祭司 5巫師" << endl;break;case 2:cout << "0狂戰(zhàn)士 3獵手 4祭司 " << endl;break;case 3:cout << "0狂戰(zhàn)士 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;elsecout << "輸入錯誤,請重新輸入" << endl;}if (occupation_choice == 0)occupation = "狂戰(zhàn)士";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 physical; //體力int intelligence; //智力int wisdom; //智慧int HP; //生命值int MP; //法力值 public:void getBaseinfoamation();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; };//隨機生成每項屬性的值,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;physical = c + rand() % 10;intelligence = d + rand() % 10;sum = strength + agility + physical + intelligence;} while (((100 - e)<sum) && (sum<100));wisdom = 100 - sum;HP = physical * 20;MP = (wisdom + intelligence) * 10; }//根據(jù)選擇的職業(yè),向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(Baseinfoamation &, Race &, Attribute &); };void Output::show(Baseinfoamation &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) << "職業(yè)" << 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.physical << 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(Baseinfoamation &, Race &, Attribute &); };void File::file(Baseinfoamation &t1, Race &t2, Attribute &t3) {ofstream outfile;outfile.open("data.dat", ios::trunc);outfile << "姓名:" << t1.name << endl;outfile << "性別:" << t1.sex << endl;outfile << "種族:" << t2.race << endl;outfile << "職業(yè):" << t2.occupation << endl;outfile << "力量:" << t3.strength << endl;outfile << "敏捷:" << t3.agility << endl;outfile << "體力:" << t3.physical << endl;outfile << "智力:" << t3.intelligence << endl;outfile << "智慧:" << t3.wisdom << endl;outfile << "生命值:" << t3.HP << endl;outfile << "法力值:" << t3.MP << endl; }//主函數(shù) int main() {Baseinfoamation player;Race player_race;Attribute player_att;Output player_show;File save;int player_choice;do{player.getBaseinfoamation();player_race.getRace();player_att.getAttribute();player_show.show(player, player_race, player_att);cout << endl;cout << "是否繼續(xù)生成游戲角色?" << endl;cout << "0.結(jié)束生成 1.繼續(xù)生成" << endl;cin >> player_choice;} while (player_choice);save.file(player, player_race, player_att);return 0; }?
7.類圖
總結(jié)
以上是生活随笔為你收集整理的C语言版RPG角色生成器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STM32F4_USART配置及细节描述
- 下一篇: 面试必备:HashMap底层数据结构?j