From bd830b2044e2219052e35ca686e8ad9b283c79ad Mon Sep 17 00:00:00 2001 From: Angelos Date: Tue, 20 Oct 2026 18:50:29 +0200 Subject: [PATCH] =?UTF-8?q?Logikfehler=20Z102=20behoben=20und=20Enum=20f?= =?UTF-8?q?=C3=BCr=20Richtungen=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Startcode/game.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/Startcode/game.c b/Startcode/game.c index 4075609..15d4ace 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -6,7 +6,7 @@ #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 -//TODO: Spiellogik implementieren: +//DONE: Spiellogik implementieren: /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ @@ -16,16 +16,21 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi srand((unsigned int)time(NULL)); +typedef enum{ + Horizontal, + Vertikal +} Direction; + for (unsigned int row = 0; row < searchFieldLen; row++) { for (unsigned int col = 0; col < searchFieldLen; col++) { - salad[row][col] = EMPTY_CHAR; // Initialize all cells to empty + salad[row][col] = EMPTY_CHAR; // Alle Felder leer setzen } } - unsigned int placedWords = 0; +unsigned int placedWords = 0; // 2. Alle Wörter nacheinander platzieren for (unsigned int i = 0; i < wordCount; i++) @@ -43,9 +48,9 @@ for (unsigned int row = 0; row < searchFieldLen; row++) unsigned int startRow = 0; unsigned int startCol = 0; - unsigned int direction = rand() % 2; + Direction direction = (Direction)(rand()%2); - if (direction == 0) // Horizontal + if (direction == Horizontal) { startRow = rand() % searchFieldLen; startCol = rand() % (searchFieldLen - wordLen + 1); @@ -56,12 +61,12 @@ for (unsigned int row = 0; row < searchFieldLen; row++) startCol = rand() % searchFieldLen; } - // 3. Überprüfen, ob das Wort an der gewählten Position platziert werden kann + // Überprüfen ob das Wort an der gewählten Position platziert werden kann int canPlace = 1; for (unsigned int j = 0; j < wordLen; j++) { - if (direction == 0) // Horizontal + if (direction == Horizontal) { if (salad[startRow][startCol + j] != EMPTY_CHAR && salad[startRow][startCol + j] != words[i][j]) { @@ -84,7 +89,7 @@ for (unsigned int row = 0; row < searchFieldLen; row++) // Wort platzieren for (unsigned int j = 0; j < wordLen; j++) { - if (direction == 0) // Horizontal + if (direction == Horizontal) { salad[startRow][startCol + j] = words[i][j]; } @@ -94,9 +99,9 @@ for (unsigned int row = 0; row < searchFieldLen; row++) } } placedWords++; - - } - placed = 1; // Mark as placed to exit the attempt loop + placed = 1; //Markiert als gesetzt + printf("Wort '%s' nach %u Versuchen platziert.\n", words[i], attempt + 1); //Ausgabe als Test + } } } @@ -115,5 +120,5 @@ for (unsigned int row = 0; row < searchFieldLen; row++) printf("Placed %u words out of %u\n", placedWords, wordCount); - return placedWords; // Return the number of successfully placed words + return placedWords; // Gibt Anzahl gesetzter Wörter zurück }