From fb2febcb03a49a7ba6d054b951aed58e37ea211d Mon Sep 17 00:00:00 2001 From: Timo Hertel Date: Mon, 20 Oct 2025 15:44:39 +0200 Subject: [PATCH] createWordsalat Teil 1 --- I2_Wortsalat/Start_Mac/game.c | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/I2_Wortsalat/Start_Mac/game.c b/I2_Wortsalat/Start_Mac/game.c index d8cc133..b73f5d7 100644 --- a/I2_Wortsalat/Start_Mac/game.c +++ b/I2_Wortsalat/Start_Mac/game.c @@ -10,10 +10,70 @@ /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ +void fillRestWithRandom(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++) + { + if (salad[i][j] == '0') { + salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A'; + } + } + } +} + // Creates the word salad by placing words randomly and filling empty spaces +// - 2D array salad -> fertiger Wortsalat +// - searchFieldLen -> Dimension der Salatschüssel +// - 2D array words -> gegebene Wörter +// - wordCount -> anzahl an Wörtern int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { + for (int i = 0; i < searchFieldLen; i++) + { + for (int j = 0; j < searchFieldLen; j++) + { + salad[i][j] = '0'; + } + } + for (int i = 0; i < wordCount; i++) + { + int wordLength = strlen(words[i]); + if (rand() % 2) // erzeugt Zufallszahl 0 oder 1: 0 -> Horizontal; 1 -> Vertical + { + for (int placementTry = 0; placementTry < MAX_RAND_TRIES_PER_WORD; placementTry++) + { + int fits = 1; + int xCoordinat = rand() % (searchFieldLen - wordLength + 1); + int yCoordinat = rand() % searchFieldLen; + for (int j = xCoordinat; j < wordLength + xCoordinat; j++) + { + if(salad[yCoordinat][j] != '0') + { + fits = 0; + break; + } + } + if (!fits) //neue Position versuchen, wenn ein Buchstabe schon belegt + { + continue; + } + else //Wort platzieren + { + for (int j = xCoordinat; j < wordLength + xCoordinat; j++) + { + } + break; + } + } + else + { + + } + } + } } // Prints the word salad to console