From ea373b6bad7b11e3db557228406a431db8b8fc21 Mon Sep 17 00:00:00 2001 From: Hofmann Jonas Date: Sat, 25 Oct 2025 21:07:02 +0200 Subject: [PATCH] funktion createWordSalad in game.c fertiggestellt, noch ungetestet --- Start_Windows/game.c | 69 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 717c86d..08b8025 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -56,9 +56,11 @@ int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN] if (position.alignment == HORIZONTAL) { - for (i = position.startingCell; i < serchFieldLen; i++) + // checking to see if position is free + // by scanning each row in the given Column + for (i = position.startingCell; (i < serchFieldLen) && !(letterFound); i++) { - if ((salad[position.rowOrColumn][i] < 'A') || (salad[position.rowOrColumn][i] > 'z')) + if ((salad[position.rowOrColumn][i] >= 'A') && (salad[position.rowOrColumn][i] <= 'z')) { letterFound = 1; } @@ -66,9 +68,11 @@ int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN] } else if (position.alignment == VERTIKAL) { - for (i = position.startingCell; i < serchFieldLen; i++) + // checking to see if position is free + // by scanning each row in the given Column + for (i = position.startingCell; (i < serchFieldLen) && !(letterFound); i++) { - if ((salad[i][position.rowOrColumn] < 'A') || (salad[i][position.rowOrColumn] > 'z')) + if ((salad[position.rowOrColumn][i] >= 'A') && (salad[position.rowOrColumn][i] <= 'z')) { letterFound = 1; } @@ -81,18 +85,67 @@ int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN] } -// 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) +// Places the current word into the +// selected position +// returns 1 if sucessfull +int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPosition position, const char currentWord[], unsigned int currentWordLen, unsigned int serchFieldLen) { int i = 0; + + + if (position.alignment == HORIZONTAL) + { + + for (i = position.startingCell; (i < serchFieldLen) && (i < currentWordLen); i++) + { + salad[position.rowOrColumn][i] = currentWord[i]; + } + salad[position.rowOrColumn][i] = '\0'; + } + else if (position.alignment == VERTIKAL) + { + for (i = position.startingCell; (i < serchFieldLen) && (i < currentWordLen); i++) + { + salad[i][position.rowOrColumn] = currentWord[i]; + } + salad[i][position.rowOrColumn] = '\0'; + } + + + return 1; +} + + +// Creates the word salad by placing words randomly and filling empty spaces +// returnes the number of sucessfully placed words +int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) +{ + int i = 0; + int j = 0; + int positionFound = 0; + int placedWords = 0; + char currentWord[MAX_WORD_LEN]; wordPosition currentWordPosition = {0,0,0}; + for (i = 1; i <= wordCount; i++) { - currentWordPosition = choserandomPosition(searchFieldLen, strlen(words[i])); - + do + { + currentWordPosition = choserandomPosition(searchFieldLen, strlen(words[i])); + positionFound = checkIfPositionIsFree(salad, currentWordPosition, strlen(words[i]), searchFieldLen); + j++; + } while ((j < MAX_RAND_TRIES_PER_WORD) && !(positionFound)); + + if (positionFound) + { + strcpy(currentWord, words[i-1]); + placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen); + } } + + return placedWords; }