Implementierung der Wortsuche und Eingabeverarbeitung

This commit is contained in:
Markus Schreiber committed 2026-10-25 09:26:34 +01:00
1 parent 85934a0661
commit 634c15ff32
2 files changed
+147 -3

No files matched your search

+106 -2
View File
@@ -11,7 +11,111 @@
* restliche Felder mit zufälligen Buchstaben füllen */
// Creates the word salad by placing words randomly and filling empty spaces
int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount)
int createWordSalad(
char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN],
unsigned int searchFieldLen,
const char words[][MAX_WORD_LEN],
unsigned int wordCount)
{
char*b="Hallo";
unsigned int placedWords = 0;
// 1. Spielfeld leeren
for (unsigned int i = 0; i < searchFieldLen; i++) {
for (unsigned int j = 0; j < searchFieldLen; j++) {
salad[i][j] = EMPTY_CHAR;
}
}
// 2. Alle Wörter durchlaufen
for (unsigned int i = 0; i < wordCount; i++) {
size_t wordLen = strlen(words[i]);
// Leere oder zu lange Wörter überspringen
if (wordLen == 0 || wordLen > searchFieldLen) {
continue;
}
// 3. Platzierungsversuche
for (int attempt = 0;
attempt < MAX_RAND_TRIES_PER_WORD;
attempt++) {
unsigned int row = rand() % searchFieldLen;
unsigned int col = rand() % searchFieldLen;
int direction = rand() % 2;
int canPlace = 1;
// 4. Horizontale Platzierung prüfen
if (direction == 0) {
if (col + wordLen > searchFieldLen) {
continue;
}
for (size_t j = 0; j < wordLen; j++) {
if (salad[row][col + j] != EMPTY_CHAR &&
salad[row][col + j] != words[i][j]) {
canPlace = 0;
break;
}
}
}
// 5. Vertikale Platzierung prüfen
else {
if (row + wordLen > searchFieldLen) {
continue;
}
for (size_t j = 0; j < wordLen; j++) {
if (salad[row + j][col] != EMPTY_CHAR &&
salad[row + j][col] != words[i][j]) {
canPlace = 0;
break;
}
}
}
// Platzierung nicht möglich
if (!canPlace) {
continue;
}
// 6. Wort tatsächlich platzieren
for (size_t j = 0; j < wordLen; j++) {
if (direction == 0) {
salad[row][col + j] = words[i][j];
}
else {
salad[row + j][col] = words[i][j];
}
}
// Erfolgreich platziertes Wort zählen
placedWords++;
// Nächstes Wort bearbeiten
break;
}
}
// 7. Leere Felder mit Zufallsbuchstaben füllen
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;
}
}
}
// 8. Anzahl erfolgreich platzierter Wörter zurückgeben
return (int)placedWords;
}
+41 -1
View File
@@ -6,7 +6,47 @@
// 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 readWords(FILE *file, char words[][MAX_WORD_LEN],
unsigned int maxWordCount)
{
unsigned int wordCount = 0;
unsigned int charIndex = 0;
int c;
if (file == NULL) {
return 0;
}
while ((c = fgetc(file)) != EOF) {
if (c == ',' || c == ';' || c == ' ' ||
c == '\n' || c == '\t' || c == '\r') {
if (charIndex > 0) {
words[wordCount][charIndex] = '\0';
wordCount++;
charIndex = 0;
if (wordCount >= maxWordCount) {
return (int)wordCount;
}
}
}
else {
if (wordCount < maxWordCount &&
charIndex < MAX_WORD_LEN - 1) {
words[wordCount][charIndex] = (char)c;
charIndex++;
}
}
}
if (charIndex > 0 && wordCount < maxWordCount) {
words[wordCount][charIndex] = '\0';
wordCount++;
}
return (int)wordCount;
}