You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
8.0 KiB

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "timequiz.h"
#define NUM_ANSWERS 4
#define ASKED_QUESTIONS 40
#define MAX_QUIZ_TIME 60
//Funktionen des Codes
void timequiz();
int getRandomQuestionIndex(int askedQuestions[], int totalQuestions);
void displayQuestion(const char* question,const char* answers[], int correctIndex);
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers,const char* answers[]);
int isValidAnswer(int userAnswer);
void timequiz() {
//Willkommensnachricht
printf("Welcome to our Time Quiz!\n");
printf("You have 60 seconds to answer the questions. Have fun!\n");
const char* questions[] = {
"What is the capital of France?",
"Who developed the theory of relativity?",
"Who was the first President of the United States?",
"What is the chemical symbol for water?",
"Who wrote 'To Kill a Mockingbird'?",
"What is the largest planet in our solar system?",
"Who painted the Mona Lisa?",
"What is the tallest mountain in the world?",
"Which country is known as the Land of the Rising Sun?",
"Who was the first person to walk on the moon?",
"What year did World War I begin?",
"What is the currency of Japan?",
"Who is known as the 'Father of Computers'?",
"What is the capital of Canada?",
"Who discovered penicillin?",
"What is the chemical formula for table salt?",
"Which is the largest ocean on Earth?",
"Who is the author of '1984'?",
"What is the main ingredient in guacamole?",
"Who was the Greek god of the sea?",
"What is the square root of 144?",
"Who wrote 'Romeo and Juliet'?",
"What is the process by which plants make their food?",
"What is the boiling point of water in Celsius?",
"Who painted 'Starry Night'?",
"What is the chemical symbol for gold?",
"Which planet is known as the Red Planet?",
"Who invented the telephone?",
"What is the capital of Brazil?",
"What is the hardest natural substance on Earth?",
"Who was the first woman to win a Nobel Prize?",
"Who composed 'The Four Seasons'?",
"What is the largest mammal in the world?",
"What is the study of earthquakes called?",
"Who wrote 'Hamlet'?",
"What is the smallest prime number?",
"Who is known as the 'Queen of Pop'?",
"What is the chemical symbol for oxygen?",
"What is the capital of Australia?"
};
const char* answers[][NUM_ANSWERS] = {
{"Paris", "London", "Berlin", "Madrid"},
{"Albert Einstein", "Isaac Newton", "Galileo Galilei", "Stephen Hawking"},
{"George Washington", "Thomas Jefferson", "John Adams", "James Madison"},
{"H2O", "CO2", "NaCl", "C6H12O6"},
{"Harper Lee", "J.K. Rowling", "Ernest Hemingway", "George Orwell"},
{"Jupiter", "Saturn", "Mars", "Neptune"},
{"Leonardo da Vinci", "Michelangelo", "Pablo Picasso", "Vincent van Gogh"},
{"Mount Everest", "K2", "Kangchenjunga", "Lhotse"},
{"Japan", "China", "South Korea", "India"},
{"Neil Armstrong", "Buzz Aldrin", "Yuri Gagarin", "Alan Shepard"},
{"1914", "1918", "1923", "1939"},
{"Yen", "Euro", "Dollar", "Pound"},
{"Charles Babbage", "Alan Turing", "Ada Lovelace", "Bill Gates"},
{"Ottawa", "Toronto", "Vancouver", "Montreal"},
{"Alexander Fleming", "Louis Pasteur", "Marie Curie", "Albert Einstein"},
{"NaCl", "H2O", "CO2", "HCl"},
{"Pacific Ocean", "Atlantic Ocean", "Indian Ocean", "Arctic Ocean"},
{"George Orwell", "F. Scott Fitzgerald", "Aldous Huxley", "J.D. Salinger"},
{"Avocado", "Tomato", "Onion", "Lime"},
{"Poseidon", "Zeus", "Hades", "Apollo"},
{"12", "10", "16", "14"},
{"William Shakespeare", "Jane Austen", "Emily Bronte", "Leo Tolstoy"},
{"Photosynthesis", "Respiration", "Fermentation", "Transpiration"},
{"100", "0", "212", "50"},
{"Vincent van Gogh", "Pablo Picasso", "Leonardo da Vinci", "Claude Monet"},
{"Au", "Ag", "Hg", "Fe"},
{"Mars", "Jupiter", "Venus", "Mercury"},
{"Alexander Graham Bell", "Thomas Edison", "Nikola Tesla", "Galileo Galilei"},
{"Brasília", "Rio de Janeiro", "São Paulo", "Buenos Aires"},
{"Diamond", "Quartz", "Graphite", "Topaz"},
{"Marie Curie", "Rosalind Franklin", "Mother Teresa", "Malala Yousafzai"},
{"299,792,458 meters per second", "300,000,000 meters per second", "200,000,000 meters per second", "150,000,000 meters per second"},
{"Antonio Vivaldi", "Wolfgang Amadeus Mozart", "Ludwig van Beethoven", "Johann Sebastian Bach"},
{"Blue whale", "African elephant", "Giraffe", "Polar bear"},
{"Seismology", "Meteorology", "Geology", "Astronomy"},
{"William Shakespeare", "Charles Dickens", "Jane Austen", "Mark Twain"},
{"2", "3", "5", "7"},
{"Madonna", "Beyoncé", "Lady Gaga", "Taylor Swift"},
{"O", "Co", "O2", "O3"},
{"Canberra", "Sydney", "Melbourne", "Perth"}
};
int correctAnswers[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++) {
int j = rand() % 4;
const char* temp = answers[i][0];
answers[i][0] = answers[i][j];
answers[i][j] = temp;
if (j == 0) {
correctAnswers[i] = 1;
}
else if (j == 1) {
correctAnswers[i] = 2;
}
else if (j == 2) {
correctAnswers[i] = 3;
}
else {
correctAnswers[i] = 4;
}
}
//Variablen für das Spiel
int score = 0;
int totalAnsweredQuestions = 0;
int totalCorrectAnswers = 0;
time_t startTime = time(NULL);
time_t currentTime;
int elapsedTime = 0;
int totalQuestions = sizeof(questions) / sizeof(questions[0]);
int askedQuestions[ASKED_QUESTIONS];
memset(askedQuestions, 0, sizeof(askedQuestions));
srand((unsigned int)time(NULL));
//While-Schleife für Spielausführung
while (elapsedTime < MAX_QUIZ_TIME && totalAnsweredQuestions < totalQuestions) {
int questionIndex = getRandomQuestionIndex(askedQuestions, totalQuestions);
askedQuestions[questionIndex] = 1;
int correctIndex = correctAnswers[questionIndex] - 1;
displayQuestion(questions[questionIndex], answers[questionIndex], correctIndex);
int userAnswer;
printf("Answer (1-4): ");
scanf_s("%d", &userAnswer);
while (!isValidAnswer(userAnswer)) {
printf("Invalid input. Please enter a number between 1 and 4: ");
scanf_s("%d", &userAnswer);
}
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers[questionIndex]);
totalAnsweredQuestions++;
currentTime = time(NULL);
elapsedTime = (int)difftime(currentTime, startTime);
}
//Endnachricht
printf("\nQuiz finished!\n");
printf("Your total score: %d out of %d\n", totalCorrectAnswers, totalAnsweredQuestions);
}
//Zufällige Frage auswählen
int getRandomQuestionIndex(int askedQuestions[], int totalQuestions) {
int questionIndex;
do {
questionIndex = rand() % totalQuestions;
} while (askedQuestions[questionIndex] == 1);
return questionIndex;
}
//Frage anzeigen
void displayQuestion(const char* question,const char* answers[], int correctIndex) {
printf("\nQuestion: %s\n", question);
for (int i = 0; i < 4; i++) {
printf("%d. %s\n", i + 1, answers[i]);
}
}
//User-Eingabe Verarbeitung
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers,const char* answers[]) {
if (userAnswer == correctIndex + 1) {
printf("Correct!\n");
(*score)++;
(*totalCorrectAnswers)++;
}
else {
printf("Wrong! The correct answer is: %d. %s\n", correctIndex + 1, answers[correctIndex]);
}
}
//Prüfung auf gültige Antwort
int isValidAnswer(int userAnswer) {
return (userAnswer >= 1 && userAnswer <= 4);
}