diff --git a/Startcode/game.c b/Startcode/game.c index 0265fb9..5ddc540 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -50,8 +50,8 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi for(int j = 0; j < strlen(words[i]); j++){ salad[startpoint_x + j][startpoint_y] = words[i][j]; } - break; wordsplaced += 1; + break; case 1: //X-Negativ startpoint_x = strlen(words[i]) - 1 + rand() % (searchFieldLen - strlen(words[i]) + 1); startpoint_y = rand() % searchFieldLen; @@ -70,8 +70,8 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi for(int j = 0; j < strlen(words[i]); j++){ salad[startpoint_x - j][startpoint_y] = words[i][j]; } - break; wordsplaced += 1; + break; case 2: //Y-Positiv startpoint_x = rand() % searchFieldLen; startpoint_y = rand() % (searchFieldLen - strlen(words[i]) + 1); diff --git a/Startcode/input.c b/Startcode/input.c index 1617eb8..b631499 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -2,19 +2,41 @@ #include #include -// TODO: -// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. - // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + int wordCount = 0; + int charCount = 0; + int c; + while ((c = fgetc(file)) != EOF) { - unsigned int wordCount = 0; - while(fgets(words[wordCount],MAX_WORD_LEN,file) != NULL && wordCount+1 < maxWordCount){ + if (c == ',' || c == ';' || c == '\n' || c == '\r') { + + if (charCount > 0) { + words[wordCount][charCount] = '\0'; + wordCount++; + charCount = 0; + } + + if (wordCount >= maxWordCount) { + break; + } + } + else if (c == ' ' || c == '\t') { + continue; // Leerzeichen und Tabs überspringen + } + else if (charCount < MAX_WORD_LEN - 1) { + words[wordCount][charCount++] = (char)toupper((unsigned char)c); + } + } + + if (charCount > 0 && wordCount < maxWordCount) { + words[wordCount][charCount] = '\0'; wordCount++; } + + fclose(file); + return wordCount; - - } \ No newline at end of file diff --git a/Startcode/main.c b/Startcode/main.c index cc61dc5..5db19ee 100644 --- a/Startcode/main.c +++ b/Startcode/main.c @@ -43,10 +43,12 @@ int main(int argc, char *argv[]) if(placedWords==wordCount){ //start game - startGame(wordSalad, SALAD_SIZE, words, placedWords, SALAD_SIZE); + startGame(wordSalad, SALAD_SIZE, words, placedWords, 1000); } else{ printf("Error! Words coundn't be placed...\n"); + printf("%d\n", placedWords); + printf("%d\n", wordCount); } }