|
|
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "quizproject.h"
int* randomNumber() { srand(time(NULL)); int k = 0, p; static int arr[8]; while(k<8){ int num =rand()%8; 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 vorName[100] ={0}; char nachName[100] ={0}; printf("\t\t Please enter your First Name \n"); scanf("%s", vorName); printf("\t\t Please enter your Surname \n"); scanf("%s", nachName); displayGameInstructions(vorName, nachName); }
}
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 vorName[], char nachName[]) { printf("\t\t------------------------------------------\n\n"); printf("\t\t------------------------------------------\n\n"); printf("\t\t Hello %s %s! \n\n", vorName, nachName); 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", "A.San Fransisco\nB.San Diego", "C.Merida\nD.Mexico city", "C.Russia\nD.USA\n", "A.Alaska\nB.Washington", "C.Toronto\nD.Ottawa", }; printf("%s\n",qC[question_num]); }
void fifty_fifty1(int question_num) { 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" }, { "A) Los Angeles Lakers", "B) Chicago Bulls", "C) Golden State Warriors", "D) Boston Celtics" }, { "A) He", "B) H", "C) O", "D) Ne" }, { "A) Parasite", "B) Joker", "C) Once Upon a Time in Hollywood", "D) 1917" } }; char correct_answer = answers[question_num]; int num_incorrect = 0; int incorrect_indices[2]; for (int i = 0; i < 4; i++) { if (options[question_num][i][0] != correct_answer) { incorrect_indices[num_incorrect] = i; num_incorrect++; } if (num_incorrect == 2) { break; } } for (int i = 0; i < 2; i++) { options[question_num][incorrect_indices[i]][0] = '\0'; } // display remaining options
for (int i = 0; i < 4; i++) { if (options[question_num][i][0] != '\0') { printf("%s\n", options[question_num][i]); } } }
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 = 2; 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 = 2, end = 4; 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 = 4, end = 7; 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", "What is the name of the largest country in the world?\n", "What U.S. state is home to no documented poisonous snakes?\n", "What is the capital of Canada?\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", "A.India\nB.China\nC.Russia\nD.USA", "A.Alaska\nB.Washington\nC.Ohio\nD.California", "A.Vancouver\nB.Montreal\nC.Toronto\nD.Ottawa",
};
char aS[100] = { 'A', 'B', 'D', 'A', 'D', 'C', 'A', 'D', };
int* arr = randomNumber(); looping(qS, qC, aS, arr);
}
void ask_hard_questions(void) {
int k = 0; while (k < 1) { int *lessFive = randomNumber(); int arr[5]; int p = 0; for(int i = 0; i < 8; i++){ if(lessFive[i] < 2){ arr[p] = lessFive[i]; p++; } } 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] = { "Which author is famed for her detective novels about Hercule Poirot and Miss Marple?", "Atticus Finch is one of the main characters in which novel from 1960?" }; char options[NUM_QUESTIONS][4][256] = { { "A) Arthur Conan Doyle", "B) Agatha Christie", "C) Charles Dickens", "D) Stephen King" }, { "A) To Kill a Mockingbird", "B) Pride and Prejudice", "C) The Great Gatsby", "D) Jane Eyre" } };
char user_answers[NUM_QUESTIONS]; int lifelines[NUM_QUESTIONS] = { 1, 1}; int randNum; for (int i = 0; i < 2; i++) { randNum = arr[i]; printf("[%d] %s\n", i + 1, questions[randNum]); for (int j = 0; j < 4; j++) { printf("%s\n", options[randNum][j]); } printf("Enter your answer (A, B, C, or D): "); char response; scanf(" %c", &response); response = toupper(response); if (response == 'F' && lifelines[i] == 1) { fifty_fifty1(randNum); printf("Enter your answer (A, B, C, or D): "); scanf(" %c", &user_answers[randNum]); user_answers[randNum] = toupper(user_answers[randNum]); lifelines[i] = 0; } else { user_answers[randNum] = response; } if (user_answers[randNum] == answers[randNum]) { 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++) {
randNum = arr[i]; printf("[%d] %s\n", i + 1, questions[randNum]); for (int j = 0; j < 4; j++) { printf("%s\n", options[randNum][j]); } printf("Enter your answer (A, B, C, or D): "); char response; scanf(" %c", &response); response = toupper(response); if (response == 'F' && lifelines[i] == 1) { fifty_fifty1(randNum); printf("Enter your answer (A, B, C, or D): "); scanf(" %c", &user_answers[randNum]); user_answers[randNum] = toupper(user_answers[randNum]); lifelines[i] = 0; } else { user_answers[randNum] = response; } // check each answer
if (user_answers[randNum] == answers[randNum]) { 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", "Hint:The area of the largest country is 17.1 million km2.\n", "Hint:It is the coldest state in the US.", "Hint:It is the 4th largest city in Canada.",
}; 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; }
|