Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <time.h>
- #include <stdlib.h>
- #include <conio.h>
- using namespace std;
- void clrscr() {
- system("@cls||clear");
- }
- class Player {
- protected:
- string username;
- int score;
- public:
- Player() {
- score = 0;
- }
- string getName() {
- return username;
- }
- int getScore() {
- return score;
- }
- void resetScore() {
- score = 0;
- }
- };
- class Game: public Player {
- public:
- void setName(string inputName) {
- username = inputName;
- }
- void addScore(int adds) {
- score = score + adds;
- }
- // Deklarasi method gameInit & gamePlay, konten game bergantung pada method turunan
- virtual void gameInit() {}
- virtual void gamePlay() {}
- void showScoreMenu() {
- clrscr();
- if(username == "") {
- cout << "Anda sama sekali belum bermain" << endl;
- cout << "Tekan tombol untuk kembali" << endl;
- getch();
- showMainMenu();
- } else {
- cout << "Skor dalam permainan terakhir" << endl;
- cout << "Nama pemain: " << getName() << endl;
- cout << "Skor: " << getScore() << endl;
- cout << "Tekan tombol untuk kembali" << endl;
- getch();
- showMainMenu();
- }
- }
- void showMainMenu() {
- int pil=0;
- do {
- clrscr();
- cout << "\t\t===========================================\n" << endl;
- cout << "\t\tSelamat datang di permainan Quiz Matematika\n" << endl;
- cout << "\t\t===========================================\n" << endl;
- cout << "\n\nMenu :" << endl;
- cout << "1. Mulai permainan" << endl;
- cout << "2. Lihat skor" << endl;
- cout << "3. Keluar permainan" << endl;
- cout << "\n\n> Pilih menu ke : ";
- cin >> pil;
- } while(pil < 1 || pil > 3);
- switch(pil) {
- case 1:
- gameInit();
- break;
- case 2:
- showScoreMenu();
- break;
- case 3:
- break;
- }
- }
- };
- class MathQuiz: public Game
- {
- private:
- int keyAnswer;
- public:
- int calc(int op, int x, int y) {
- switch(op) {
- case 0:
- return x + y;
- break;
- case 1:
- return x - y;
- break;
- case 2:
- return x * y;
- break;
- case 3:
- return x / y;
- break;
- }
- }
- string strOps(int n) {
- switch(n) {
- case 0:
- return "+";
- break;
- case 1:
- return "-";
- break;
- case 2:
- return "*";
- break;
- case 3:
- return "/";
- break;
- }
- }
- void createQuestion() {
- clrscr();
- int x = rand() % 10 + 1;
- int y = rand() % 10 + 1;
- int z = rand() % 10 + 1;
- int n = rand() % 4;
- int m = rand() % 4;
- string op_n = strOps(n);
- string op_m = strOps(m);
- keyAnswer = calc(n, x, y);
- keyAnswer = calc(m, keyAnswer, z);
- cout << "Pertanyaan :\n" << endl;
- cout << "[" << x << " " + op_n + " " << y << " " + op_m + " " << z << "]" << endl;
- }
- void gameInit() {
- clrscr();
- string inputName;
- cout << "Selamat bermain!\n" << endl;
- cout << "Untuk melanjutkan silahkan masukkan nama anda: ";
- cin >> inputName;
- setName(inputName);
- resetScore();
- this->gamePlay();
- }
- void gamePlay() {
- int answer;
- createQuestion();
- cout << "\nJawaban anda : ";
- cin >> answer;
- if(isAnswerTrue(answer)) {
- cout << "\nSelamat, jawaban anda benar! :D" << endl;
- addScore(1);
- cout << "Skor semenetara : " << getScore() << endl;
- cout << "Tekan tombol untuk kembali ke menu";
- getch();
- gamePlay();
- } else {
- cout << "Maaf, jawaban anda salah ! :(" << endl;
- cout << "Skor anda: " << getScore() << endl;
- cout << "Tekan tombol untuk kembali ke menu";
- getch();
- showMainMenu();
- }
- }
- bool isAnswerTrue(int answer) {
- return answer == keyAnswer;
- }
- };
- int main() {
- srand(time(NULL));
- Game *game;
- MathQuiz mathGame;
- game = &mathGame;
- game->showMainMenu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment