Logikfehler Z102 behoben und Enum für Richtungen hinzugefügt

This commit is contained in:
Angelos committed 2026-10-20 18:56:24 +02:00
1 parent 1d87ea69bc
commit bd830b2044
1 file changed
+16 -11
+16 -11
View File
@@ -6,7 +6,7 @@
#define MAX_RAND_TRIES_PER_WORD 10 #define MAX_RAND_TRIES_PER_WORD 10
#define EMPTY_CHAR 0 #define EMPTY_CHAR 0
//TODO: Spiellogik implementieren: //DONE: Spiellogik implementieren:
/* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren
* restliche Felder mit zufälligen Buchstaben füllen */ * 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)); srand((unsigned int)time(NULL));
typedef enum{
Horizontal,
Vertikal
} Direction;
for (unsigned int row = 0; row < searchFieldLen; row++) for (unsigned int row = 0; row < searchFieldLen; row++)
{ {
for (unsigned int col = 0; col < searchFieldLen; col++) 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 // 2. Alle Wörter nacheinander platzieren
for (unsigned int i = 0; i < wordCount; i++) 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 startRow = 0;
unsigned int startCol = 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; startRow = rand() % searchFieldLen;
startCol = rand() % (searchFieldLen - wordLen + 1); startCol = rand() % (searchFieldLen - wordLen + 1);
@@ -56,12 +61,12 @@ for (unsigned int row = 0; row < searchFieldLen; row++)
startCol = rand() % searchFieldLen; 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; int canPlace = 1;
for (unsigned int j = 0; j < wordLen; j++) 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]) 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 // Wort platzieren
for (unsigned int j = 0; j < wordLen; j++) for (unsigned int j = 0; j < wordLen; j++)
{ {
if (direction == 0) // Horizontal if (direction == Horizontal)
{ {
salad[startRow][startCol + j] = words[i][j]; salad[startRow][startCol + j] = words[i][j];
} }
@@ -94,9 +99,9 @@ for (unsigned int row = 0; row < searchFieldLen; row++)
} }
} }
placedWords++; placedWords++;
placed = 1; //Markiert als gesetzt
printf("Wort '%s' nach %u Versuchen platziert.\n", words[i], attempt + 1); //Ausgabe als Test
} }
placed = 1; // Mark as placed to exit the attempt loop
} }
} }
@@ -115,5 +120,5 @@ for (unsigned int row = 0; row < searchFieldLen; row++)
printf("Placed %u words out of %u\n", placedWords, wordCount); 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
} }