diff --git a/Startcode/game.c b/Startcode/game.c index 86e06fc..a0ee4a7 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -18,8 +18,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi int x = 0; int y = 0; int placedWords = 0; - int i = 0; - int nichtPlatziert =0; typedef enum {vertikal, horizontal} Platzierungsrichtung; for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) //Array Salad mit 0 füllen um es nachher als vergleichsoperator zu nutzen @@ -30,73 +28,67 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi } } - while(placedWords < wordCount) + for(int i = 0; i < wordCount; i++) { - x = rand() % 20 +1; - y = rand() % 20 +1; + int platziert = 0; + int platzierversuche = 0; - Platzierungsrichtung zufaellige_richtung = rand() % 2; - - if(zufaellige_richtung == 0 && salad[y][x] == '0') - { - - if(sizeof(words[i]-1) <= (MAX_SEARCH_FIELD_LEN-y)) // -1, da Nullzeichen sowieso weg muss - { - for(int j = 0; j < sizeof(words[i]-1); j++) - { - if(salad[(y+j)][x] == '0') - { - strncpy(&salad[(y+j)][x], &words[i][j], 1); - } - else if(&salad[(y+j)][x] == &words[i][j]) - { - - } - else - { - nichtPlatziert = 1; - } - } - if(nichtPlatziert == 0) - { - placedWords++; - i++; - } - } - - } - else if( zufaellige_richtung == 1 && salad[y][x] == '0') + while(platziert == 0 && platzierversuche < MAX_RAND_TRIES_PER_WORD ) { - if(sizeof(words[i]-1) <= (MAX_SEARCH_FIELD_LEN-x)) // -1, da Nullzeichen sowieso weg muss + platzierversuche++; + x = rand() % searchFieldLen; + y = rand() % searchFieldLen; + + Platzierungsrichtung zufaellige_richtung = rand() % 2; + + if(zufaellige_richtung == vertikal && strlen(words[i]) <= (searchFieldLen-y)) + { + int check = 0; + for(int k = 0; k < strlen(words[i]); k++) { - for(int j = 0; j < sizeof(words[i]-1); j++) + if(salad[y+k][x] == '0' || salad[y+k][x] == words[i][k]) { - if(salad[y][(x+j)] == '0') - { - strncpy(&salad[y][(x+j)], &words[i][j], 1); - - } - else if(&salad[y][(x+j)] == &words[i][j]) - { - - } - else - { - nichtPlatziert = 1; - } + check ++; + } + } + if(check == strlen(words[i])) + { + for(int j = 0; j < strlen(words[i]); j++) + { + salad[y+j][x] = words[i][j]; } - if(nichtPlatziert == 0) + placedWords++; + platziert = 1; + } + + } + else if( zufaellige_richtung == horizontal && strlen(words[i]) <= (searchFieldLen-x)) + { + int check = 0; + for(int k = 0; k < strlen(words[i]); k++) { + if(salad[y][x+k] == '0' || salad[y][x+k] == words[i][k]) + { + check ++; + } + } + + if(check == strlen(words[i])) + { + for(int j = 0; j < strlen(words[i]); j++) + { + + salad[y][x+j] = words[i][j]; + } placedWords++; - i++; + platziert = 1; } } - } } - for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) //Array Salad mit 0 füllen um es nachher als vergleichsoperator zu nutzen - { + for(int i =0; i < MAX_SEARCH_FIELD_LEN; i++) + { for( int j = 0; j < MAX_SEARCH_FIELD_LEN; j++) { if(salad[i][j] == '0') @@ -105,6 +97,6 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi } } } - - return placedWords; + + return placedWords; } diff --git a/Startcode/main.c b/Startcode/main.c index 0d3ecb6..4f80285 100644 --- a/Startcode/main.c +++ b/Startcode/main.c @@ -43,9 +43,10 @@ int main(int argc, char *argv[]) } else { - fprintf(stderr, "konnte nicht alle Wörter platzieren %d von %d \n", placedWords, wordCount); + fprintf(stderr, "konnte nicht alle Woerter platzieren %d von %d \n", placedWords, wordCount); exitCode = EXIT_FAILURE; } + } else { // Print error message if file couldn't be opened