diff --git a/Startcode/game.c b/Startcode/game.c index 803301f..6edd8ab 100644 --- a/Startcode/game.c +++ b/Startcode/game.c @@ -13,5 +13,89 @@ // 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) { + if (salad == NULL || words == NULL || searchFieldLen == 0){ + return 0; + } + //1. Spielfeld mit Nullen (Leerzeichen/leeren Zellen) Vorinitialisieren + for(unsigned int i = 0; i< searchFieldLen; i++){ + for(unsigned int j = 0; j < searchFieldLen; j++) + { + salad[i][j] = '\0'; + } + } + //Zufallszahlengenerator initialisation + srand((unsigned int)time(NULL)); + + unsigned int sucessfullyPlaced = 0; + + //2. Jedes Wort einzeln im Gitter Plazieren + for(unsigned int w = 0; w < wordCount; w++) + { + int len = (int)strlen(words[w]); + if((unsigned int)len > searchFieldLen){ + //wort ist zu lang für das Suchfeld + continue; + } + + int placed = 0; + int attempts = 0; + + //MAximum Versuche, um eine passende Position zu finden, um ENdlosshleifen zu vermeiden + while(!placed && attempts < 100){ + attempts++; + int direction = rand() %2; // 0 = horizontal, 1 = vertical + int row = 0; + int col = 0; + + if(direction == 0) // horizontal + { + row = rand() % searchFieldLen; + col = rand() % (searchFieldLen - len + 1); + } + else // vertical + { + row = rand() % (searchFieldLen - len + 1); + col = rand() % searchFieldLen; + } + + // KOllisionsprüfung + + int collision = 0; + for(int i = 0; i < len; i++) + { + int r = row + (direction == 1? i : 0); + int c = col + (direction == 0 ? i : 0); + + + if(salad[r][c] != '\0' && salad[r][c] != words[w][i]){ + collision = 1; + break; + } + } + + //wenn keine kollision vorliegt, Wort ins Gitter schreiben + + if(!collision){ + for(int i = 0; i < len; i++){ + int r = row + (direction == 1 ? i : 0); + int c = col + (direction == 0 ? i : 0); + salad[r][c] = words[w][i]; + } + sucessfullyPlaced++; + placed = 1; + } + } + } + + // Alle restlichen (leeren) Felder mit zufäalligen Großbuchstaben ('A' - 'Z') auffullen + for(unsigned int i = 0; i < searchFieldLen; i++){ + for(unsigned int j = 0; j < searchFieldLen; j++) + { + if(salad[i][j] == '\0'){ + salad[i][j] = (char)('A' + (rand() % 26)); + } + } + } + return (int)sucessfullyPlaced; } diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..8f24b4f 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -4,9 +4,52 @@ // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. +// HIlfsfunktion: LIest das nächste Wort(Tocken) aus der Datei und gibt es als C-String züruck +char* readNextWord(FILE *file, char *lineBuffer, unsigned int maxLineLen, int *isNewLineNeeded){ + const char *delimiters = " ,;\t\r\n"; + char *token = NULL; + + //Falls wir schon in einer Zeile sind, Versuche das nächste Word daraus zu holen + if(!(*isNewLineNeeded)){ + token = strtok(NULL, delimiters); + } + + while (token == NULL) + { + if(fgets(lineBuffer, maxLineLen, file) == NULL) + { + // Dateiend (EOF) erreicht + return NULL; + } + token = strtok(lineBuffer, delimiters); + *isNewLineNeeded = 0; + } + return token; +} // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + if(file == NULL || words == NULL || maxWordCount == 0){ + return 0; + } + char lineBuffer[MAX_LINE_LEN]; + int isNewLineNeeded = 1; + unsigned int count = 0; + char *word = NULL; + while(count < maxWordCount && (word = readNextWord(file, lineBuffer, MAX_LINE_LEN, &isNewLineNeeded)) != NULL) + { + //Sicher kopieren mit strncpy und Nulltermination garantieren + strncpy(words[count], word, MAX_WORD_LEN - 1); + words[count][MAX_WORD_LEN - 1] = '\0'; + + //changing all leter to caplogs + + for(unsigned int i = 0; words[count][i] != '\0'; i++){ + words[count][i] = (char)toupper((unsigned char)words[count][i]); + } + count++; + } + return (int)count; } \ No newline at end of file