Browse Source

Merge branch 'Pascal' into 'main'

Pascal zweiter Merge

See merge request fdai7760/the-quizstars!9
main
fdai7760 11 months ago
parent
commit
70f69211a7
  1. 48
      src/timequiz.c
  2. 5
      src/timequiz.h
  3. 40
      test/test_timequiz.c

48
src/timequiz.c

@ -4,22 +4,32 @@
#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(char* question, char* answers[], int correctIndex);
void displayQuestion(const char* question,const char* answers[], int correctIndex);
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers, char* answers[]);
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers,const char* answers[]);
int isValidAnswer(int userAnswer);
void printQuizResult(int totalCorrectAnswers, int totalAnsweredQuestions);
void timequiz() {
//Willkommensnachricht
printf("Welcome to our Time Quiz!\n");
printf("You have 60 seconds to answer the questions. Have fun!\n");
char* questions[] = {
const char* questions[] = {
"What is the capital of France?",
"Who developed the theory of relativity?",
"Who was the first President of the United States?",
@ -61,7 +71,7 @@ void timequiz() {
"What is the capital of Australia?"
};
char* answers[][4] = {
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"},
@ -111,7 +121,7 @@ void timequiz() {
for (int i = 0; i < sizeof(questions) / sizeof(questions[0]); i++) {
int j = rand() % 4;
char* temp = answers[i][0];
const char* temp = answers[i][0];
answers[i][0] = answers[i][j];
answers[i][j] = temp;
@ -128,7 +138,7 @@ void timequiz() {
correctAnswers[i] = 4;
}
}
//Variablen für das Spiel
int score = 0;
int totalAnsweredQuestions = 0;
int totalCorrectAnswers = 0;
@ -136,12 +146,13 @@ void timequiz() {
time_t currentTime;
int elapsedTime = 0;
int totalQuestions = sizeof(questions) / sizeof(questions[0]);
int askedQuestions[40];
int askedQuestions[ASKED_QUESTIONS];
memset(askedQuestions, 0, sizeof(askedQuestions));
srand((unsigned int)time(NULL));
while (elapsedTime < 60 && totalAnsweredQuestions < totalQuestions) {
//While-Schleife für Spielausführung
while (elapsedTime < MAX_QUIZ_TIME && totalAnsweredQuestions < totalQuestions) {
int questionIndex = getRandomQuestionIndex(askedQuestions, totalQuestions);
askedQuestions[questionIndex] = 1;
@ -165,10 +176,11 @@ void timequiz() {
elapsedTime = (int)difftime(currentTime, startTime);
}
printf("\nQuiz finished!\n");
printf("Your total score: %d out of %d\n", totalCorrectAnswers, totalAnsweredQuestions);
//Endnachricht
printQuizResult(totalCorrectAnswers, totalAnsweredQuestions);
}
//Zufällige Frage auswählen
int getRandomQuestionIndex(int askedQuestions[], int totalQuestions) {
int questionIndex;
do {
@ -176,15 +188,15 @@ int getRandomQuestionIndex(int askedQuestions[], int totalQuestions) {
} while (askedQuestions[questionIndex] == 1);
return questionIndex;
}
void displayQuestion(char* question, char* answers[], int correctIndex) {
//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]);
}
}
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers, char* answers[]) {
//User-Eingabe Verarbeitung
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers,const char* answers[]) {
if (userAnswer == correctIndex + 1) {
printf("Correct!\n");
(*score)++;
@ -194,7 +206,13 @@ void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalC
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);
}
//Ausgabe Endscore
void printQuizResult(int totalCorrectAnswers, int totalAnsweredQuestions) {
printf("\nQuiz finished!\n");
printf("Your total score: %d out of %d\n", totalCorrectAnswers, totalAnsweredQuestions);
}

5
src/timequiz.h

@ -3,8 +3,9 @@
void timequiz();
int getRandomQuestionIndex(int askedQuestions[], int totalQuestions);
void displayQuestion(char* question, char* answers[], int correctIndex);
void processUserAnswer(int userAnswer, int correctIndex, int* score, int* totalCorrectAnswers, char* answers[]);
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 printQuizResult(int totalCorrectAnswers, int totalAnsweredQuestions);
#endif //ende TIMEQUIZ_H

40
test/test_timequiz.c

@ -8,17 +8,17 @@ void setUp(void) {
void tearDown(void) {
}
//Funktion schaut, ob richtger Index rausgegeben wird
void test_getRandomQuestionIndex(void) {
int askedQuestions[10] = {0};
int totalQuestions = 10;
int index = getRandomQuestionIndex(askedQuestions, totalQuestions);
TEST_ASSERT_TRUE(index >= 0 && index < totalQuestions);
}
//Funktion schaut, ob die Fragen richtig angezeigt werden
void test_displayQuestion(void) {
char* question = "Test Question";
char* answers[] = {"A", "B", "C", "D"};
const char* question = "Test Question";
const char* answers[] = {"A", "B", "C", "D"};
int correctIndex = 0;
printf("\nExpected Output:\n");
printf("Question: %s\n", question);
@ -29,11 +29,11 @@ void test_displayQuestion(void) {
printf("Actual Output:\n");
displayQuestion(question, answers, correctIndex);
}
//Funktion schaut, ob User-Eingabe richtig ist
void test_processUserAnswer_correct(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
const char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 0;
int userAnswer = 1;
@ -41,73 +41,73 @@ void test_processUserAnswer_correct(void) {
TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
}
//Funktion schaut, ob User-Eingabe falsch ist
void test_processUserAnswer_wrong(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
const char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 0;
int userAnswer = 2; // Assuming user selects the second option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(0, score);
TEST_ASSERT_EQUAL_INT(0, totalCorrectAnswers);
}
//Funktion schaut, ob User-Eingabe richtig ist
void test_processUserAnswer_correctAnswer_index3(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"A", "B", "C", "D"};
const char* answers[] = {"A", "B", "C", "D"};
processUserAnswer(4, 3, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
}
//Funktion schaut, ob User-Eingabe richtig ist
void test_processUserAnswer_correctAnswer_index0(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"A", "B", "C", "D"};
const char* answers[] = {"A", "B", "C", "D"};
processUserAnswer(1, 0, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
}
//Funktion schaut, ob User-Eingabe falsch ist
void test_processUserAnswer_wrongAnswer_index3(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
const char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 0;
int userAnswer = 4; // Assuming user selects the fourth option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(0, score);
TEST_ASSERT_EQUAL_INT(0, totalCorrectAnswers);
}
//Funktion schaut, ob User-Eingabe richtig ist
void test_processUserAnswer_correctAnswer_index1(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
const char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 1;
int userAnswer = 2; // Assuming user selects the second option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
}
//Funktion schaut, ob User-Eingabe richtig ist
void test_processUserAnswer_correctAnswer_index2(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
const char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 2;
int userAnswer = 3; // Assuming user selects the third option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);
TEST_ASSERT_EQUAL_INT(1, score);
TEST_ASSERT_EQUAL_INT(1, totalCorrectAnswers);
}
//Funktion schaut, ob User-Eingabe falsch ist
void test_processUserAnswer_wrongAnswer_index1(void) {
int score = 0;
int totalCorrectAnswers = 0;
char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
const char* answers[] = {"Paris", "London", "Berlin", "Madrid"};
int correctIndex = 2;
int userAnswer = 2; // Assuming user selects the second option
processUserAnswer(userAnswer, correctIndex, &score, &totalCorrectAnswers, answers);

Loading…
Cancel
Save