From 9b704552cc35d03063cea460528e049e55106606 Mon Sep 17 00:00:00 2001 From: Max Perschke Date: Mon, 19 Oct 2026 22:32:49 +0200 Subject: [PATCH] Spiellogik implementiert und mit gametest.c getestet --- Startcode/game.c | 102 +++++++++++++++++++++++++++++++++++++++++++ Startcode/gametest.c | 27 ++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 Startcode/gametest.c diff --git a/Startcode/game.c b/Startcode/game.c index 803301f..4075609 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -14,4 +14,106 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { +srand((unsigned int)time(NULL)); + +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 + } +} + + unsigned int placedWords = 0; + + // 2. Alle Wörter nacheinander platzieren + for (unsigned int i = 0; i < wordCount; i++) + { + unsigned int wordLen = strlen(words[i]); + int placed = 0; + + // Wort darf nicht länger als das Spielfeld sein + if (wordLen == 0 || wordLen > searchFieldLen) + { + continue; + } + for (unsigned int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++) + { + + unsigned int startRow = 0; + unsigned int startCol = 0; + unsigned int direction = rand() % 2; + + if (direction == 0) // Horizontal + { + startRow = rand() % searchFieldLen; + startCol = rand() % (searchFieldLen - wordLen + 1); + } + else // Vertikal + { + startRow = rand() % (searchFieldLen - wordLen + 1); + startCol = rand() % searchFieldLen; + } + + // 3. Ü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 (salad[startRow][startCol + j] != EMPTY_CHAR && salad[startRow][startCol + j] != words[i][j]) + { + canPlace = 0; + break; + } + } + else // Vertikal + { + if (salad[startRow + j][startCol] != EMPTY_CHAR && salad[startRow + j][startCol] != words[i][j]) + { + canPlace = 0; + break; + } + } + } + + if (canPlace) + { + // Wort platzieren + for (unsigned int j = 0; j < wordLen; j++) + { + if (direction == 0) // Horizontal + { + salad[startRow][startCol + j] = words[i][j]; + } + else // Vertikal + { + salad[startRow + j][startCol] = words[i][j]; + } + } + placedWords++; + + } + placed = 1; // Mark as placed to exit the attempt loop + } + + } + + // 4. Restliche Felder mit zufälligen Buchstaben füllen + for (unsigned int row = 0; row < searchFieldLen; row++) + { + for (unsigned int col = 0; col < searchFieldLen; col++) + { + if (salad[row][col] == EMPTY_CHAR) + { + salad[row][col] = 'A' + (rand() % 26); // Zufälliger Buchstabe von A-Z + } + } + } + + + printf("Placed %u words out of %u\n", placedWords, wordCount); + return placedWords; // Return the number of successfully placed words } diff --git a/Startcode/gametest.c b/Startcode/gametest.c new file mode 100644 index 0000000..588422d --- /dev/null +++ b/Startcode/gametest.c @@ -0,0 +1,27 @@ + +#include +#include "game.h" + +int main(void) +{ + char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN]; + + const char words[][MAX_WORD_LEN] = { + "HALLO", + "WELT", + "AUTO" + }; + + unsigned int wordCount = sizeof(words) / sizeof(words[0]); + + int placedWords = createWordSalad( + salad, + MAX_SEARCH_FIELD_LEN, + words, + wordCount + ); + + printf("Placed words: %d\n", placedWords); + + return 0; +}