#include #include #include #include #include #include "quizproject.h" int* randomNumber() { srand(time(NULL)); int k = 0, p; static int arr[5]; while(k<5){ int num =rand()%5; for (p = 0; p < k; p++){ if(arr[p]==num){ break; } } if(p==k){ arr[k++]=num; } } return arr; } void displayWelcomeMessage(void) { printf("\t\t------------------------------------------\n\n"); printf("\t\t------------------------------------------\n\n"); printf("\t\t Welcome to The Quiz \n\n"); printf("\t\t------------------------------------------\n"); printf("\t\t------------------------------------------\n\n"); printf("\t\t Press 'Enter' to begin the Game \n"); char startGame; scanf("%c", &startGame); startGame = toupper(startGame); if (startGame != ' ') { char name[100] = {0}; printf("\t\t Please enter your First Name \n"); scanf("%s", name); displayGameInstructions(name); } } void displayGoodLuckMessage(void) { printf("\t\t------------------------------------------\n\n"); printf("\t\t------------------------------------------\n\n"); printf("\t\t GOOD LUCK! \n\n"); printf("\t\t------------------------------------------\n"); printf("\t\t------------------------------------------\n\n"); } void displayGameInstructions(char name[]) { printf("\t\t------------------------------------------\n\n"); printf("\t\t------------------------------------------\n\n"); printf("\t\t Hello %s! \n\n", name); printf("\t\t To start this game, here are the Game Instructions\n\n"); printf("\t\t------------------------------------------\n"); printf("\t\t------------------------------------------\n\n"); printf("\t\tYou will be presented with questions from Round 1(Super Easy) to Round 5(Very Hard).\n"); printf("\t\tFor every question you have four options (A, B, C, D).\n"); printf("\t\tFor each question, you can either choose the answer from the four options or use a lifeline.\n\n"); printf("\t\t>>To Pass Through Round 1: You have to get 3 out of 5 correct.<<\n"); printf("\t\t>>To Pass Through Round 2: You have to get 3 out of 5 correct.<<\n"); printf("\t\t>>To Pass Through Round 3: You have to get 4 out of 5 correct.<<\n"); printf("\t\t>>To Pass Through Round 4: You have to get 4 out of 5 correct.<<\n"); printf("\t\t>>To Pass Through Round 5: You have to get 5 out of 5 correct.<<\n\n"); printf("\t\tIf you choose the wrong answer, you lose a point.\n\n"); printf("\t\tYou may lose the game if not you don't have enough points to pass to the next round.\n\n"); printf("\t\tIf you use a lifeline and still choose the wrong answer, you lose the game.\n"); } void displayLifelineInstructions(void) { printf("\t\t>>You have the following lifelines available:<< \n"); printf("\t\t>>'F' 50-50: Eliminates two incorrect answers.<< \n"); printf("\t\t>>'H' Hint: Reveals a hint for the answer.<<\n"); } void displayInstructions(void) { displayWelcomeMessage(); displayLifelineInstructions(); displayGoodLuckMessage(); } void correct(int score2){ printf("\nCorrect!\n"); printf("You have %d points now\n\n", score); } void wrong(int score1){ printf("\nwrong!\n"); printf("You have %d points now\n\n", score); } void fifty_fifty(int question_num) { char qC[][100] = { "A.Mount Everest\nB.Uludag", "A.USA\nB.China", "C.Amazon River\nD.The Nile River", }; printf("%s\n",qC[question_num]); } void printOutOption(char qS[][100], char qC[][100],char aS[], int initial, int end, int ranArr[]){ char response; for (int i = initial; i < end; i++){ int randNum = ranArr[i]; printf("[%d] %s\n", i+1, qS[randNum]); printf("%s\n", qC[randNum]); printf("Enter your answer (A, B, C, or D) or use a lifeline (F for 50/50, H for hint): "); scanf(" %c", &response); response = toupper(response); if (response == 'F') { fifty_fifty(randNum); printf("Enter your answer (A, B, C, or D): "); scanf(" %c", &response); response = toupper(response); } else if (response == 'H') { hint(randNum); printf("Enter your answer (A, B, C, or D): "); scanf(" %c", &response); response = toupper(response); } if(response == aS[randNum]){ score++; correct(score); } else { wrong(score); } } } void looping(char qS[][100], char qC[][100], char aS[], int randArr[]){ int k = 0; while(k < 1){ printf("\t\t--------------------Round 1-------------------\n\n"); printf(">>In this round you need to answer 3 questions correctly to move to Round 2<<\n\n"); int initial = 0, end = 1; printOutOption(qS,qC,aS,initial,end,randArr); if (score < 1) { printf("You lost! Better luck next time\n"); break; } else { printf("\t\t--------------------Round 2-------------------\n\n"); printf(">>In this round you need to answer 2 questions correctly to move to Round 3<<\n\n"); initial = 1, end = 3; printOutOption(qS,qC,aS,initial,end,randArr); } if (score < 1) { printf("You lost! Better luck next time\n"); break; } else { printf("\t\t--------------------Round 3-------------------\n\n"); printf(">>In this round you need to answer 2 questions correctly to move to Round 4<<\n\n"); initial = 3, end = 5; printOutOption(qS,qC,aS,initial,end,randArr); } k++; } if (score < 2) { printf("\nYour final score: %d\n", score); } } void ask_questions(void) { char qS[][100] = { "What is the name of the tallest mountain in the world?\n", "Which country has the largest population in the world??\n", "What is the name of the longest river in Africa?\n", "What American city is the Golden Gate Bridge located in?\n", "What is the capital of Mexico?\n", }; char qC[][100] = {//6 "A.Mount Everest\nB.Uludag\nC.K2\nD.Makalu", "A.USA\nB.China\nC.Russia\nD.India", "A.Missisipi River\nB.Yangtze River\nC.Amazon River\nD.The Nile River", "A.San Fransisco\nB.San Diego\nC.New York\nD.Los Angeles", "A.Oaxaca\nB.Puebla\nC.Merida\nD.Mexico city", }; char aS[100] = { 'A', 'B', 'D', 'A', 'D', }; int* arr = randomNumber(); looping(qS, qC, aS, arr); } void ask_hard_questions(void) { int k = 0; while (k < 1) { printf("\t\t--------------------Round 4-------------------\n\n"); printf(">>In this round you need to answer 2 questions correctly to move to Round 5<<\n\n"); char questions[NUM_QUESTIONS][256] = { "What is the capital of France?", "Who was the first president of the United States?" }; char options[NUM_QUESTIONS][4][256] = { { "A) Berlin", "B) Paris", "C) London", "D) Rome" }, { "A) George Washington", "B) John Adams", "C) Thomas Jefferson", "D) James Madison" } }; char answers[NUM_QUESTIONS] = { 'B', 'A'}; // correct answers (A, B, C, or D) char user_answers[NUM_QUESTIONS]; // to store user answers (A, B, C, or D) for (int i = 0; i < 2; i++) { printf("[%d] %s\n", i + 1, questions[i]); for (int j = 0; j < 4; j++) { printf("%s\n", options[i][j]); } printf("Enter your answer (A, B, C, or D): "); char response; scanf(" %c", &response); response = toupper(response); user_answers[i] = response; // check each answer if (user_answers[i] == answers[i]) { score++; correct(score); } else { wrong(score); } } if ( score < 4) { printf("You lost! Better luck next time\n"); break; } else { printf("\t\t--------------------Round 5-------------------\n\n"); printf(">>This is the final round. Good Luck!<<\n\n"); for (int i = 0; i < 2; i++) { printf("[%d] %s\n", i + 1, questions[i]); for (int j = 0; j < 4; j++) { printf("%s\n", options[i][j]); } printf("Enter your answer (A, B, C, or D): "); char response; scanf(" %c", &response); response = toupper(response); user_answers[i] = response; // check each answer if (user_answers[i] == answers[i]) { score++; correct(score); } else { wrong(score); } } } k++; } printf("\nYour final score: %d\n", score); } void hint(int question_num) { char qC[][100] = { "Hint:The Tallest mountain is 8,849m.", "Hint:It's population is 1.412 billion.", "Hint:The longest river measures 6,650km.", "Hint:The Golden States Warriors play there.\n", "Hint:It is one of the oldest and largest cities in the Americas.\n", }; printf("%s\n",qC[question_num]); } void displayThankYouMessage(void) { printf("*******************************\n"); printf("Thank you for playing QuizGame!\n"); printf("*******************************\n\n"); } int main(){ displayInstructions(); printf(">>Press 's' and Enter start the Game<<\n"); char var, var1 = 's'; getchar(); while(var != var1) { var = getchar(); } printf("\n"); ask_questions(); if (score > 2){ ask_hard_questions(); } displayThankYouMessage(); return 0; }