diff --git a/Start_Linux/input.c b/Start_Linux/input.c index ed77805..5e752b3 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. +string readWord(FILE *file) +{ + char word[MAX_WORD_LEN]; // Puffer für Wort + int index = 0; // Position im Puffer + int c; // Variablefür jedes gelesene Zeichen + + // Whitespace überspringen + while(isspace(c = fgetc(file))); //isspace checkt ob gelesenes Zeichen whitespace ist, wenn ja 1 wenn nein 0 + + // Buchstaben einlesen bis nächstes whitespace/EOF + while(c != EOF && !isspace(c) && index < MAX_WORD_LEN - 1) // -1 wegen Nullterminator + { + word[index++] = (char)c; + c = fgetc(file); + } + word[index] = '\0'; // Nullterminator (= Ende String) + + return strdup(word); // Rückgabe string, dynamisch allokiert da Zeiger auf lokalen Puffer zurückgegeben +} // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + unsigned int count = 0; + while(count < maxWordCount && !feof(file)) + { + char *word = readWord(file); + if(word != NULL) + { + strncpy(words[count], word, MAX_WORD_LEN - 1); // Wort in Array kopieren + words[count][MAX_WORD_LEN - 1] = '\0'; // Sicherstellen, dass String nullterminiert ist + free(word); // Dynamisch allokierten Speicher freigeben + count++; + } + } + + return count; // Anzahl der gelesenen Wörter zurückgeben } \ No newline at end of file diff --git a/Start_Linux/input.o b/Start_Linux/input.o new file mode 100644 index 0000000..8d2710d Binary files /dev/null and b/Start_Linux/input.o differ diff --git a/Start_Linux/main.c b/Start_Linux/main.c index 03da755..ae5253f 100644 --- a/Start_Linux/main.c +++ b/Start_Linux/main.c @@ -31,16 +31,25 @@ int main(int argc, char *argv[]) // Read words from file and store in 'words' array wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS); - fclose(file); + fclose(file); // TODO: + // Check if all words were successfully placed + // Start the game if successful + // error message if some words couldn't be placed + // Create the word salad by placing words into grid 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 graphical game with the created word salad and the list of words + startGame(wordSalad, SALAD_SIZE, words, wordCount, 800); + } + else + { + fprintf(stderr, "Could not place all words in the word salad. Placed %u out of %u words.\n", placedWords, wordCount); + exitCode = EXIT_FAILURE; + } } else { diff --git a/Start_Linux/main.o b/Start_Linux/main.o new file mode 100644 index 0000000..3cf8072 Binary files /dev/null and b/Start_Linux/main.o differ diff --git a/Start_Linux/wordsalad_initial b/Start_Linux/wordsalad_initial new file mode 100755 index 0000000..f4f709e Binary files /dev/null and b/Start_Linux/wordsalad_initial differ