diff --git a/Startcode/game.c b/Startcode/game.c index 54a4fe6..d3ba468 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -3,75 +3,56 @@ #include #include -#define MAX_RAND_TRIES_PER_WORD 1000 +#define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 -// TODO: Spiellogik implementieren: -/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren - * 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 wordCount, char words[][100], unsigned int gridSize) { - // 1. Spielfeld vollständig mit EMPTY_CHAR initialisieren - for (int r = 0; r < MAX_SEARCH_FIELD_LEN; r++) { - for (int c = 0; c < MAX_SEARCH_FIELD_LEN; c++) { - salad[r][c] = EMPTY_CHAR; - } - } + // 1. Komplettes Array sicherheitshalber mit EMPTY_CHAR leeren + memset(salad, EMPTY_CHAR, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN); int placedWordsCount = 0; - // 2. Jedes Wort aus der Wörterliste platzieren + // 2. Wörter platzieren for (unsigned int i = 0; i < wordCount; i++) { - if (words[i] == NULL) continue; - int len = (int)strlen(words[i]); - if (len == 0 || len > MAX_SEARCH_FIELD_LEN) continue; + + // WICHTIG: Wenn das Wort länger ist als das eigentliche Spielfeld (gridSize), direkt überspringen! + if (len == 0 || len > gridSize) continue; - // Versuche bis zu MAX_RAND_TRIES_PER_WORD Mal, eine freie Position zu finden + // Versuche eine freie Position zu finden for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) { - // 0 = Horizontal, 1 = Vertikal - int dir = rand() % 2; + + int is_vert = rand() % 2; + int dr = is_vert; + int dc = !is_vert; - int row, col; - if (dir == 0) { // Horizontal - row = rand() % MAX_SEARCH_FIELD_LEN; - col = rand() % (MAX_SEARCH_FIELD_LEN - len + 1); - } else { // Vertikal - row = rand() % (MAX_SEARCH_FIELD_LEN - len + 1); - col = rand() % MAX_SEARCH_FIELD_LEN; - } + // Zufällige Position INNERHALB der gridSize wählen (nicht mehr 100x100!) + int row = rand() % (gridSize - (is_vert ? len - 1 : 0)); + int col = rand() % (gridSize - (is_vert ? 0 : len - 1)); - // Prüfen, ob das Wort platziert werden kann (Kollisionsprüfung) int canPlace = 1; for (int k = 0; k < len; k++) { - int r = (dir == 1) ? (row + k) : row; - int c = (dir == 0) ? (col + k) : col; - - // Feld muss noch leer sein oder exakt den gleichen Buchstaben enthalten - if (salad[r][c] != EMPTY_CHAR && salad[r][c] != words[i][k]) { + char current = salad[row + k * dr][col + k * dc]; + if (current != EMPTY_CHAR && current != words[i][k]) { canPlace = 0; break; } } - // Wenn die Position gültig ist, Buchstaben in das Spielfeld schreiben if (canPlace) { for (int k = 0; k < len; k++) { - int r = (dir == 1) ? (row + k) : row; - int c = (dir == 0) ? (col + k) : col; - salad[r][c] = words[i][k]; + salad[row + k * dr][col + k * dc] = words[i][k]; } placedWordsCount++; - break; // Nächstes Wort verarbeiten + break; } } } - // 3. Verbleibende leere Felder (EMPTY_CHAR) 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++) { + // 3. Verbleibende leere Felder (nur innerhalb der gridSize!) mit Zufallsbuchstaben auffüllen + for (unsigned int r = 0; r < gridSize; r++) { + for (unsigned int c = 0; c < gridSize; c++) { if (salad[r][c] == EMPTY_CHAR) { salad[r][c] = 'A' + (rand() % 26); } @@ -79,4 +60,4 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi } return placedWordsCount; -} +} \ No newline at end of file diff --git a/Startcode/game.h b/Startcode/game.h index cc66572..41c9c0c 100644 --- a/Startcode/game.h +++ b/Startcode/game.h @@ -5,6 +5,7 @@ #define MAX_SEARCH_FIELD_LEN 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); +// 4. Parameter ist die gridSize! +int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int wordCount, char words[][100], unsigned int gridSize); #endif diff --git a/Startcode/runTests b/Startcode/runTests new file mode 100755 index 0000000..db07437 Binary files /dev/null and b/Startcode/runTests differ