From 4d7ccdf9ff414862bc880899673c602111df1fd4 Mon Sep 17 00:00:00 2001 From: Adrian Schulze Date: Thu, 29 Oct 2026 21:32:46 +0100 Subject: [PATCH 1/2] erster Versuch --- Startcode/game.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 135 insertions(+), 1 deletion(-) diff --git a/Startcode/game.c b/Startcode/game.c index 803301f..1011b71 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -10,8 +10,142 @@ /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ +unsigned int findmaxLength(const char wordlist[][MAX_WORD_LEN], unsigned int Number); +unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_LEN], char salad[][MAX_SEARCH_FIELD_LEN], unsigned int position); +void putInTheWord(const char words[][MAX_WORD_LEN], unsigned int wort, char tabelle[][MAX_SEARCH_FIELD_LEN], unsigned int X, unsigned int Y, unsigned int richtung, unsigned int laenge); +void initializeField(unsigned int groesse, char field[][MAX_SEARCH_FIELD_LEN]); +void fillTheRest(char field[][MAX_SEARCH_FIELD_LEN], unsigned int sizeOfField); + // 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 maxLength = 0; + unsigned int sizeOfField = 0; + unsigned int returnValue = 0; + // search for longest word + maxLength = findmaxLength(words, wordCount); + // Size of Field! + if (searchFieldLen < maxLength) + sizeOfField = maxLength; + else + sizeOfField = searchFieldLen; + initializeField(sizeOfField, salad); + // random + srand(time(NULL)); + // for all words put them in + for (unsigned int i = 0; i < wordCount; i++){ + if (findAPlace(sizeOfField,words,salad,i) == 1) { + returnValue += 1; + } + } + fillTheRest(salad, sizeOfField); + return returnValue; } + +unsigned int findmaxLength(const char wordlist[][MAX_WORD_LEN], unsigned int Number) +{ + unsigned int max = 0; + for (unsigned int i = 0; i < Number; i++) + { + for (unsigned int j = 0; j < MAX_WORD_LEN; j++) + { + if(wordlist[i][j] == '\0') { + if (max < j) + max = j; + break; + } + } + } + return max; +} + +// unsigned int free(unsigned int richtung, unsigned int laenge, unsigned int x, unsigned int y, char spielfeld [][MAX_SEARCH_FIELD_LEN], unsigned int groesse) +// { + +// } +void initializeField(unsigned int groesse, char field[][MAX_SEARCH_FIELD_LEN]) +{ + for (unsigned int j = 0; j < groesse; j++){ + for (unsigned int i = 0; i < groesse; i++) + { + field[j][i] = EMPTY_CHAR; + } + + } +} + +unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_LEN], char salad[][MAX_SEARCH_FIELD_LEN], unsigned int position) +{ + unsigned int direction, length = 0; + unsigned int maxLineColumn = 0; + unsigned int xNumber, yNumber = 0; + + direction = rand() % 2; // choose 0 = X oder 1 = Y - direction + length = strlen(words[position]); + maxLineColumn = (sizeOfField + 1) - length; + + for (unsigned int j = 0; j < MAX_RAND_TRIES_PER_WORD; j++) + { + if (direction == 0){ + yNumber = rand() % maxLineColumn; + xNumber = rand() % sizeOfField; + } // Maximale Werte je nach X - Y - Richtung + else { + xNumber = rand() % maxLineColumn; + yNumber = rand() % sizeOfField; + } + if(direction == 1){ + unsigned int a = xNumber; + while (a < (xNumber + length) && salad[a][yNumber] == 0) + a++; + if (a == xNumber+length) { + putInTheWord(words, position, salad, xNumber, yNumber, direction, length); + return 1; + } + } + else{ + unsigned int a = yNumber; + while (a < (yNumber + length) && salad[xNumber][a] == 0) + a++; + if (a == yNumber+length) { + putInTheWord(words, position, salad, xNumber, yNumber, direction, length); + return 1; + } + } + // if (j == (MAX_RAND_TRIES_PER_WORD -1)){ + // if (search() == 1) + // putInTheWord(words, position, salad, xNumber, yNumber, direction, length); + // return 1; + // } + } + return 0; +} + +void putInTheWord(const char words[][MAX_WORD_LEN], unsigned int wort, char tabelle[][MAX_SEARCH_FIELD_LEN], unsigned int X, unsigned int Y, unsigned int richtung, unsigned int laenge) +{ + if (richtung == 0){ + for (unsigned int i=0; i < laenge; i++) + tabelle[X][(i+Y)] = words[wort][i]; + } + else { + for (unsigned int i=0; i < laenge; i++) + tabelle[X+i][(Y)] = words[wort][i]; + } +} + +void fillTheRest(char field[][MAX_SEARCH_FIELD_LEN], unsigned int sizeOfField) +{ + for (unsigned int i = 0; i < sizeOfField; i++) + { + for (unsigned int j = 0; j < sizeOfField; j++) + { + if (field[i][j] == 0) { + char random = 'A' + rand() % 26; + field[i][j] = random; + } + printf("%1c ", field[i][j]); + } + printf("\n"); + + } +} \ No newline at end of file From ab3d269759a6fc1f38f9258496ab6f0488bee33a Mon Sep 17 00:00:00 2001 From: Andreas Binner Date: Fri, 30 Oct 2026 09:45:10 +0100 Subject: [PATCH 2/2] changed numbers to variable --- Startcode/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Startcode/main.c b/Startcode/main.c index fbf9ad6..7152f65 100644 --- a/Startcode/main.c +++ b/Startcode/main.c @@ -41,9 +41,9 @@ int main(int argc, char *argv[]) // Start the game if successful // error message if some words couldn't be placed - if(wordCount > 19) + if(wordCount >= SALAD_SIZE) { - if(placedWords != 20) + if(placedWords != SALAD_SIZE) { exitCode = EXIT_FAILURE; printf("Only %d of %d possible words could be placed\n", placedWords, 20);