Advertisement
Guest User

Game

a guest
Jun 23rd, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. //header
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <conio.h>
  8. using namespace std;
  9.  
  10. //function init
  11. int getWhatTheyWant();
  12. void displayItems(int);
  13. void addItem(string,double);
  14. string getHighScore();
  15. void setHighScore(int);
  16.  
  17. //main function
  18. int main(){
  19. int whatTheyWant;
  20. whatTheyWant = getWhatTheyWant();
  21. while(whatTheyWant!=9){
  22. switch(whatTheyWant) {
  23. case 1:
  24. displayItems(1);
  25. break;
  26. case 2:
  27. displayItems(2);
  28. break;
  29. case 3:
  30. displayItems(3);
  31. break;
  32. case 4:
  33. {
  34. string itemName;
  35. double itemPower;
  36. cout << "Enter item name" << endl;
  37. cin >> itemName;
  38. cout << "Enter item power" << endl;
  39. cin >> itemPower;
  40. addItem(itemName,itemPower);
  41. }
  42. break;
  43. case 5:
  44. //the actual game
  45.  
  46. {
  47. srand(time(0));
  48. int health=100;
  49. string fightName;
  50. double fightPow;
  51. int score=0;
  52. ifstream objectFile("objects.txt");
  53. string objName;
  54. double objPow;
  55. int objNum=0;
  56. while(objectFile >> objName >> objPow) {
  57. objNum++;
  58. }
  59. if (objNum==0) {
  60. cout << endl;
  61. cout << "You really want to fight in an empty dungeon?" << endl;
  62. cout << endl;
  63. break;
  64. }
  65. cout << "Monsters in dungeon: " << objNum << endl;
  66. getch();
  67. while(health>0){
  68. cout<<"Health: " << health<<endl;
  69. cout<<"Score: " << score<<endl;
  70. getch();
  71. int monster=1+(rand()%objNum);
  72. int monster2=0;
  73. ifstream objectFile("objects.txt");
  74. while(objectFile >> objName >> objPow) {
  75. monster2++;
  76. if (monster2==monster){
  77. fightName=objName;
  78. fightPow=objPow;
  79. }
  80. }
  81. cout << "a(n) " << fightName << " is in the next dungeon room."<<endl;
  82. if(fightPow==0){
  83. cout << "the " << fightName << " does nothing."<<endl;
  84. }
  85. if(fightPow>0){
  86. cout << "the " << fightName << " heals you for "<<fightPow<<endl;
  87. health+=fightPow;
  88. }
  89. if(fightPow<0){
  90. cout << "the " << fightName << " hits you for "<<fightPow<<endl;
  91. health+=fightPow;
  92. }
  93. score+=1;
  94. getch();
  95. }
  96. cout << "Game over" << endl;
  97. getch();
  98. cout << "Final Score: " << score << endl;
  99. setHighScore(score);
  100. }
  101.  
  102. //end of actual game
  103.  
  104. break;
  105. case 6:
  106. {
  107. cout << endl;
  108. ofstream objectFile("objects.txt");
  109. objectFile << "";
  110. objectFile.close();
  111. cout << "Monsters cleared!" << endl;
  112. }
  113. break;
  114. case 7:
  115. cout <<endl;
  116. cout <<"Highscore: " << getHighScore() << endl;
  117. cout <<endl;
  118. break;
  119. case 8:
  120. {
  121. ofstream highWrite("highscore.txt");
  122. highWrite << 0;
  123. highWrite.close();
  124. cout << "\nHighscore cleared!\n" << endl;
  125. }
  126. break;
  127. default:
  128. cout << "Please enter a valid input!" << endl;
  129. break;
  130. }
  131. whatTheyWant = getWhatTheyWant();
  132. }
  133. cout << endl << "Thanks for playing!" <<endl;
  134. return 0;
  135. }
  136.  
  137. //getwhattheywant function
  138. int getWhatTheyWant(){
  139. int choice;
  140. cout << endl;
  141. cout << "Enter what you want: " << endl;
  142. cout << "1- Plain Monsters" << endl;
  143. cout << "2- Helpful Monsters" << endl;
  144. cout << "3- Harmful Monsters" << endl;
  145. cout << "4- Add Monster" << endl;
  146. cout << "5- Play Game" << endl;
  147. cout << "6- Clear Monsters" << endl;
  148. cout << "7- Show Highscore" << endl;
  149. cout << "8- Clear Highscore" << endl;
  150. cout << "9- Quit Game" << endl;
  151. cout << endl;
  152. cin >> choice;
  153. return choice;
  154. }
  155.  
  156. //displayitems function
  157. void displayItems(int x){
  158. cout << endl;
  159. ifstream objectFile("objects.txt");
  160. string name;
  161. double power;
  162. switch(x){
  163. case 1:
  164. while(objectFile >> name >> power) {
  165. if(power==0) {
  166. cout << name << " " << power << endl;
  167. }
  168. }
  169. break;
  170. case 2:
  171. while(objectFile >> name >> power) {
  172. if(power>0) {
  173. cout << name << " " << power << endl;
  174. }
  175. }
  176. break;
  177. case 3:
  178. while(objectFile >> name >> power) {
  179. if(power<0) {
  180. cout << name << " " << power << endl;
  181. }
  182. }
  183. }
  184. }
  185. //additem function
  186. void addItem(string n, double x) {
  187. ofstream objectAddFile("objects.txt",ios::app);
  188. objectAddFile << n << " " << x << endl;
  189. objectAddFile.close();
  190. }
  191.  
  192. //gethighscore function
  193. string getHighScore() {
  194. ifstream highFile("highscore.txt");
  195. string highScore;
  196. getline(highFile,highScore);
  197. return highScore;
  198. highFile.close();
  199. }
  200.  
  201. //sethighscore function
  202. void setHighScore(int x) {
  203. ifstream highFile("highscore.txt");
  204. string highScore;
  205. getline(highFile,highScore);
  206. highFile.close();
  207. int value = atoi(highScore.c_str());
  208. if (x>value){
  209. ofstream highWrite("highscore.txt");
  210. highWrite << x;
  211. highWrite.close();
  212. cout << "You made the new highscore!" << endl;
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement