diff --git a/Startcode/game.c b/Startcode/game.c index 1cd98c7..0f6d423 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -16,25 +16,47 @@ int tryPlaceHorizontal(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], u // 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) { + // initialise with '\0' + for (int x = MAX_SEARCH_FIELD_LEN - 1; x >= 0; --x) { + for (int y = MAX_SEARCH_FIELD_LEN - 1; y >= 0; --y) { + salad[x][y] = '\0'; + } + } + // place words randomly int placedWords = 0; srand((unsigned int)time(NULL)); for (int word = 0; word < wordCount; word++) { + // test length: // (re-)try randomly int retries = searchFieldLen * 2; int placed = 0; - + if(strlen(words[word]) >= searchFieldLen) break; + // int vertical = rand() / (RAND_MAX / 2); int vertical = 1; while (!placed && retries > 0) { - int x = rand() / (RAND_MAX / (!vertical ? searchFieldLen - 1 - strlen(words[word]) : searchFieldLen)); - int y = rand() / (RAND_MAX / (vertical ? searchFieldLen - 1 - strlen(words[word]) : searchFieldLen)); - printf("Placed %s at %d,%d\n", words[word], x, y); + unsigned int x = 0; + unsigned int y = 0; + + if (vertical) { + int max_x = searchFieldLen - 1; + int max_y = searchFieldLen - 1 - strlen(words[word]); + x = rand() / (RAND_MAX / max_x); + y = rand() / (RAND_MAX / max_y); + } else { + int max_x = searchFieldLen - 1 - strlen(words[word]); + int max_y = searchFieldLen - 1; + x = rand() / (RAND_MAX / max_x); + y = rand() / (RAND_MAX / max_y); + } + + // printf("Placing %s at %d,%d\n", words[word], x, y); placed = tryPlaceVertical(salad, searchFieldLen, words[word], x, y); - if (placed) printf("Placed %s at %d,%d!\n", words[word], x, y); + // if (placed) printf("Placed %s at %d,%d!\n", words[word], x, y); if (--retries < 1) break; @@ -44,10 +66,16 @@ int createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsi break; else placedWords++; - // printf("Placed!\n"); } // fill empty spaces + for (int x = searchFieldLen - 1; x >= 0; --x) { + for (int y = searchFieldLen - 1; y >= 0; --y) { + if (salad[x][y] == '\0') { + salad[x][y] = 'A' + (rand() / (RAND_MAX / 26)); + } + } + } return placedWords; } @@ -59,8 +87,10 @@ int tryPlaceVertical(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], uns for (int testY = y; testY < (y + wordLen); testY++) { - if (salad[testY][x] != '\0') + if (salad[testY][x] != '\0') { + // printf("Besetzt!\n"); return 0; + } } // if ok then put my word in diff --git a/Startcode/makefile b/Startcode/makefile index 7246e47..d7cddda 100644 --- a/Startcode/makefile +++ b/Startcode/makefile @@ -51,7 +51,7 @@ graphicalGame.o: $(codefolder)/graphicalGame.c TEST_BIN = runTests test: input.o game.o $(codefolder)/unit_tests.c $(unityfolder)/unity.c - $(CC) -Wall -I$(unityfolder) -o $(TEST_BIN) input.o game.o $(codefolder)/unit_tests.c $(unityfolder)/unity.c + $(CC) -Wall -g -I$(unityfolder) -o $(TEST_BIN) input.o game.o $(codefolder)/unit_tests.c $(unityfolder)/unity.c # -------------------------- # Clean # -------------------------- diff --git a/Startcode/runTests b/Startcode/runTests index 5e2cbdb..1180073 100755 Binary files a/Startcode/runTests and b/Startcode/runTests differ