diff --git a/Start_Windows/game.c b/Start_Windows/game.c index f7fa67d..cd33f4e 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -55,7 +55,10 @@ wordPosition choserandomPosition(unsigned int searchFieldLen, unsigned int wordL //srand(time(NULL)); position.alignment = rand()%(VERTIKAL - HORIZONTAL + 1) + HORIZONTAL; position.rowOrColumn = rand()%(searchFieldLen); + printf("Wordlength: %d\n", wordLength); position.startingCell = rand()%(searchFieldLen - wordLength); + printf("Starting Cell: %d\n", position.startingCell); + return position; } @@ -112,7 +115,7 @@ int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPositi if (position.alignment == HORIZONTAL) { - for (i = position.startingCell; (i < serchFieldLen) || (j < currentWordLen); i++) + for (i = position.startingCell; (i < serchFieldLen) && (currentWord[j] != '\0'); i++) { salad[position.rowOrColumn][i] = currentWord[j]; printf("%c",currentWord[j]); @@ -122,7 +125,7 @@ int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPositi } else if (position.alignment == VERTIKAL) { - for (i = position.startingCell; (i < serchFieldLen) || (j < currentWordLen); i++) + for (i = position.startingCell; (i < serchFieldLen) && (currentWord[j] != '\0'); i++) { salad[i][position.rowOrColumn] = currentWord[j]; printf("%c",currentWord[j]); @@ -130,7 +133,7 @@ int placeWord(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], wordPositi } salad[i][position.rowOrColumn] = '\0'; } - + printf("\n"); return 1; } @@ -162,11 +165,19 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi { int i = 0; int j = 0; + int k = 0; int positionFound = 0; int placedWords = 0; + char nullWord[MAX_WORD_LEN]; char currentWord[MAX_WORD_LEN]; wordPosition currentWordPosition = {0,0,0}; + + for (k = 0; k < MAX_WORD_LEN; k++) + { + nullWord[k] = '\0'; + } + srand(time(NULL)); initializeWordsalad(salad, searchFieldLen); showWordSalad(salad, searchFieldLen); @@ -174,18 +185,19 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi for (i = 1; i <= wordCount; i++) { j = 0; + + strcpy(currentWord, nullWord); + strcpy(currentWord, words[i-1]); + do { - currentWordPosition = choserandomPosition(searchFieldLen, strlen(words[i])); - positionFound = checkIfPositionIsFree(salad, currentWordPosition, strlen(words[i]), searchFieldLen); + currentWordPosition = choserandomPosition(searchFieldLen, strlen(currentWord)); + positionFound = checkIfPositionIsFree(salad, currentWordPosition, strlen(currentWord), searchFieldLen); j++; } while ((j < MAX_RAND_TRIES_PER_WORD) && !(positionFound)); if (positionFound) { - strcpy(currentWord, "\0"); - strcpy(currentWord, words[i-1]); - printf("%d, %d, %d \n", currentWordPosition.alignment, currentWordPosition.rowOrColumn, currentWordPosition.startingCell); printf("%s\n", currentWord); placedWords += placeWord(salad, currentWordPosition, currentWord, strlen(currentWord), searchFieldLen); diff --git a/Start_Windows/game.o b/Start_Windows/game.o index b2fada6..8dc0825 100644 Binary files a/Start_Windows/game.o and b/Start_Windows/game.o differ