From bd39d8ad5e578afd5f2850536f50f4c47c2048f0 Mon Sep 17 00:00:00 2001 From: Adrian Schulze Date: Fri, 30 Oct 2026 10:00:04 +0100 Subject: [PATCH] zweiter Versuch --- Startcode/game.c | 70 +++++++++++++++++++++++++++++++++++------------- Startcode/main.c | 10 +++++-- 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/Startcode/game.c b/Startcode/game.c index 1011b71..11b04b1 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -15,6 +15,7 @@ unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_ 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); +unsigned int search(char salad[][MAX_SEARCH_FIELD_LEN], unsigned int length, unsigned int sizeOfField, unsigned int *x, unsigned int *y, unsigned int *direction); // 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) @@ -30,8 +31,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi 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) { @@ -59,10 +58,6 @@ unsigned int findmaxLength(const char wordlist[][MAX_WORD_LEN], unsigned int Num 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++){ @@ -82,7 +77,7 @@ unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_ direction = rand() % 2; // choose 0 = X oder 1 = Y - direction length = strlen(words[position]); - maxLineColumn = (sizeOfField + 1) - length; + maxLineColumn = (sizeOfField - length) + 1; for (unsigned int j = 0; j < MAX_RAND_TRIES_PER_WORD; j++) { @@ -96,7 +91,7 @@ unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_ } if(direction == 1){ unsigned int a = xNumber; - while (a < (xNumber + length) && salad[a][yNumber] == 0) + while (a < (xNumber + length) && salad[a][yNumber] == EMPTY_CHAR) a++; if (a == xNumber+length) { putInTheWord(words, position, salad, xNumber, yNumber, direction, length); @@ -105,18 +100,19 @@ unsigned int findAPlace (unsigned int sizeOfField, const char words [][MAX_WORD_ } else{ unsigned int a = yNumber; - while (a < (yNumber + length) && salad[xNumber][a] == 0) + while (a < (yNumber + length) && salad[xNumber][a] == EMPTY_CHAR) 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; - // } + if (j == (MAX_RAND_TRIES_PER_WORD -1)){ + if (search(salad,length, sizeOfField, &xNumber, &yNumber, &direction) == 1) { + putInTheWord(words, position, salad, xNumber, yNumber, direction, length); + return 1; + } + } } return 0; } @@ -139,13 +135,51 @@ void fillTheRest(char field[][MAX_SEARCH_FIELD_LEN], unsigned int sizeOfField) { for (unsigned int j = 0; j < sizeOfField; j++) { - if (field[i][j] == 0) { + if (field[i][j] == EMPTY_CHAR) { char random = 'A' + rand() % 26; field[i][j] = random; } - printf("%1c ", field[i][j]); } - printf("\n"); - } +} + +unsigned int search(char field[][MAX_SEARCH_FIELD_LEN], unsigned int length, unsigned int sizeOfField, unsigned int *x, unsigned int *y, unsigned int *direction) +{ + unsigned int freeLength = 0; + for (unsigned int i = 0; i < sizeOfField; i++) // search for free space in any line + { + freeLength = 0; + for (unsigned int j = 0; j < sizeOfField; j++) + { + if (field[i][j] == EMPTY_CHAR) + freeLength += 1; + else + freeLength = 0; + if (freeLength >= length){ + *x = i; + *y = j+1-length; + *direction = 0; + return 1; + } + + } + } + for (unsigned int i = 0; i < sizeOfField; i++) // search for free space in any column + { + freeLength = 0; + for (unsigned int j = 0; j < sizeOfField; j++) + { + if (field[j][i] == EMPTY_CHAR) + freeLength += 1; + else + freeLength = 0; + if (freeLength == length){ + *x = j+1-length; + *y = i; + *direction = 1; + return 1; + } + } + } + return 0; } \ No newline at end of file diff --git a/Startcode/main.c b/Startcode/main.c index 03da755..fb36aef 100644 --- a/Startcode/main.c +++ b/Startcode/main.c @@ -10,7 +10,7 @@ int main(int argc, char *argv[]) { int exitCode = EXIT_SUCCESS; - + srand(time(NULL)); // Check if the correct number of arguments is provided if(argc != 2) { @@ -35,7 +35,13 @@ int main(int argc, char *argv[]) // Create the word salad by placing words into grid placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); - + if (placedWords == wordCount) { + startGame(wordSalad, SALAD_SIZE, words, wordCount, 1024); + } + else { + printf("Could only %u of %u words. ", placedWords, wordCount); + exitCode = EXIT_FAILURE; + } // TODO: // Check if all words were successfully placed // Start the game if successful