Guest User

Untitled

a guest
Nov 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. #include "GameSystem.h"
  2.  
  3. GameSystem::GameSystem() {
  4. Start();
  5. }
  6.  
  7. void GameSystem::Start() {
  8. while (true) {
  9. PrintMenu();
  10. string menuChoiceStr;
  11. int menuChoice;
  12. getline(cin, menuChoiceStr);
  13. //檢查輸入的選項是否為正整數
  14. if (!isUnsignedNumber(menuChoiceStr)) {
  15. cout << "指令錯誤,請重新輸入。" << endl << endl;
  16. continue;
  17. }
  18. stringstream menuChoiceSS(menuChoiceStr);
  19. menuChoiceSS >> menuChoice;
  20. //選項最多4個
  21. if (!(menuChoice >= 1 && menuChoice <= 5)) {
  22. cout << "指令錯誤,請重新輸入。" << endl << endl;
  23. continue;
  24. }
  25. //選擇創建角色
  26. if (menuChoice == 1) {
  27. //角色數量最多只能5隻
  28. if (_charactersList.size() >= 5) {
  29. cout << "角色數量已達上限!" << endl << endl;
  30. continue;
  31. }
  32. cout << "請輸入角色名稱 : ";
  33. string name;
  34. int point = 5, str = 1, dex = 1, con = 1;
  35. //檢查輸入的名稱是否符合規定
  36. while (true) {
  37. getline(cin, name);
  38. if (!checkName(name)) {
  39. cout << "名稱不合規定,請重新輸入 : ";
  40. continue;
  41. }
  42. break;
  43. }
  44. //分配創建出來角色的屬性
  45. string createAttrChoiceStr;
  46. int createAttrChoice;
  47. while (point>0) {
  48. cout << endl;
  49. cout << "1. 目前STR : " << str << endl
  50. << "2. 目前DEX : " << dex << endl
  51. << "3. 目前CON : " << con << endl;
  52. cout << "目前剩餘點數 : " << point << endl;
  53. cout << "請選擇欲增加之屬性 : ";
  54. getline(cin, createAttrChoiceStr);
  55. if (!isUnsignedNumber(createAttrChoiceStr)) {
  56. cout << "指令錯誤,請重新輸入。" << endl << endl;
  57. continue;
  58. }
  59. stringstream createAttrChoiceSS(createAttrChoiceStr);
  60. createAttrChoiceSS >> createAttrChoice;
  61. if (!(createAttrChoice >= 1 && createAttrChoice <= 3)) {
  62. cout << "指令錯誤,請重新輸入。" << endl << endl;
  63. continue;
  64. }
  65. switch (createAttrChoice) {
  66. case 1:
  67. str++;
  68. break;
  69. case 2:
  70. dex++;
  71. break;
  72. case 3:
  73. con++;
  74. break;
  75. }
  76. point--;
  77. }
  78. Character hero(name, str, dex, con);
  79. _charactersList.push_back(hero); //將創建出來的角色存放至Vector
  80. cout << "創建成功!\n" << endl << endl;
  81. cout << hero.getInformation() << endl; //顯示創建的角色資訊
  82. }
  83. //角色作戰
  84. else if (menuChoice == 2) {
  85. //沒有角色可以作戰
  86. if (_charactersList.size() == 0) {
  87. cout << "沒有角色可供選擇!" << endl;
  88. continue;
  89. }
  90. cout << endl;
  91. string fightChoiceStr;
  92. int fightChoice;
  93. while (true) {
  94. //列出所有角色
  95. for (int i = 0; i<(int)_charactersList.size(); i++)
  96. {
  97. cout << (i + 1) << ". " << _charactersList[i].getName() << endl;
  98. }
  99. //選擇角色作戰
  100. cout << "請選擇角色進行作戰 : ";
  101. getline(cin, fightChoiceStr);
  102. if (!isUnsignedNumber(fightChoiceStr)) {
  103. cout << "指令錯誤,請重新輸入。" << endl << endl;
  104. continue;
  105. }
  106. stringstream fightChoiceSS(fightChoiceStr);
  107. fightChoiceSS >> fightChoice;
  108. if (!(fightChoice >= 1 && fightChoice <= (int)_charactersList.size())) {
  109. cout << "指令錯誤,請重新輸入。" << endl << endl;
  110. continue;
  111. }
  112. break;
  113. }
  114. //系統預設的4隻角色
  115. _monstersList = { Character("哥布林", 1, 6, 1), Character("史萊姆", 1, 1, 6), Character("獸人", 6, 1, 1), Character("大青蛙", 3, 2, 3) };
  116. //隨機挑選一隻和玩家PK
  117. srand((unsigned)time(NULL));
  118. int monsterChoice = rand() % _monstersList.size();
  119. //讓戰鬥時敵人的等級與角色相同,敵人升級時為三者屬性各加 1
  120. int level = _charactersList[fightChoice - 1].getLevel();
  121. if (level > 1) {
  122. level--; //計算敵人與角色的等差
  123. _monstersList[monsterChoice].LevelUP(level, level, level); //更新敵人的屬性
  124. }
  125. //開始戰鬥,分出勝負
  126. //玩家勝利
  127. if (_charactersList[fightChoice - 1].Fight(_monstersList[monsterChoice])) {
  128. cout << _monstersList[monsterChoice].getName() << "倒下了!獲勝的是 : " << _charactersList[fightChoice - 1].getName() << endl << endl;
  129. int point = 3, str = 0, dex = 0 , con = 0;
  130. int exp = _charactersList[fightChoice - 1].getExp();
  131. switch (exp) {
  132. case 100:
  133. case 300:
  134. case 600:
  135. case 1000:
  136. if (_charactersList[fightChoice - 1].getLevel() == 5) {
  137. break;
  138. }
  139. cout << "角色升級了!獲1得新的3點點數可以分到當前屬性上:" << endl << endl;
  140. //角色升級,分配3點點數
  141. string levelUpAttrChoiceStr;
  142. int levelUpAttrChoice;
  143. while (point>0) {
  144. cout << endl;
  145. cout << "1. 目前STR : " << _charactersList[fightChoice - 1].getStr() + str << endl
  146. << "2. 目前DEX : " << _charactersList[fightChoice - 1].getDex() + dex << endl
  147. << "3. 目前CON : " << _charactersList[fightChoice - 1].getCon() + con << endl;
  148. cout << "目前剩餘點數 : " << point << endl;
  149. cout << "請選擇欲增加之屬性 : ";
  150. getline(cin, levelUpAttrChoiceStr);
  151. if (!isUnsignedNumber(levelUpAttrChoiceStr)) {
  152. cout << "指令錯誤,請重新輸入。" << endl << endl;
  153. continue;
  154. }
  155. stringstream levelUpAttrChoiceSS(levelUpAttrChoiceStr);
  156. levelUpAttrChoiceSS >> levelUpAttrChoice;
  157. if (!(levelUpAttrChoice >= 1 && levelUpAttrChoice <= 3)) {
  158. cout << "指令錯誤,請重新輸入。" << endl << endl;
  159. continue;
  160. }
  161. switch (levelUpAttrChoice) {
  162. case 1:
  163. str++;
  164. break;
  165. case 2:
  166. dex++;
  167. break;
  168. case 3:
  169. con++;
  170. break;
  171. }
  172. point--;
  173. }
  174. cout << "\n配點完成!" << _charactersList[fightChoice - 1].getName() << "目前的等級達到" << _charactersList[fightChoice - 1].LevelUP(str, dex, con) << "。" << endl << endl;
  175. cout << _charactersList[fightChoice - 1].getInformation() << endl;
  176. break;
  177. }
  178. }
  179. //玩家戰敗
  180. else {
  181. cout << _charactersList[fightChoice - 1].getName() << "倒下了!獲勝的是 : " << _monstersList[monsterChoice].getName() << endl << endl;
  182. }
  183. }
  184. //列出所有角色的資訊
  185. else if (menuChoice == 3) {
  186. for (Character hero : _charactersList) {
  187. cout << hero.getInformation() << endl;
  188. }
  189. }
  190. //刪除角色
  191. else if (menuChoice == 4) {
  192. if (_charactersList.size() == 0) {
  193. cout << "沒有角色可供選擇!" << endl << endl;
  194. }
  195. string deleteChoiceStr;
  196. int deleteChoice;
  197. while (true) {
  198. cout << endl;
  199. for (int i = 0; i < (int)_charactersList.size(); i++) {
  200. cout << (i + 1) << ". " << _charactersList[i].getName() << endl;
  201. }
  202. cout << "請輸入欲刪除的角色 : " << endl;
  203.  
  204. getline(cin, deleteChoiceStr);
  205. if (!isUnsignedNumber(deleteChoiceStr)) {
  206. cout << "指令錯誤,請重新輸入。" << endl << endl;
  207. continue;
  208. }
  209. stringstream deleteChoiceSS(deleteChoiceStr);
  210. deleteChoiceSS >> deleteChoice;
  211. if (deleteChoice == 0) {
  212. cout << endl;
  213. cout << "不刪除角色,跳回選單。" << endl << endl;
  214. break;
  215. }
  216. else if (deleteChoice >= 1 && deleteChoice <= (int)_charactersList.size()) {
  217. cout << "已刪除角色 : " << _charactersList[deleteChoice - 1].getName() << endl << endl;
  218. _charactersList.erase(_charactersList.begin() + deleteChoice - 1);
  219. }
  220. break;
  221. }
  222.  
  223. }
  224. else {
  225. break;
  226. }
  227. }
  228. }
  229.  
  230. void GameSystem::PrintMenu() {
  231. cout << "1. 創建角色" << endl
  232. << "2. 角色作戰" << endl
  233. << "3. 列出所有角色" << endl
  234. << "4. 刪除角色" << endl
  235. << "5. 離開" << endl;
  236. }
  237.  
  238. //檢查是否為正整數
  239. bool GameSystem::isUnsignedNumber(string str) {
  240. for (char s : str) {
  241. if (!(s >= '0' && s <= '9')) {
  242. return false;
  243. }
  244. }
  245. return true;
  246. }
  247. //檢查輸入的名稱是否符合規定
  248. bool GameSystem::checkName(string name) {
  249. if ((int)name.length() == 0) {
  250. return false;
  251. }
  252. char table[] = { ' ', '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
  253. for (char n : name)
  254. {
  255. for (char t : table)
  256. {
  257. if (n == t)
  258. {
  259. return false;
  260. }
  261. }
  262. }
  263. return true;
  264. }
Add Comment
Please, Sign In to add comment