Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "GameSystem.h"
- GameSystem::GameSystem() {
- Start();
- }
- void GameSystem::Start() {
- while (true) {
- PrintMenu();
- string menuChoiceStr;
- int menuChoice;
- getline(cin, menuChoiceStr);
- //檢查輸入的選項是否為正整數
- if (!isUnsignedNumber(menuChoiceStr)) {
- cout << "指令錯誤,請重新輸入。" << endl << endl;
- continue;
- }
- stringstream menuChoiceSS(menuChoiceStr);
- menuChoiceSS >> menuChoice;
- //選項最多4個
- if (!(menuChoice >= 1 && menuChoice <= 5)) {
- cout << "指令錯誤,請重新輸入。" << endl << endl;
- continue;
- }
- //選擇創建角色
- if (menuChoice == 1) {
- //角色數量最多只能5隻
- if (_charactersList.size() >= 5) {
- cout << "角色數量已達上限!" << endl << endl;
- continue;
- }
- cout << "請輸入角色名稱 : ";
- string name;
- int point = 5, str = 1, dex = 1, con = 1;
- //檢查輸入的名稱是否符合規定
- while (true) {
- getline(cin, name);
- if (!checkName(name)) {
- cout << "名稱不合規定,請重新輸入 : ";
- continue;
- }
- break;
- }
- //分配創建出來角色的屬性
- string createAttrChoiceStr;
- int createAttrChoice;
- while (point>0) {
- cout << endl;
- cout << "1. 目前STR : " << str << endl
- << "2. 目前DEX : " << dex << endl
- << "3. 目前CON : " << con << endl;
- cout << "目前剩餘點數 : " << point << endl;
- cout << "請選擇欲增加之屬性 : ";
- getline(cin, createAttrChoiceStr);
- if (!isUnsignedNumber(createAttrChoiceStr)) {
- cout << "指令錯誤,請重新輸入。" << endl << endl;
- continue;
- }
- stringstream createAttrChoiceSS(createAttrChoiceStr);
- createAttrChoiceSS >> createAttrChoice;
- if (!(createAttrChoice >= 1 && createAttrChoice <= 3)) {
- cout << "指令錯誤,請重新輸入。" << endl << endl;
- continue;
- }
- switch (createAttrChoice) {
- case 1:
- str++;
- break;
- case 2:
- dex++;
- break;
- case 3:
- con++;
- break;
- }
- point--;
- }
- Character hero(name, str, dex, con);
- _charactersList.push_back(hero); //將創建出來的角色存放至Vector
- cout << "創建成功!\n" << endl << endl;
- cout << hero.getInformation() << endl; //顯示創建的角色資訊
- }
- //角色作戰
- else if (menuChoice == 2) {
- //沒有角色可以作戰
- if (_charactersList.size() == 0) {
- cout << "沒有角色可供選擇!" << endl;
- continue;
- }
- cout << endl;
- string fightChoiceStr;
- int fightChoice;
- while (true) {
- //列出所有角色
- for (int i = 0; i<(int)_charactersList.size(); i++)
- {
- cout << (i + 1) << ". " << _charactersList[i].getName() << endl;
- }
- //選擇角色作戰
- cout << "請選擇角色進行作戰 : ";
- getline(cin, fightChoiceStr);
- if (!isUnsignedNumber(fightChoiceStr)) {
- cout << "指令錯誤,請重新輸入。" << endl << endl;
- continue;
- }
- stringstream fightChoiceSS(fightChoiceStr);
- fightChoiceSS >> fightChoice;
- if (!(fightChoice >= 1 && fightChoice <= (int)_charactersList.size())) {
- cout << "指令錯誤,請重新輸入。" << endl << endl;
- continue;
- }
- break;
- }
- //系統預設的4隻角色
- _monstersList = { Character("哥布林", 1, 6, 1), Character("史萊姆", 1, 1, 6), Character("獸人", 6, 1, 1), Character("大青蛙", 3, 2, 3) };
- //隨機挑選一隻和玩家PK
- srand((unsigned)time(NULL));
- int monsterChoice = rand() % _monstersList.size();
- //讓戰鬥時敵人的等級與角色相同,敵人升級時為三者屬性各加 1
- int level = _charactersList[fightChoice - 1].getLevel();
- if (level > 1) {
- level--; //計算敵人與角色的等差
- _monstersList[monsterChoice].LevelUP(level, level, level); //更新敵人的屬性
- }
- //開始戰鬥,分出勝負
- //玩家勝利
- if (_charactersList[fightChoice - 1].Fight(_monstersList[monsterChoice])) {
- cout << _monstersList[monsterChoice].getName() << "倒下了!獲勝的是 : " << _charactersList[fightChoice - 1].getName() << endl << endl;
- int point = 3, str = 0, dex = 0 , con = 0;
- int exp = _charactersList[fightChoice - 1].getExp();
- switch (exp) {
- case 100:
- case 300:
- case 600:
- case 1000:
- if (_charactersList[fightChoice - 1].getLevel() == 5) {
- break;
- }
- cout << "角色升級了!獲1得新的3點點數可以分到當前屬性上:" << endl << endl;
- //角色升級,分配3點點數
- string levelUpAttrChoiceStr;
- int levelUpAttrChoice;
- while (point>0) {
- cout << endl;
- cout << "1. 目前STR : " << _charactersList[fightChoice - 1].getStr() + str << endl
- << "2. 目前DEX : " << _charactersList[fightChoice - 1].getDex() + dex << endl
- << "3. 目前CON : " << _charactersList[fightChoice - 1].getCon() + con << endl;
- cout << "目前剩餘點數 : " << point << endl;
- cout << "請選擇欲增加之屬性 : ";
- getline(cin, levelUpAttrChoiceStr);
- if (!isUnsignedNumber(levelUpAttrChoiceStr)) {
- cout << "指令錯誤,請重新輸入。" << endl << endl;
- continue;
- }
- stringstream levelUpAttrChoiceSS(levelUpAttrChoiceStr);
- levelUpAttrChoiceSS >> levelUpAttrChoice;
- if (!(levelUpAttrChoice >= 1 && levelUpAttrChoice <= 3)) {
- cout << "指令錯誤,請重新輸入。" << endl << endl;
- continue;
- }
- switch (levelUpAttrChoice) {
- case 1:
- str++;
- break;
- case 2:
- dex++;
- break;
- case 3:
- con++;
- break;
- }
- point--;
- }
- cout << "\n配點完成!" << _charactersList[fightChoice - 1].getName() << "目前的等級達到" << _charactersList[fightChoice - 1].LevelUP(str, dex, con) << "。" << endl << endl;
- cout << _charactersList[fightChoice - 1].getInformation() << endl;
- break;
- }
- }
- //玩家戰敗
- else {
- cout << _charactersList[fightChoice - 1].getName() << "倒下了!獲勝的是 : " << _monstersList[monsterChoice].getName() << endl << endl;
- }
- }
- //列出所有角色的資訊
- else if (menuChoice == 3) {
- for (Character hero : _charactersList) {
- cout << hero.getInformation() << endl;
- }
- }
- //刪除角色
- else if (menuChoice == 4) {
- if (_charactersList.size() == 0) {
- cout << "沒有角色可供選擇!" << endl << endl;
- }
- string deleteChoiceStr;
- int deleteChoice;
- while (true) {
- cout << endl;
- for (int i = 0; i < (int)_charactersList.size(); i++) {
- cout << (i + 1) << ". " << _charactersList[i].getName() << endl;
- }
- cout << "請輸入欲刪除的角色 : " << endl;
- getline(cin, deleteChoiceStr);
- if (!isUnsignedNumber(deleteChoiceStr)) {
- cout << "指令錯誤,請重新輸入。" << endl << endl;
- continue;
- }
- stringstream deleteChoiceSS(deleteChoiceStr);
- deleteChoiceSS >> deleteChoice;
- if (deleteChoice == 0) {
- cout << endl;
- cout << "不刪除角色,跳回選單。" << endl << endl;
- break;
- }
- else if (deleteChoice >= 1 && deleteChoice <= (int)_charactersList.size()) {
- cout << "已刪除角色 : " << _charactersList[deleteChoice - 1].getName() << endl << endl;
- _charactersList.erase(_charactersList.begin() + deleteChoice - 1);
- }
- break;
- }
- }
- else {
- break;
- }
- }
- }
- void GameSystem::PrintMenu() {
- cout << "1. 創建角色" << endl
- << "2. 角色作戰" << endl
- << "3. 列出所有角色" << endl
- << "4. 刪除角色" << endl
- << "5. 離開" << endl;
- }
- //檢查是否為正整數
- bool GameSystem::isUnsignedNumber(string str) {
- for (char s : str) {
- if (!(s >= '0' && s <= '9')) {
- return false;
- }
- }
- return true;
- }
- //檢查輸入的名稱是否符合規定
- bool GameSystem::checkName(string name) {
- if ((int)name.length() == 0) {
- return false;
- }
- char table[] = { ' ', '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
- for (char n : name)
- {
- for (char t : table)
- {
- if (n == t)
- {
- return false;
- }
- }
- }
- return true;
- }
Add Comment
Please, Sign In to add comment