diff --git a/I2_Wortsalat/Start_Mac/game.c b/I2_Wortsalat/Start_Mac/game.c index d8cc133..04e3dc2 100644 --- a/I2_Wortsalat/Start_Mac/game.c +++ b/I2_Wortsalat/Start_Mac/game.c @@ -10,10 +10,100 @@ /* * 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) { + int placedWords = 0; + 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++) + { + salad[yCoordinat][j] = words[i][j - xCoordinat]; + } + placedWords += 1; + break; + } + } + else + { + 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++) + { + salad[yCoordinat][j] = words[i][j - xCoordinat]; + } + placedWords += 1; + break; + } + } + } + } + } } // Prints the word salad to console