jaOjaa

Untitled

Jun 23rd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6.  
  7. using namespace std;
  8.  
  9. void clrscr() {
  10. system("@cls||clear");
  11. }
  12.  
  13. class Player {
  14. protected:
  15. string username;
  16. int score;
  17.  
  18. public:
  19. Player() {
  20. score = 0;
  21. }
  22.  
  23. string getName() {
  24. return username;
  25. }
  26.  
  27. int getScore() {
  28. return score;
  29. }
  30.  
  31. void resetScore() {
  32. score = 0;
  33. }
  34. };
  35.  
  36. class Game: public Player {
  37. public:
  38. void setName(string inputName) {
  39. username = inputName;
  40. }
  41.  
  42. void addScore(int adds) {
  43. score = score + adds;
  44. }
  45.  
  46. // Deklarasi method gameInit & gamePlay, konten game bergantung pada method turunan
  47. virtual void gameInit() {}
  48. virtual void gamePlay() {}
  49.  
  50. void showScoreMenu() {
  51. clrscr();
  52.  
  53. if(username == "") {
  54. cout << "Anda sama sekali belum bermain" << endl;
  55. cout << "Tekan tombol untuk kembali" << endl;
  56. getch();
  57. showMainMenu();
  58. } else {
  59. cout << "Skor dalam permainan terakhir" << endl;
  60. cout << "Nama pemain: " << getName() << endl;
  61. cout << "Skor: " << getScore() << endl;
  62. cout << "Tekan tombol untuk kembali" << endl;
  63. getch();
  64. showMainMenu();
  65. }
  66. }
  67.  
  68. void showMainMenu() {
  69. int pil=0;
  70.  
  71. do {
  72. clrscr();
  73. cout << "\t\t===========================================\n" << endl;
  74. cout << "\t\tSelamat datang di permainan Quiz Matematika\n" << endl;
  75. cout << "\t\t===========================================\n" << endl;
  76. cout << "\n\nMenu :" << endl;
  77. cout << "1. Mulai permainan" << endl;
  78. cout << "2. Lihat skor" << endl;
  79. cout << "3. Keluar permainan" << endl;
  80. cout << "\n\n> Pilih menu ke : ";
  81. cin >> pil;
  82. } while(pil < 1 || pil > 3);
  83.  
  84. switch(pil) {
  85. case 1:
  86. gameInit();
  87. break;
  88. case 2:
  89. showScoreMenu();
  90. break;
  91. case 3:
  92. break;
  93. }
  94. }
  95. };
  96.  
  97. class MathQuiz: public Game
  98. {
  99. private:
  100. int keyAnswer;
  101.  
  102. public:
  103. int calc(int op, int x, int y) {
  104. switch(op) {
  105. case 0:
  106. return x + y;
  107. break;
  108. case 1:
  109. return x - y;
  110. break;
  111. case 2:
  112. return x * y;
  113. break;
  114. case 3:
  115. return x / y;
  116. break;
  117. }
  118. }
  119.  
  120. string strOps(int n) {
  121. switch(n) {
  122. case 0:
  123. return "+";
  124. break;
  125. case 1:
  126. return "-";
  127. break;
  128. case 2:
  129. return "*";
  130. break;
  131. case 3:
  132. return "/";
  133. break;
  134. }
  135. }
  136.  
  137. void createQuestion() {
  138. clrscr();
  139. int x = rand() % 10 + 1;
  140. int y = rand() % 10 + 1;
  141. int z = rand() % 10 + 1;
  142. int n = rand() % 4;
  143. int m = rand() % 4;
  144.  
  145. string op_n = strOps(n);
  146. string op_m = strOps(m);
  147.  
  148. keyAnswer = calc(n, x, y);
  149. keyAnswer = calc(m, keyAnswer, z);
  150.  
  151. cout << "Pertanyaan :\n" << endl;
  152. cout << "[" << x << " " + op_n + " " << y << " " + op_m + " " << z << "]" << endl;
  153. }
  154.  
  155. void gameInit() {
  156. clrscr();
  157.  
  158. string inputName;
  159. cout << "Selamat bermain!\n" << endl;
  160. cout << "Untuk melanjutkan silahkan masukkan nama anda: ";
  161. cin >> inputName;
  162.  
  163. setName(inputName);
  164. resetScore();
  165.  
  166. this->gamePlay();
  167. }
  168.  
  169. void gamePlay() {
  170. int answer;
  171.  
  172. createQuestion();
  173. cout << "\nJawaban anda : ";
  174. cin >> answer;
  175.  
  176. if(isAnswerTrue(answer)) {
  177. cout << "\nSelamat, jawaban anda benar! :D" << endl;
  178. addScore(1);
  179. cout << "Skor semenetara : " << getScore() << endl;
  180. cout << "Tekan tombol untuk kembali ke menu";
  181. getch();
  182. gamePlay();
  183. } else {
  184. cout << "Maaf, jawaban anda salah ! :(" << endl;
  185. cout << "Skor anda: " << getScore() << endl;
  186. cout << "Tekan tombol untuk kembali ke menu";
  187. getch();
  188. showMainMenu();
  189. }
  190. }
  191.  
  192. bool isAnswerTrue(int answer) {
  193. return answer == keyAnswer;
  194. }
  195. };
  196.  
  197. int main() {
  198. srand(time(NULL));
  199. Game *game;
  200. MathQuiz mathGame;
  201. game = &mathGame;
  202.  
  203. game->showMainMenu();
  204.  
  205. return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment