From 9e719321983b54ef83eb6b4e3b64d2a8293b06f4 Mon Sep 17 00:00:00 2001 From: Ayoub Mardi Date: Mon, 2 Nov 2026 20:32:22 +0100 Subject: [PATCH] Implementierung von game.c --- Startcode/game.c | 123 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 117 insertions(+), 6 deletions(-) diff --git a/Startcode/game.c b/Startcode/game.c index 803301f..cda8fa6 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -6,12 +6,123 @@ #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) +// 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) { + unsigned int placedWords = 0; + // Check the dimensions + if (searchFieldLen == 0 || + searchFieldLen > MAX_SEARCH_FIELD_LEN) + return 0; + + // Initialize the field with empty cells + for (unsigned int row = 0; row < searchFieldLen; row++) + { + for (unsigned int col = 0; col < searchFieldLen; col++) + { + salad[row][col] = EMPTY_CHAR; + } + } + + // Go through all words + for (unsigned int w = 0; w < wordCount; w++) + { + // Determine the length of the word + unsigned int len = 0; + + while (len < MAX_WORD_LEN && + words[w][len] != '\0') + { + len++; + } + + // Skip invalid or too long words + if (len == 0 || + len == MAX_WORD_LEN || + len > searchFieldLen) + continue; + + // Try up to 10 random positions + for (unsigned int attempt = 0; + attempt < MAX_RAND_TRIES_PER_WORD; + attempt++) + { + // 0 = horizontal, 1 = vertical + int vertical = rand() % 2; + + // Generate a valid starting position + unsigned int row = + (unsigned int)(rand() % + (searchFieldLen - + (vertical ? len : 1) + 1)); + + unsigned int col = + (unsigned int)(rand() % + (searchFieldLen - + (vertical ? 1 : len) + 1)); + + int fits = 1; + + // Check if the word fits + for (unsigned int i = 0; i < len; i++) + { + unsigned int r = + row + (vertical ? i : 0); + + unsigned int c = + col + (vertical ? 0 : i); + + if (salad[r][c] != EMPTY_CHAR && + salad[r][c] != words[w][i]) + { + fits = 0; + break; + } + } + + // Try another position if necessary + if (!fits) + continue; + + // Place the word + for (unsigned int i = 0; i < len; i++) + { + unsigned int r = + row + (vertical ? i : 0); + + unsigned int c = + col + (vertical ? 0 : i); + + salad[r][c] = words[w][i]; + } + + placedWords++; + break; + } + } + + // Fill remaining cells with random letters + 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] = + (char)('A' + rand() % 26); + } + } + } + + return (int)placedWords; } + + -- 2.54.0