diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..fcece2a 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -14,10 +14,100 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { + srand((unsigned int)time(NULL)); // Seed für Zufallsgenerator + + // Initialisiere das Spielfeld mit EMPTY_CHAR + for (unsigned int i = 0; i < searchFieldLen; ++i) + { + for (unsigned int j = 0; j < searchFieldLen; ++j) + { + salad[i][j] = EMPTY_CHAR; + } + } + + unsigned int placedWords = 0; + + for (unsigned int w = 0; w < wordCount; ++w) + { + int placed = 0; + for (int tries = 0; tries < MAX_RAND_TRIES_PER_WORD && !placed; ++tries) + { + int direction = rand() % 2; // 1 = horizontal, 0 = vertical + int row = rand() % searchFieldLen; + int col = rand() % searchFieldLen; + int len = (int)strlen(words[w]); + + if (direction == 1 && col + len <= searchFieldLen) // horizontal + { + int canPlace = 1; + for (int i = 0; i < len; ++i) + { + + if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != words[w][i]) + { + canPlace = 0; + break; + } + } + if (canPlace) + { + for (int i = 0; i < len; ++i) + salad[row][col + i] = words[w][i]; + placed = 1; + } + } + else if (direction == 0 && row + len <= searchFieldLen) // vertical + { + int canPlace = 1; + for (int i = 0; i < len; ++i) + { + if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != words[w][i]) + { + canPlace = 0; + + break; + } + } + if (canPlace) + { + for (int i = 0; i < len; ++i) + salad[row + i][col] = words[w][i]; + placed = 1; + } + } + } + + if (placed) + placedWords++; + } + + // Fülle leere Felder mit zufälligen Buchstaben + for (unsigned int i = 0; i < searchFieldLen; ++i) + + { + for (unsigned int j = 0; j < searchFieldLen; ++j) + { + if (salad[i][j] == EMPTY_CHAR) + { + salad[i][j] = 'A' + rand() % 26; + } + } + } + + return placedWords; + } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { + for (unsigned int i = 0; i < searchFieldLen; ++i) + { + for (unsigned int j = 0; j < searchFieldLen; ++j) + { + printf("%c ", salad[i][j]); + } + printf("\n"); + } } diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..a487317 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -2,11 +2,30 @@ #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 +// Liest Wörter aus Datei und speichert sie sicher im Array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + char buffer[MAX_WORD_LEN]; + unsigned int count_word = 0; + while (count_word < maxWordCount && fgets(buffer, sizeof(buffer), file) != NULL) + { + // Zeilenumbruch manuell entfernen + for (int i = 0; i < MAX_WORD_LEN; i++) + { + if (buffer[i] == '\n' || buffer[i] == '\r') + { + buffer[i] = '\0'; + break; + } + } + + // Sicheres Kopieren in das Zielarray + strncpy(words[count_word], buffer, MAX_WORD_LEN - 1); + words[count_word][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung sicherstellen + + count_word++; + } + + return count_word; } \ No newline at end of file diff --git a/Start_Windows/wordsalad_initial.exe b/Start_Windows/wordsalad_initial.exe new file mode 100644 index 0000000..84577f6 Binary files /dev/null and b/Start_Windows/wordsalad_initial.exe differ