diff --git a/Start_Windows/game.c b/Start_Windows/game.c index b029559..d286ad0 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -11,27 +11,52 @@ /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ + +int checkforOverlap(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], short x, short y, short richtung, const char wort[]) +{ + for(size_t i = 0 ; i < strlen(wort); i++ ) + { + short xoffset = (richtung) ? i : 0; + short yoffset = (richtung) ? 0 : i; + if (!(salad[x+xoffset][y+yoffset]=='#' || salad[x+xoffset][y+yoffset]==wort[i])) + { + return(-1); + } + } + return(1); +} // 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) { - for(wordCount;wordCount>0;wordCount--) + for(short i = searchFieldLen; i>0; i--) { - size_t platzbedarf = strlen(words[wordCount-1]); + for(short j = searchFieldLen; j>0; j--) + { + salad[i][j]= "#"; + } + } + + for(short i = wordCount;i>0;i--) + { + size_t platzbedarf = strlen(words[i-1]); if (platzbedarf >= searchFieldLen) { printf("%s konnte nicht eingefuegt werden da es groeßer als das Feld ist",words[wordCount-1]); continue; } + srand(time(NULL)); + short x,y,waagrecht; do { - srand(time(NULL)); short max = searchFieldLen - platzbedarf; - short position = rand() % (max +1); - srand(time(NULL)); - short waagrecht = rand()%2; - + short position1 = rand() % (max +1); + short position2 = rand() % (searchFieldLen); + waagrecht = rand()%2; + x = (waagrecht) ? position1:position2; + y = (waagrecht) ? position2:position1; - }while(1); + + }while(checkforOverlap(salad,x,y,waagrecht,words[i-1])!=1); } @@ -42,5 +67,11 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - + for (unsigned int y = 0; y < searchFieldLen; y++) { + for (unsigned int x = 0; x < searchFieldLen; x++) { + printf("%c ", salad[x][y]); // oder printf("%c", salad[x][y]); ohne Leerzeichen + } + printf("\n"); // neue Zeile nach jeder Zeile + } } +