diff --git a/Startcode/game.c b/Startcode/game.c index 5cbfcb1..70d25c6 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -1,68 +1,77 @@ #include "game.h" +#include #include #include -#include -#include -#define MAX_RAND_TRIES_PER_WORD 1000 +#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[][MAX_WORD_LEN], unsigned int gridSize) +// 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 wordCount, char **words) { - // 1. Spielfeld zurücksetzen - memset(salad, EMPTY_CHAR, MAX_SEARCH_FIELD_LEN * MAX_SEARCH_FIELD_LEN); + // 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; + } + } int placedWordsCount = 0; - // 2. Wörter platzieren + // 2. Jedes Wort aus der Wörterliste platzieren for (unsigned int i = 0; i < wordCount; i++) { - size_t len = strlen(words[i]); + if (words[i] == NULL) continue; - // Steuerzeichen am Ende entfernen - while (len > 0 && (words[i][len - 1] == '\n' || words[i][len - 1] == '\r' || words[i][len - 1] == ' ' || words[i][len - 1] == '\t')) { - words[i][--len] = '\0'; - } - - if (len == 0 || len > gridSize) { - continue; - } + int len = (int)strlen(words[i]); + if (len == 0 || len > MAX_SEARCH_FIELD_LEN) continue; + // Versuche bis zu MAX_RAND_TRIES_PER_WORD Mal, eine freie Position zu finden for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD; attempt++) { - Direction dir = (rand() % 2 == 0) ? DIR_HORIZONTAL : DIR_VERTICAL; + // 0 = Horizontal, 1 = Vertikal + int dir = rand() % 2; - int dr = (dir == DIR_VERTICAL) ? 1 : 0; - int dc = (dir == DIR_HORIZONTAL) ? 1 : 0; - - unsigned int maxRow = (dir == DIR_VERTICAL) ? (gridSize - (unsigned int)len + 1) : gridSize; - unsigned int maxCol = (dir == DIR_HORIZONTAL) ? (gridSize - (unsigned int)len + 1) : gridSize; - - unsigned int row = (unsigned int)(rand() % maxRow); - unsigned int col = (unsigned int)(rand() % maxCol); + 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; + } + // Prüfen, ob das Wort platziert werden kann (Kollisionsprüfung) int canPlace = 1; - for (unsigned int k = 0; k < len; k++) { - char current = salad[row + k * dr][col + k * dc]; - char wordChar = (char)toupper((unsigned char)words[i][k]); + for (int k = 0; k < len; k++) { + int r = (dir == 1) ? (row + k) : row; + int c = (dir == 0) ? (col + k) : col; - if (current != EMPTY_CHAR && current != wordChar) { + // Feld muss noch leer sein oder exakt den gleichen Buchstaben enthalten + if (salad[r][c] != EMPTY_CHAR && salad[r][c] != words[i][k]) { canPlace = 0; break; } } + // Wenn die Position gültig ist, Buchstaben in das Spielfeld schreiben if (canPlace) { - for (unsigned int k = 0; k < len; k++) { - salad[row + k * dr][col + k * dc] = (char)toupper((unsigned char)words[i][k]); + 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]; } placedWordsCount++; - break; + break; // Nächstes Wort verarbeiten } } } - // 3. Leere Felder mit Zufallsbuchstaben ('A'-'Z') füllen - for (unsigned int r = 0; r < gridSize; r++) { - for (unsigned int c = 0; c < gridSize; c++) { + // 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++) { if (salad[r][c] == EMPTY_CHAR) { salad[r][c] = 'A' + (rand() % 26); }