diff --git a/Startcode/game.c b/Startcode/game.c index 2a0ee6a..0158489 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -6,21 +6,20 @@ #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 -int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int wordCount, char **words, unsigned int seed) +// 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 wordCount, char words[][100], unsigned int seed) { - // Falls die Unit-Tests deterministisch sein müssen, Zufallsgenerator initialisieren: - srand(seed); + // 1. Zufallsgenerator für deterministische Unit-Tests initialisieren + srand(seed); - // 1. Spielfeld initialisieren + // 2. Spielfeld initialisieren (memset füllt das gesamte 2D-Array sehr effizient mit EMPTY_CHAR) memset(salad, EMPTY_CHAR, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN); - - // ... restlicher Code bleibt exakt gleich ... + int placedWordsCount = 0; - // 2. Jedes Wort aus der Wörterliste platzieren + // 3. Jedes Wort aus der Wörterliste platzieren for (unsigned int i = 0; i < wordCount; i++) { - if (!words[i]) continue; - + int len = (int)strlen(words[i]); if (len == 0 || len > MAX_SEARCH_FIELD_LEN) continue; @@ -32,7 +31,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi int dr = is_vert; // Zeilensprung (Delta Row): 1 wenn vertikal, sonst 0 int dc = !is_vert; // Spaltensprung (Delta Col): 1 wenn horizontal, sonst 0 - // Startposition berechnen (verhindert Out-of-Bounds) + // Startposition berechnen (verhindert automatisch Out-of-Bounds Fehler) int row = rand() % (MAX_SEARCH_FIELD_LEN - (is_vert ? len - 1 : 0)); int col = rand() % (MAX_SEARCH_FIELD_LEN - (is_vert ? 0 : len - 1)); @@ -40,24 +39,25 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi int canPlace = 1; for (int k = 0; k < len; k++) { char current = salad[row + k * dr][col + k * dc]; + // Feld muss noch leer sein oder exakt den gleichen Buchstaben enthalten (Überschneidung) if (current != EMPTY_CHAR && current != words[i][k]) { canPlace = 0; break; } } - // Wenn die Position gültig ist, Wort eintragen + // Wenn die Position gültig ist, Buchstaben eintragen if (canPlace) { for (int k = 0; k < len; k++) { salad[row + k * dr][col + k * dc] = words[i][k]; } placedWordsCount++; - break; // Erfolgreich platziert -> Nächstes Wort + break; // Erfolgreich platziert -> Schleife abbrechen, nächstes Wort verarbeiten } } } - // 3. Verbleibende leere Felder mit Zufallsbuchstaben auffüllen + // 4. Verbleibende leere Felder mit Zufallsbuchstaben 'A'-'Z' auffüllen for (int r = 0; r < MAX_SEARCH_FIELD_LEN; r++) { for (int c = 0; c < MAX_SEARCH_FIELD_LEN; c++) { if (salad[r][c] == EMPTY_CHAR) { diff --git a/Startcode/runTests b/Startcode/runTests new file mode 100755 index 0000000..5f7a612 Binary files /dev/null and b/Startcode/runTests differ diff --git a/Startcode/testwords_delims.txt b/Startcode/testwords_delims.txt new file mode 100644 index 0000000..e721fce --- /dev/null +++ b/Startcode/testwords_delims.txt @@ -0,0 +1 @@ +Hund,Katze; Maus diff --git a/Startcode/testwords_empty.txt b/Startcode/testwords_empty.txt new file mode 100644 index 0000000..e69de29 diff --git a/Startcode/testwords_simple.txt b/Startcode/testwords_simple.txt new file mode 100644 index 0000000..37d7524 --- /dev/null +++ b/Startcode/testwords_simple.txt @@ -0,0 +1,3 @@ +Apfel +Banane +Kiwi \ No newline at end of file