diff --git a/game.c b/game.c index 22d9bd5..72f0945 100644 --- a/game.c +++ b/game.c @@ -2,6 +2,7 @@ #include #include #include +#include #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 @@ -22,108 +23,124 @@ // } -// Funktion, die einen zufälligen Großbuchstaben generiert +// Funktion, die einen zufälligen Großbuchstaben (A–Z) erzeugt char randomLetter() { - return 'A' + rand() % 26; + return 'A' + rand() % 26; } -// Hauptfunktion: Erstellt das Wortsalat-Spiel +// Erstellt das Wortsalat-Spielfeld und platziert die Wörter zufällig int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { - // 1. Leeres Spielfeld initialisieren - for (unsigned int i = 0; i < searchFieldLen; i++) - { - for (unsigned int j = 0; j < searchFieldLen; j++) - { - salad[i][j] = EMPTY_CHAR; - } - } + // 1. Spielfeld mit leeren Zeichen initialisieren + for (unsigned int i = 0; i < searchFieldLen; i++) + { + for (unsigned int j = 0; j < searchFieldLen; j++) + { + salad[i][j] = EMPTY_CHAR; + } + } - srand((unsigned int)time(NULL)); + srand((unsigned int)time(NULL)); + int placedWords = 0; - // 2. Jedes Wort zufällig im Spielfeld platzieren - for (unsigned int w = 0; w < wordCount; w++) - { - const char *word = words[w]; - size_t len = strlen(word); - if (len > searchFieldLen) - continue; // Überspringen, falls das Wort zu lang ist + // 2. Jedes Wort zufällig auf dem Spielfeld platzieren + for (unsigned int w = 0; w < wordCount; w++) + { + char word[MAX_WORD_LEN]; + strncpy(word, words[w], MAX_WORD_LEN); + word[MAX_WORD_LEN - 1] = '\0'; - int placed = 0; - for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++) - { - int dir = rand() % 2; // 0 = horizontal, 1 = vertikal - int row = rand() % searchFieldLen; - int col = rand() % searchFieldLen; + // Alle Buchstaben des Wortes in Großbuchstaben umwandeln + for (size_t k = 0; k < strlen(word); k++) + { + word[k] = (char)toupper((unsigned char)word[k]); + } - // Prüfen, ob das Wort an die Position passt - if (dir == 0 && col + len <= searchFieldLen) - { - // Prüfen, ob keine Überschneidungen mit anderen Buchstaben auftreten - int ok = 1; - for (size_t i = 0; i < len; i++) + size_t len = strlen(word); + if (len > searchFieldLen) + continue; // Wort ist zu lang → überspringen + + int placed = 0; + for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++) + { + int dir = rand() % 2; // 0 = horizontal, 1 = vertikal + int row = rand() % searchFieldLen; + int col = rand() % searchFieldLen; + + if (dir == 0 && col + len <= searchFieldLen) { - if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != word[i]) - { - ok = 0; - break; - } + int ok = 1; + // Prüfen, ob der Platz frei ist oder die Buchstaben passen + for (size_t i = 0; i < len; i++) + { + if (salad[row][col + i] != EMPTY_CHAR && salad[row][col + i] != word[i]) + { + ok = 0; + break; + } + } + if (ok) + { + // Wort horizontal einfügen + for (size_t i = 0; i < len; i++) + salad[row][col + i] = word[i]; + placed = 1; + } } - if (ok) + else if (dir == 1 && row + len <= searchFieldLen) { - for (size_t i = 0; i < len; i++) - salad[row][col + i] = word[i]; - placed = 1; + int ok = 1; + // Prüfen, ob der Platz frei ist oder die Buchstaben passen + for (size_t i = 0; i < len; i++) + { + if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != word[i]) + { + ok = 0; + break; + } + } + if (ok) + { + // Wort vertikal einfügen + for (size_t i = 0; i < len; i++) + salad[row + i][col] = word[i]; + placed = 1; + } } - } - else if (dir == 1 && row + len <= searchFieldLen) - { - int ok = 1; - for (size_t i = 0; i < len; i++) - { - if (salad[row + i][col] != EMPTY_CHAR && salad[row + i][col] != word[i]) - { - ok = 0; - break; - } - } - if (ok) - { - for (size_t i = 0; i < len; i++) - salad[row + i][col] = word[i]; - placed = 1; - } - } - } - } + } - // 3. Leere Felder mit zufälligen Buchstaben 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] = randomLetter(); - } - } + if (placed) + placedWords++; // Anzahl der erfolgreich platzierten Wörter erhöhen + } - return 0; // 0 = erfolgreich + // 3. Alle leeren Felder mit zufälligen Buchstaben 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] = randomLetter(); + } + } + + // Rückgabewert: Anzahl der platzierten Wörter + return placedWords; } -// Funktion zur Anzeige des Spielfelds in der Konsole +// Gibt das Spielfeld in der Konsole aus 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"); - } + for (unsigned int i = 0; i < searchFieldLen; i++) + { + for (unsigned int j = 0; j < searchFieldLen; j++) + { + printf("%c ", salad[i][j]); + } + printf("\n"); + } } \ No newline at end of file diff --git a/game.o b/game.o index ed7575a..3a8608f 100644 Binary files a/game.o and b/game.o differ diff --git a/input.c b/input.c index 54a6a7a..3bec224 100644 --- a/input.c +++ b/input.c @@ -23,35 +23,41 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) } unsigned int count = 0; - char line[MAX_WORD_LEN + 5]; // etwas Puffer, um Überlauf zu vermeiden + char buffer[512]; - // Zeilen einzeln lesen, bis EOF erreicht oder das Limit erreicht ist - while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount) + // Ganze Datei zeilenweise lesen + while (fgets(buffer, sizeof(buffer), file) != NULL && count < maxWordCount) { - // Zeilenumbruch '\n' oder '\r\n' entfernen - line[strcspn(line, "\r\n")] = '\0'; + // Entfernt Zeilenumbruch '\n' oder '\r\n' + buffer[strcspn(buffer, "\r\n")] = '\0'; - // Leere Zeilen überspringen - if (strlen(line) == 0) - continue; - - // Leerzeichen am Anfang und Ende entfernen - char *start = line; - while (isspace((unsigned char)*start)) - start++; - - char *end = start + strlen(start) - 1; - while (end > start && isspace((unsigned char)*end)) + // Zerlegt die Zeile in Wörter mit den Trennzeichen: Leerzeichen, Komma, Semikolon + char *token = strtok(buffer, " ,;"); + while (token != NULL && count < maxWordCount) { - *end = '\0'; - end--; + // Leerzeichen am Anfang und Ende entfernen + while (isspace((unsigned char)*token)) + token++; + char *end = token + strlen(token) - 1; + while (end > token && isspace((unsigned char)*end)) + { + *end = '\0'; + end--; + } + + // In Großbuchstaben umwandeln + for (unsigned int i = 0; i < strlen(token); i++) + { + token[i] = (char)toupper((unsigned char)token[i]); + } + + // In das Array 'words' kopieren + strncpy(words[count], token, MAX_WORD_LEN - 1); + words[count][MAX_WORD_LEN - 1] = '\0'; + + count++; + token = strtok(NULL, " ,;"); } - - // Sichere Kopie in das Array 'words' - strncpy(words[count], start, MAX_WORD_LEN - 1); - words[count][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung sicherstellen - - count++; } return count; diff --git a/input.o b/input.o index 34d5019..a6f83a7 100644 Binary files a/input.o and b/input.o differ diff --git a/runTests.exe b/runTests.exe index 8a4a3b0..938ebcb 100644 Binary files a/runTests.exe and b/runTests.exe differ