Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. // FighterGame.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6.  
  7. #include <iostream>
  8. #include <cstdlib>
  9. #include <string>
  10. #include <ctime>
  11. #include <random>
  12. #include <fstream>
  13. using namespace std;
  14.  
  15. void setupPlayerStats(string &name, int &str, int &def, int &armor, int &skill, int &wins);
  16. void savePlayerStats(string name, int str, int def, int armor, int skill, int wins);
  17.  
  18. int main() {
  19.  
  20. random_device rd;
  21. uniform_int_distribution<int> d6(1, 6);
  22.  
  23. string player1Name;
  24. int player1Str;
  25. int player1Def;
  26. int player1Armor;
  27. int player1Skill;
  28. int player1Wins;
  29.  
  30. string player2Name;
  31. int player2Str;
  32. int player2Def;
  33. int player2Armor;
  34. int player2Skill;
  35. int player2Wins;
  36.  
  37. cout << "Setup Player 1" << endl;
  38. setupPlayerStats(player1Name, player1Str, player1Def, player1Armor, player1Skill, player1Wins);
  39.  
  40. cout << "Setup Player 2" << endl;
  41. setupPlayerStats(player2Name, player2Str, player2Def, player2Armor, player2Skill, player2Wins);
  42.  
  43. int player1Health = 20;
  44. int player2Health = 20;
  45. int turns = 0;
  46.  
  47. cout << player1Name << " - " << player1Wins << " Wins" << endl;
  48. cout << "- vs. -" << endl;
  49. cout << player2Name << " - " << player2Wins << " Wins" << endl;
  50.  
  51. cout << "FIGHT!";
  52. while (player1Health > 0 && player2Health > 0 && turns < 100) {
  53. int attackRoll;
  54. int damageRoll;
  55.  
  56. // Attack P1 > P2
  57. attackRoll = player1Skill + d6(rd);
  58. if (attackRoll >= 3 + player2Def) {
  59. // Attack hits!
  60. cout << player1Name << " hits " << player2Name << "." << endl;
  61. damageRoll = player1Str + d6(rd) - player2Armor;
  62. if (damageRoll > 0) {
  63. player2Health -= damageRoll;
  64. cout << player2Name << " takes " << damageRoll << " damage." << endl;
  65. }
  66. else {
  67. cout << player2Name << "'s armor blocks the hit." << endl;
  68. }
  69. }
  70. else {
  71. cout << player1Name << " missed." << endl;
  72. }
  73. cout << player1Name << " HP: " << player1Health << endl;
  74. cout << player2Name << " HP: " << player2Health << endl;
  75. turns++;
  76.  
  77. }
  78.  
  79. if ((player1Health > 0 && player2Health > 0) || (player1Health < 0 && player2Health < 0))
  80. {
  81. cout << "Draw!" << endl;
  82. }
  83. else if (player1Health > 0)
  84. {
  85. cout << player1Name << " wins!" << endl;
  86. player1Wins;
  87.  
  88. }
  89. else
  90. {
  91. cout << player2Name << " wins!" << endl;
  92. player2Wins;
  93. }
  94. savePlayerStats(player1Name, player1Str, player1Def, player1Armor, player1Skill, player1Wins);
  95. savePlayerStats(player2Name, player2Str, player2Def, player2Armor, player2Skill, player2Wins);
  96. }
  97.  
  98.  
  99. //cout << player1Name << player1Str << player1Def << player1Armor << player1Skill << player1Wins << endl;
  100.  
  101.  
  102. void setupPlayerStats(string &name, int &str, int &def, int &armor, int &skill, int &wins) {
  103. fstream file;
  104.  
  105. cout << "Name: ";
  106. getline(cin, name);
  107.  
  108. // If they enter no name, use a default
  109. if (name == "") {
  110. name = "John Doe";
  111. }
  112. string filename = name + ".txt";
  113.  
  114. file.open(filename, ios::in);
  115. if (file.is_open()) {
  116. // File doesn't exist, have the user fill it in
  117.  
  118. cout << "File found, loading from file." << endl;
  119.  
  120. file >> str;
  121. file >> def;
  122. file >> armor;
  123. file >> skill;
  124. file >> wins;
  125. }
  126.  
  127. else {
  128. // File exists, read data from file
  129. bool accepted = false;
  130. while (!accepted) {
  131.  
  132. int points = 20;
  133.  
  134. cout << points << " points left." << endl;
  135. cout << "Player Str: ";
  136. cin >> str;
  137. points -= str;
  138.  
  139. cout << points << " points left." << endl;
  140. cout << "Player Def: ";
  141. cin >> def;
  142. points -= def;
  143.  
  144. cout << points << " points left." << endl;
  145. cout << "Player Armor: ";
  146. cin >> armor;
  147. points -= armor;
  148.  
  149. cout << points << " points left." << endl;
  150. cout << "Player Skill: ";
  151. cin >> skill;
  152. points -= skill;
  153.  
  154. wins = 0;
  155. if (points >= 0) {
  156. accepted = true;
  157. }
  158. else {
  159. cout << "You used too many points!" << endl;
  160. }
  161.  
  162. }
  163. }
  164. }
  165.  
  166. void savePlayerStats(string name, int str, int def, int armor, int skill, int wins) {
  167.  
  168. // TODO: Save the player's stats to a file.
  169. fstream file;
  170. string filename = name + ".txt";
  171.  
  172. file.open(filename, ios::out);
  173.  
  174.  
  175. file << str << endl;
  176. file << def << endl;
  177. file << armor << endl;
  178. file << skill << endl;
  179. file << wins << endl;
  180.  
  181. file.close();
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement