diff --git a/Start_Windows/game.c b/Start_Windows/game.c index d8cc133..e29eee3 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -6,6 +6,8 @@ #define MAX_RAND_TRIES_PER_WORD 10 #define EMPTY_CHAR 0 + + //TODO: Spiellogik implementieren: /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ @@ -13,7 +15,100 @@ // 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 direction = 0; + int step = 0; + int added = 0; + int addedWords = 0; + int positionX = 0; + int positionY = 0; + srand(time(NULL)); + + for(int i = 0; i < searchFieldLen; i++) + { + for(int j = 0; j < searchFieldLen; j++) + { + salad[i][j] = EMPTY_CHAR; + } + } + while(step <= wordCount) + { + int tries = 0; + int wordLength = 0; + direction = rand() % 1; + + for (int i = 0; i < MAX_WORD_LEN; i++) + { + if (words[step][i] == '\0') + break; + wordLength++; + } + if(direction == 0) + { + while(added <= 0 && tries < MAX_RAND_TRIES_PER_WORD){ + positionY = rand() % searchFieldLen-1; + positionX = rand() % searchFieldLen-1; + if(searchFieldLen - positionX >= wordLength) + { + + for(int i = 0; i < searchFieldLen; i++) + { + if(salad[positionY][positionX+i] != EMPTY_CHAR) + { + tries++; + break; + } + for (int j = 0; j < searchFieldLen; j++) + { + salad[positionY][positionX+j] = words[step][j]; + added = 1; + } + } + + } + else + { + tries++; + } + } + step++; + tries = 0; + added = 0; + } + if(direction == 1) + { + while(added <= 0 && tries < MAX_RAND_TRIES_PER_WORD){ + positionY = rand() % searchFieldLen-1; + positionX = rand() % searchFieldLen-1; + if(searchFieldLen - positionY >= wordLength) + { + + for(int i = 0; i < searchFieldLen; i++) + { + if(salad[positionY+i][positionX] != EMPTY_CHAR) + { + tries++; + break; + } + for (int j = 0; j < searchFieldLen; j++) + { + salad[positionY+j][positionX] = words[step][j]; + added = 1; + } + } + + } + else { + tries++; + } + step++; + tries = 0; + added = 0; + } + + } + + return addedWords; } // Prints the word salad to console diff --git a/Start_Windows/game.o b/Start_Windows/game.o index c2f1507..1edb3db 100644 Binary files a/Start_Windows/game.o and b/Start_Windows/game.o differ diff --git a/Start_Windows/input.c b/Start_Windows/input.c index 405ba02..cdb5ece 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -14,7 +14,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) char* token; while(fgets(zeile, MAX_LINE_LEN, file)!=NULL) { - for (int i = 0; i < 1024; i++) + for (int i = 0; i < MAX_LINE_LEN; i++) { if (zeile[i] >= 'a'&&zeile[i] <= 'z') { diff --git a/Start_Windows/input.o b/Start_Windows/input.o index 8633804..bd8f22f 100644 Binary files a/Start_Windows/input.o and b/Start_Windows/input.o differ diff --git a/Start_Windows/runTests.exe b/Start_Windows/runTests.exe index b2182dd..ca0a1a3 100644 Binary files a/Start_Windows/runTests.exe and b/Start_Windows/runTests.exe differ