From b15fb04b88ffa2507f1c91abab6f213d1b35443f Mon Sep 17 00:00:00 2001 From: Hofmann Jonas Date: Sat, 25 Oct 2025 17:47:28 +0200 Subject: [PATCH 1/6] funktion createWordSalad in game.c begonnen --- Start_Windows/game.c | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..717c86d 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -6,18 +6,99 @@ #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 */ +// enum to make it more clear what de current orientation is +typedef enum +{ + HORIZONTAL, + VERTIKAL +} orientation; + + +// new struct for handing over the entire wordPosition +// alignment is HORIZONTAL or VERTICAL +// rowOrColumn is the int of the colum or row the word is suposed to be placed in +// startingCell is the int of where in the row/colum the first leter of the word suposed to be placed +typedef struct +{ + orientation alignment; + unsigned int rowOrColumn; + unsigned int startingCell; +} wordPosition; + + +// Choses a random Position for the current Word +wordPosition choserandomPosition(unsigned int searchFieldLen, unsigned int wordLength) +{ + wordPosition position = {0, 0, 0}; + + srand(time(NULL)); + position.alignment = rand()%(VERTIKAL - HORIZONTAL + 1) + HORIZONTAL; + position.rowOrColumn = rand()%(searchFieldLen); + position.startingCell = rand()%(searchFieldLen - wordLength); + + return position; +} + + +// Checks if the current Position is free +// returns 1 if the Position is free +// returns 0 if the Position is occupied +int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPosition position,unsigned int wordLength, unsigned int serchFieldLen) +{ + int i = 0; + int letterFound = 0; + int positionfree = 0; + + if (position.alignment == HORIZONTAL) + { + for (i = position.startingCell; i < serchFieldLen; i++) + { + if ((salad[position.rowOrColumn][i] < 'A') || (salad[position.rowOrColumn][i] > 'z')) + { + letterFound = 1; + } + } + } + else if (position.alignment == VERTIKAL) + { + for (i = position.startingCell; i < serchFieldLen; i++) + { + if ((salad[i][position.rowOrColumn] < 'A') || (salad[i][position.rowOrColumn] > 'z')) + { + letterFound = 1; + } + } + } + + positionfree = !letterFound; + + return positionfree; +} + + // 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) { + int i = 0; + wordPosition currentWordPosition = {0,0,0}; + for (i = 1; i <= wordCount; i++) + { + currentWordPosition = choserandomPosition(searchFieldLen, strlen(words[i])); + + } + } + // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { } + From ea373b6bad7b11e3db557228406a431db8b8fc21 Mon Sep 17 00:00:00 2001 From: Hofmann Jonas Date: Sat, 25 Oct 2025 21:07:02 +0200 Subject: [PATCH 2/6] 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; } From f407d5a36f1044a4d0761c950a3eff6a127f1b75 Mon Sep 17 00:00:00 2001 From: Hofmann Jonas Date: Sat, 25 Oct 2025 21:37:36 +0200 Subject: [PATCH 3/6] =?UTF-8?q?funktion=20placeRandomLetters=20f=C3=BCr=20?= =?UTF-8?q?createWordSalad=20erg=C3=A4nzt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Start_Windows/game.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 08b8025..9923771 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -115,6 +115,31 @@ int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPositi return 1; } +// Places random letters into the +// empty fields of the wordSalad +void placeRandomLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) +{ + int i = 0; + int j = 0; + + + srand(time(NULL)); + + for (i = 0; i < searchFieldLen; i++) + { + for (j = 0; i < searchFieldLen; j++) + { + if (salad[i][j]) + { + if ((salad[i][j] < 'A') && (salad[i][j] > 'z')) + { + salad[i][j] = rand()%('Z' - 'A' + 1) + 'A'; + } + } + } + } +} + // Creates the word salad by placing words randomly and filling empty spaces // returnes the number of sucessfully placed words @@ -143,6 +168,8 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen); } } + + placeRandomLetters(salad, searchFieldLen); return placedWords; From 2f625c98b91dcf7b7723fb9b0c7aa58e918bf0df Mon Sep 17 00:00:00 2001 From: Hofmann Jonas Date: Sat, 25 Oct 2025 21:45:56 +0200 Subject: [PATCH 4/6] =?UTF-8?q?funktion=20showWordSalad=20erg=C3=A4nzt,=20?= =?UTF-8?q?fehler=20in=20placeRandomLetters=20korrigiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Start_Windows/game.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 9923771..397d213 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -129,12 +129,9 @@ void placeRandomLetters(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], { for (j = 0; i < searchFieldLen; j++) { - if (salad[i][j]) + if ((salad[i][j] < 'A') && (salad[i][j] > 'z')) { - if ((salad[i][j] < 'A') && (salad[i][j] > 'z')) - { - salad[i][j] = rand()%('Z' - 'A' + 1) + 'A'; - } + salad[i][j] = rand()%('Z' - 'A' + 1) + 'A'; } } } @@ -171,7 +168,7 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi placeRandomLetters(salad, searchFieldLen); - + return placedWords; } @@ -179,6 +176,17 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { + int i = 0; + int j = 0; + + for (i = 0; i < searchFieldLen; i++) + { + for (j = 0; i < searchFieldLen; j++) + { + printf("%c", salad[i][j]); + } + printf("\n"); + } } From d1fd9b38ea8b46edd3bfb83fa5fe3d5980f6d34c Mon Sep 17 00:00:00 2001 From: Hofmann Jonas Date: Tue, 28 Oct 2025 08:46:26 +0100 Subject: [PATCH 5/6] fixed logic error in 'checkIfPositionIsFree' in game.c and updated some comments in game.c and main.c --- Start_Windows/game.c | 4 ++-- Start_Windows/main.c | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 397d213..23b8216 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -7,7 +7,7 @@ #define EMPTY_CHAR 0 -//TODO: Spiellogik implementieren: +//DONE: Spiellogik implementieren: /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ @@ -72,7 +72,7 @@ int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN] // 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[i][position.rowOrColumn] >= 'A') && (salad[i][position.rowOrColumn] <= 'z')) { letterFound = 1; } diff --git a/Start_Windows/main.c b/Start_Windows/main.c index c591f9c..69016d5 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -8,9 +8,9 @@ #define SALAD_SIZE 20 // selbstgeschriebene Definitionen: -// annahmen ist das windowWidth in pixeln gegeben -// werden muss, da auch andere funktionen wie -// createCharSquarePanel in Pixeln arbeiten +// annahmen ist das windowWidth in pixeln gegeben +// werden muss, da auch andere funktionen wie +// createCharSquarePanel in Pixeln arbeiten #define WINDOW_WIDTH 540 @@ -57,8 +57,8 @@ int main(int argc, char *argv[]) else { // annahme: windowWidth wird in Pixeln gesucht, - // da auch andere funktionen, wie createCharSquarePanel - // in Pixeln arbeiten + // da auch andere funktionen, wie createCharSquarePanel + // in Pixeln arbeiten startGame(wordSalad, MAX_SEARCH_FIELD_LEN, words, placedWords, WINDOW_WIDTH); } From 7dc036df15ddf73e886535946bc92402278a0280 Mon Sep 17 00:00:00 2001 From: Hofmann Jonas Date: Tue, 28 Oct 2025 09:30:39 +0100 Subject: [PATCH 6/6] changed two comments --- Start_Windows/game.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 23b8216..847ff47 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -57,7 +57,7 @@ int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN] if (position.alignment == HORIZONTAL) { // checking to see if position is free - // by scanning each row in the given Column + // by scanning each column in the given row for (i = position.startingCell; (i < serchFieldLen) && !(letterFound); i++) { if ((salad[position.rowOrColumn][i] >= 'A') && (salad[position.rowOrColumn][i] <= 'z')) @@ -69,7 +69,7 @@ int checkIfPositionIsFree(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN] else if (position.alignment == VERTIKAL) { // checking to see if position is free - // by scanning each row in the given Column + // 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'))