From 4c6a4b41e29b060d9655c7ecfd36e5d564587446 Mon Sep 17 00:00:00 2001 From: Max-R Date: Tue, 4 Nov 2025 09:07:52 +0100 Subject: [PATCH] =?UTF-8?q?m=C3=BCsste=20passen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Start_Windows/.idea/editor.xml | 573 +++++++++++++++++++-------------- Start_Windows/game.c | 100 +++++- Start_Windows/input.c | 28 ++ Start_Windows/main.c | 9 +- 4 files changed, 470 insertions(+), 240 deletions(-) diff --git a/Start_Windows/.idea/editor.xml b/Start_Windows/.idea/editor.xml index 55d1bc1..1f0ef49 100644 --- a/Start_Windows/.idea/editor.xml +++ b/Start_Windows/.idea/editor.xml @@ -1,483 +1,580 @@ + \ No newline at end of file diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..c42505e 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -13,11 +13,109 @@ // 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) { + srand(time(NULL)); + // wipe field + int placedWords = 0; + for (int i = 0; i < searchFieldLen; i++) + { + for (int j = 0; j < searchFieldLen ; j++) + salad[i][j] = EMPTY_CHAR; + } + // place Words + for (int wordNumber = 0; wordNumber < wordCount; wordNumber++) + { + int wordlength = strlen(words[wordNumber]); + int placed = 0; + + for (int try = 0; (try < MAX_RAND_TRIES_PER_WORD) && !placed; try++) + { + int horizontal = rand() % 2; // 0 vertikal --> 1 horizontal + + int x = rand() % searchFieldLen; + int y = rand() % searchFieldLen; + + if (horizontal) + { + if (x + wordlength > searchFieldLen) // word does not fit + { + continue; + } + + int fits = 1; + + for (int i = 0; i < wordlength; i++) // position already occupied by other word + { + if (salad[y][x + i] != EMPTY_CHAR) + { + fits = 0; + break; + } + } + + if (fits) + { + for (int i = 0; i < wordlength; i++) + { + salad[y][x + i] = words[wordNumber][i]; + } + placed = 1; + placedWords++; + } + } + else + { + if ((y + wordlength) > searchFieldLen) + { + continue; + } + + int fits = 1; + + for (int i = 0; i < wordlength; i++) // position already occupied by other word + { + if (salad[y + i][x] != EMPTY_CHAR) + { + fits = 0; + break; + } + } + + if (fits) + { + for (int i = 0; i < wordlength; i++) + { + salad[y + i][x] = words[wordNumber][i]; + } + placed = 1; + placedWords++; + } + } + } + } + // fill rest with random characters + + for (int i = 0; i < searchFieldLen; i++) + { + for (int j = 0; j < searchFieldLen; j++) + { + if (salad[i][j] == EMPTY_CHAR) + { + salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A'; // 90 ist Z in ASCII und A ist 65 + } + } + } + return placedWords; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - + for (int i = 0; i < searchFieldLen; i++) + { + for (int j = 0; j < searchFieldLen; j++) + { + printf("%c", salad[i][j]); + } + } } diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..0035ae4 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -8,5 +8,33 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + //fopen und fclose bereits, in der main und mit *file übergeben // + if (file == NULL) + { + perror("Error invalid pointer to file"); + return -1; + } + + char line[1024]; + unsigned int count = 0; + + while (fgets(line, sizeof(line), file) && (count < maxWordCount)) + { + line[strcspn(line, "\n")] = '\0'; + + char *token = strtok(line, " ;,"); + while (token && (count < maxWordCount)) + { + for (int i = 0; token[i] != '\0'; i++) + { + token[i] = toupper(token[i]); + } + + strcpy(words[count], token); + count++; + token = strtok(NULL, " ;,"); + } + } + return count; } \ No newline at end of file diff --git a/Start_Windows/main.c b/Start_Windows/main.c index 03da755..af3b6a9 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -40,7 +40,14 @@ int main(int argc, char *argv[]) // Check if all words were successfully placed // Start the game if successful // error message if some words couldn't be placed - + if (wordCount != placedWords) + { + fprintf(stderr, "Error: %u out of %u were placed\n", placedWords, wordCount); + } + else + { + startGame(wordSalad, SALAD_SIZE, words, placedWords, 1024); + } } else {