This commit is contained in:
zidanmo99094 committed 2026-10-30 01:08:54 +01:00
1 parent 464c7ff1ad
commit 408e58205a
5 files changed
+17 -13

No files matched your search

+13 -13
View File
@@ -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) {