diff --git a/Startcode/game.c b/Startcode/game.c index 803301f..2e61e5b 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -11,7 +11,59 @@ * restliche Felder mit zufälligen Buchstaben füllen */ // 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 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)); + for(int i = 0; i < searchFieldLen; i++){ + for(int j = 0; i < searchFieldLen; i++){ + salad[i][j] = 1; + } + } + int failed = 0; + for(int i = 0; i < wordCount; i++){ + int wordlen = strlen(words[i]); + if(wordlen >= searchFieldLen){ + failed++; + } + else{ + if(rand() % 2){ + //place horizontal + while(wordlen){ + int row = rand() % searchFieldLen; + int col = rand() % (searchFieldLen - wordlen); + int j = 0; + for(; (j <= wordlen) && (salad[row][col + j] == 1); j++){} + if(j == wordlen + 1){ + //word can fit + wordlen = 0; + strcpy(&(salad[row][col]), words[i]); + } + } + } + else{ + //place vertical + while(wordlen){ + int row = rand() % (searchFieldLen - wordlen); + int col = rand() % searchFieldLen; + int j = 0; + for(; (j <= wordlen) && (salad[row + j][col] == 1); j++){} + if(j == wordlen + 1){ + //word can fit + for(int k = 0; k <= wordlen; k++){ + salad[row + k][col] = words[i][k]; + } + wordlen = 0; + } + } + } + } + } + //fill remaining spots with random chars + for(int i = 0; i < searchFieldLen; i++){ + for(int j = 0; j < searchFieldLen; j++){ + salad[i][j] = salad[i][j] == 1 ? (rand() % 25) + 65 : salad[i][j]; + } + } + return wordCount - failed; }