From 9e3c8e697d5c6f0c027fe5a1f6188665309459ee Mon Sep 17 00:00:00 2001 From: manusmac Date: Tue, 28 Oct 2025 13:53:10 +0100 Subject: [PATCH] input und main in Start_Windows und Start_Linux geupdated --- Start_Linux/input.c | 37 +++++++++++++++++++++++++++++++++++-- Start_Linux/main.c | 17 ++++++++++++++--- Start_Windows/input.c | 37 +++++++++++++++++++++++++++++++++++-- Start_Windows/main.c | 16 +++++++++++++--- 4 files changed, 97 insertions(+), 10 deletions(-) diff --git a/Start_Linux/input.c b/Start_Linux/input.c index ed77805..6a94ca2 100644 --- a/Start_Linux/input.c +++ b/Start_Linux/input.c @@ -4,9 +4,42 @@ // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. - +/** + * @param file Zeiger auf die geöffnete Datei + * @param words 2D-Array zum Speichern der Wörter + * @param maxWordCount Maximale Anzahl von Wörtern, die eingelesen werden sollen + * @return Anzahl der eingelesenen Wörter + */ // Read words from file and store in 'words' array +// ERLEDIGT int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { - + if (file == NULL || words == NULL || maxWordCount == 0) { + return 0; + } + + int wordCount = 0; + char line[MAX_LINE_LEN]; + + // Zeilen einlesen + while (wordCount < maxWordCount && fgets(line, MAX_LINE_LEN, file) != NULL) { + // Zeile in Wörter aufteilen (Trennzeichen: Leerzeichen, Komma, Semikolon, Newline) + char *token = strtok(line, " ,;\t\n\r"); + + while (token != NULL && wordCount < maxWordCount) { + // Prüfen ob Token nicht leer ist + if (strlen(token) > 0 && strlen(token) < MAX_WORD_LEN) { + // Wort kopieren und in Großbuchstaben umwandeln + strcpy(words[wordCount], token); + for (int i = 0; words[wordCount][i] != '\0'; i++) { + words[wordCount][i] = toupper((unsigned char)words[wordCount][i]); + } + wordCount++; + } + // Nächstes Wort + token = strtok(NULL, " ,;\t\n\r"); + } + } + + return wordCount; } \ No newline at end of file diff --git a/Start_Linux/main.c b/Start_Linux/main.c index 03da755..87e7f01 100644 --- a/Start_Linux/main.c +++ b/Start_Linux/main.c @@ -36,10 +36,21 @@ int main(int argc, char *argv[]) // Create the word salad by placing words into grid placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); - // TODO: + // TODO: + // Check if all words were successfully placed - // Start the game if successful - // error message if some words couldn't be placed + if(placedWords == wordCount) + { + // Start the game if successful + startGame(wordSalad,SALAD_SIZE,words,wordCount,SALAD_SIZE); + } + else + { + // Error message if some words couldn't be placed + fprintf(stderr, "Error: Only %u out of %u words could be placed in the word salad.\n", + placedWords, wordCount); + exitCode = EXIT_FAILURE; + } } else diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..6a94ca2 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -4,9 +4,42 @@ // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. - +/** + * @param file Zeiger auf die geöffnete Datei + * @param words 2D-Array zum Speichern der Wörter + * @param maxWordCount Maximale Anzahl von Wörtern, die eingelesen werden sollen + * @return Anzahl der eingelesenen Wörter + */ // Read words from file and store in 'words' array +// ERLEDIGT int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { - + if (file == NULL || words == NULL || maxWordCount == 0) { + return 0; + } + + int wordCount = 0; + char line[MAX_LINE_LEN]; + + // Zeilen einlesen + while (wordCount < maxWordCount && fgets(line, MAX_LINE_LEN, file) != NULL) { + // Zeile in Wörter aufteilen (Trennzeichen: Leerzeichen, Komma, Semikolon, Newline) + char *token = strtok(line, " ,;\t\n\r"); + + while (token != NULL && wordCount < maxWordCount) { + // Prüfen ob Token nicht leer ist + if (strlen(token) > 0 && strlen(token) < MAX_WORD_LEN) { + // Wort kopieren und in Großbuchstaben umwandeln + strcpy(words[wordCount], token); + for (int i = 0; words[wordCount][i] != '\0'; i++) { + words[wordCount][i] = toupper((unsigned char)words[wordCount][i]); + } + wordCount++; + } + // Nächstes Wort + token = strtok(NULL, " ,;\t\n\r"); + } + } + + return wordCount; } \ No newline at end of file diff --git a/Start_Windows/main.c b/Start_Windows/main.c index 03da755..0175942 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -37,10 +37,20 @@ int main(int argc, char *argv[]) placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); // TODO: + // Check if all words were successfully placed - // Start the game if successful - // error message if some words couldn't be placed - + if(placedWords == wordCount) + { + // Start the game if successful + startGame(wordSalad,SALAD_SIZE,words,wordCount,SALAD_SIZE); + } + else + { + // Error message if some words couldn't be placed + fprintf(stderr, "Error: Only %u out of %u words could be placed in the word salad.\n", + placedWords, wordCount); + exitCode = EXIT_FAILURE; + } } else {