From 029eb426ab2b87b5050b87e58f03f7b99d94a236 Mon Sep 17 00:00:00 2001 From: sengpielto106561 Date: Mon, 26 Oct 2026 17:22:23 +0100 Subject: [PATCH] =?UTF-8?q?implemented=20input.c=20(fails=20unit=20test,?= =?UTF-8?q?=20wegen=20Gro=C3=9F/Kleinschreibung)=20und=20game.c=20tests=20?= =?UTF-8?q?sind=20jetzt=20ok?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Startcode/game.c | 22 +++++++--------------- Startcode/input.c | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/Startcode/game.c b/Startcode/game.c index 6568e5d..93710d3 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -31,17 +31,11 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi //Iterate over words unsigned int counter = 0; while(counter < wordCount){ - //load current word - char currentWord[MAX_WORD_LEN]; - if(strncpy(currentWord, words[counter], MAX_WORD_LEN) == NULL){ - //strncpy failed - continue; - } - size_t len = strlen(currentWord); + size_t len = strlen(words[counter]); //try to place a word - int try = 0; - while(try < 10){ + + for(int try = 0; try < 10; try++){ int x = rand() % MAX_SEARCH_FIELD_LEN; int y = rand() % MAX_SEARCH_FIELD_LEN; @@ -50,7 +44,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi if ((MAX_SEARCH_FIELD_LEN - x) < len){ if((MAX_SEARCH_FIELD_LEN - y) < len){ // cant place word here (too far in the bottom right corner) - try++; continue; } } @@ -62,7 +55,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi for (int i = 0; i < (int)len; i++){ if(salad[x][y+i] != '0'){ cont = true; - try++; break; } } @@ -70,12 +62,12 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi if(cont) continue; //insert currentWord into playing field for (int i = 0; i < (int)len; i++){ - salad[x][y+i] = currentWord[i]; + salad[x][y+i] = words[counter][i]; } placedWords++; + counter++; break; } - //place currentWord horizontally //see if all neccessary spaces are emtry @@ -83,7 +75,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi for (int i = 0; i < (int)len; i++){ if(salad[x+1][y] != '0'){ cont = true; - try++; break; } } @@ -93,9 +84,10 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi //place currentWord for (int i = 0; i < (int)len; i++){ - salad[x+1][y] = currentWord[i]; + salad[x+1][y] = words[counter][i]; } placedWords++; + counter++; } } for (int i = 0; i < MAX_SEARCH_FIELD_LEN; i++){ diff --git a/Startcode/input.c b/Startcode/input.c index b4ea2ec..39eca79 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -1,6 +1,7 @@ #include "input.h" #include #include +#include // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. @@ -8,5 +9,29 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + int count = 0; + char *line = calloc(MAX_LINE_LEN, sizeof(char)); + char delimit[] = ",.;:!? \n"; + + while(fgets(line, MAX_LINE_LEN, file) != NULL && (count < maxWordCount)){ + + char *token = strtok(line, delimit); + + while(token != NULL){ + size_t len = strlen(token); + token[len] = '\0'; + char copy[MAX_LINE_LEN]; + strncpy(copy, token, len); + copy[len] = '\0'; + + strncpy(words[count], copy, len); + words[count][len] = '\0'; + count++; + + token = strtok(NULL, delimit); + } + } + + return count; } \ No newline at end of file