Advertisement
Guest User

mycode

a guest
Mar 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. #include "Database.h"
  2.  
  3. Database::Database(std::string filePath)
  4. {
  5. pathToDB = filePath;
  6. }
  7.  
  8. std::string Database::getQuestion(std::string progress)
  9. {
  10. std::string question = "";
  11. try {
  12. sqlite::sqlite db(pathToDB);
  13. auto cur = db.get_statement();
  14. cur->set_sql("select content from questions where id = '" + progress + "'");
  15. cur->prepare();
  16. if (cur->step()) {
  17. question = cur->get_text(0);
  18. }
  19. }
  20. catch (sqlite::exception e)
  21. {
  22. std::cerr << e.what() << std::endl;
  23. return "";
  24. }
  25. return question;
  26. }
  27.  
  28. std::string Database::getAnswers(std::string progress)
  29. {
  30. std::string answer = "";
  31. try {
  32. sqlite::sqlite db(pathToDB);
  33. auto cur = db.get_statement();
  34. cur->set_sql("select content from answers where id = '" + progress + "'");
  35. cur->prepare();
  36. if (cur->step()) {
  37. answer = cur->get_text(0);
  38. }
  39. }
  40. catch (sqlite::exception e)
  41. {
  42. std::cerr << e.what() << std::endl;xx
  43. return "";
  44. }
  45. return answer;
  46. }
  47.  
  48. std::vector<Item*> Database::getItems()
  49. {
  50. std::vector<Item*> items;
  51. try {
  52. sqlite::sqlite db(pathToDB);
  53. auto cur = db.get_statement();
  54. cur->set_sql("select * from item");
  55. cur->prepare();
  56. int i = 0;
  57. while (cur->step()) {
  58. std::string name = cur->get_text(1);;
  59. int weight = cur->get_int(2);
  60. std::string consumable = cur->get_text(3);
  61. int price = cur->get_int(4);
  62. int damage = cur->get_int(5);
  63. if (consumable == "TRUE") {//food
  64. items.push_back(new Food(price, weight, name));
  65. }
  66. else if(damage != 0){//weapon
  67. double x = 44 + (price / 3) + (damage / 3);
  68. double r = ((double)rand() / (RAND_MAX));
  69. std::random_device rd;
  70. std::mt19937 gen(rd());
  71. std::uniform_real_distribution<> dis(0, 1);
  72. int durability = (double)(x*dis(gen))+4;
  73. items.push_back(new Weapon(price, weight, name, damage, durability, getType(durability)));
  74. }
  75. else {//item
  76. items.push_back(new Item(price, weight, name));
  77. }
  78. i++;
  79. }
  80. }
  81. catch (sqlite::exception e)
  82. {
  83. std::cerr << e.what() << std::endl;
  84. return items;
  85. }
  86. return items;
  87. }
  88.  
  89. std::string Database::getType(int durability)
  90. {
  91. try {
  92. sqlite::sqlite db(pathToDB);
  93. auto cur = db.get_statement();
  94. cur->set_sql("select * from type");
  95. cur->prepare();
  96. std::string last;
  97. while (cur->step()) {
  98. if (durability < cur->get_int(2))
  99. {
  100. return cur->get_text(1);
  101. }
  102. else {
  103. last = cur->get_text(1);
  104. }
  105. }
  106. return last;
  107. }
  108. catch (sqlite::exception e)
  109. {
  110. std::cerr << e.what() << std::endl;
  111. return "";
  112. }
  113. return "";
  114. }
  115.  
  116.  
  117. --------------------------------
  118.  
  119.  
  120. #define CATCH_CONFIG_MAIN
  121. #include <iostream>
  122. #include <string>
  123. #include <sstream>
  124. #include <algorithm>
  125. #include <thread>
  126. #include <chrono>
  127. #include <iomanip>
  128. #include "catch.hpp"
  129. #include "Database.h"
  130.  
  131. std::string getInput() {
  132. std::string input;
  133. std::cin >> input;
  134. std::transform(input.begin(), input.end(), input.begin(), ::tolower);
  135. while (input.compare("a") != 0 && input.compare("b") != 0 && input.compare("c") != 0)
  136. {
  137. std::cout << input << " is not a valid response. Please try again." << "\n";
  138. std::cin >> input;
  139. std::transform(input.begin(), input.end(), input.begin(), ::tolower);
  140. }
  141. return input;
  142. }
  143.  
  144. SCENARIO("database class can be used to retrieve questions, answers, items, and types")
  145. {
  146. GIVEN("a predefined database with question, answers, item, and type tables")
  147. {
  148. Database db("game.db");
  149. THEN("query a few random questions from the database")
  150. {
  151. REQUIRE(db.getQuestion("1aa") == "Hello?! Can you hear me?!");
  152. REQUIRE(db.getQuestion("1abc") == "Believe me or not, it doesn't matter.");
  153. REQUIRE(db.getQuestion("1abb") == "I don't know, I just tried a random number and here we are talking.");
  154. }
  155. THEN("query a few random answers from the database")
  156. {
  157. REQUIRE(db.getAnswers("1aab") == "A. 'How come? Where are you?'\nB. 'Die? Why?'\nC. 'Can I somehow help you?'");
  158. REQUIRE(db.getAnswers("1abb") == "A. 'It can't be true.'\nB. 'Lucky you.'\nC. 'Oh really?'");
  159. REQUIRE(db.getAnswers("1a") == "A. Answer the call.\nB. Hang up and put the phone in your pocket.\nC. Leave it on the ground and continue going home.");
  160. }
  161. THEN("query the list of item names and compare against our list")
  162. {
  163. std::string item_names[16] = { "Water","Cokecole","Lemonade","Axe","Shovel","Lockpicks","Rations","Cake","Chocolate","Sword","Pebble","Rocks","Dirt","Dead Rat","Screws","Phone" };
  164. int item_prices[16] = { 2,3,3,15,10,2,4,5,2,25,0,0,0,0,2,58 };
  165. int item_damages[16] = { 0,0,0,5,3,0,0,0,0,8,1,2,0,0,0,0 };
  166. bool item_not_consumable[16] = { false, false, false, true, true, true, false, false, false, true, true, true, true, false, true, true };
  167. std::vector<Item*> items = db.getItems();
  168. REQUIRE(items.size() == (sizeof(item_names) / sizeof(*item_names)));
  169. for (int i = 0; i < items.size(); i++) {
  170. REQUIRE(items[i]->name == item_names[i]);
  171. REQUIRE(items[i]->price == item_prices[i]);
  172. //REQUIRE(((Weapon*)items[i])->damage == item_damages[i]);
  173. //REQUIRE(((Food*)items[i])->consumable == item_consumable[i]);
  174. }
  175. }
  176. THEN("query the different types and their values to make sure they generate valid info")
  177. {
  178. REQUIRE(db.getType(5) == "Poor");
  179. REQUIRE(db.getType(14) == "Ok");
  180. REQUIRE(db.getType(22) == "Good");
  181. REQUIRE(db.getType(30) == "Excellent");
  182. REQUIRE(db.getType(38) == "Supreme");
  183. REQUIRE(db.getType(50) == "Supreme");
  184. }
  185. }
  186. }
  187. SCENARIO("test the game")
  188. {
  189. Database db("game.db");
  190.  
  191. bool game_over = false;
  192. int chapter = 1;
  193. std::string progress = "";
  194.  
  195. std::vector<Item*> items = db.getItems();
  196. std::cout << "\n\n";
  197. std::cout << std::setw(28) << std::right << "" << "----TEXTMART----";
  198. std::cout << std::setw(35) << std::right << "\n" << "|____________________________________________________________________|" << "\n\n\n";
  199.  
  200. std::cout
  201. << std::setw(5) << std::right << " "
  202.  
  203. << std::setw(10) << std::left <<
  204. "Name"
  205. << std::setw(10) << std::left <<
  206. "Value"
  207. << std::setw(10) << std::left <<
  208. "Weight"
  209. << std::setw(10) << std::left <<
  210. "Damage"
  211. << std::setw(15) << std::left <<
  212. "Durability"
  213. <<
  214. "Type"
  215. <<
  216. "\n |__________________________________________________________________|\n" << std::endl;
  217.  
  218.  
  219. for (int i = 0; i < items.size(); i++) {
  220. items[i]->print();
  221.  
  222.  
  223. }
  224. std::cout << "\n |___________________________________________________________________|\n\n\n";
  225.  
  226.  
  227.  
  228. while (!game_over) {
  229. std::ostringstream convert;
  230. convert << chapter;
  231. std::string question = db.getQuestion(convert.str() + progress);
  232. std::string answers = db.getAnswers(convert.str() + progress);
  233. std::cout << question << "\n";
  234. if (answers == "") {
  235. std::cout << "\n\n" << "GAME OVER" << "\n\n\n";
  236. game_over = true;
  237. break;
  238. }
  239. else {
  240. std::cout << answers << "\n";
  241. std::string input = getInput();
  242. progress += input;
  243. if (progress.length() == 4) {
  244. progress = "****";
  245. }
  246. }
  247. for (int i = 0; i < 20; i++) {
  248. std::cout << ". ";
  249. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  250. }
  251. std::cout << '\n';
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement