diff --git a/I2_Wortsalat/Start_Linux/game.c b/I2_Wortsalat/Start_Linux/game.c index d8cc133..1c8027c 100644 --- a/I2_Wortsalat/Start_Linux/game.c +++ b/I2_Wortsalat/Start_Linux/game.c @@ -10,14 +10,116 @@ /* * Wörter aus der Wortliste zufällig horizontal oder vertikal platzieren * restliche Felder mit zufälligen Buchstaben füllen */ +void fillRestWithRandom(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) +{ + for (int i = 0; i < searchFieldLen; i++) + { + for (int j = 0; j < searchFieldLen; j++) + { + if (salad[i][j] == EMPTY_CHAR) { + salad[i][j] = rand() % ('Z' - 'A' + 1) + 'A'; + } + } + } +} + // Creates the word salad by placing words randomly and filling empty spaces +// - 2D array salad -> fertiger Wortsalat +// - searchFieldLen -> Dimension der Salatschüssel +// - 2D array words -> gegebene Wörter +// - wordCount -> anzahl an Wörtern 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 placedWords = 0; + //Feld komplettmit EMPTY_CHAR füllen + for (int i = 0; i < searchFieldLen; i++) + { + for (int j = 0; j < searchFieldLen; j++) + { + salad[i][j] = EMPTY_CHAR; + } + } + + //Wörter plazieren + for (int wordNumber = 0; wordNumber < wordCount; wordNumber++) + { + int wordLength = strlen(words[wordNumber]); + int placed = 0; + + for (int attempt = 0; attempt < MAX_RAND_TRIES_PER_WORD && !placed; attempt++) + { + int horizontal = rand() % 2; // 0 = vertikal, 1 = horizontal + + int x = rand() % searchFieldLen; + int y = rand() % searchFieldLen; + + if (horizontal) + { + if (x + wordLength > searchFieldLen) continue; // passt nicht + + //Prüfen ob kein anderes Wort im Weg + int fits = 1; + for (int i = 0; i < wordLength; i++) + { + if (salad[y][x + i] != EMPTY_CHAR) + { + fits = 0; + break; + } + } + + //Wort plazieren + if (fits) + { + for (int i = 0; i < wordLength; i++) + { + salad[y][x + i] = words[wordNumber][i]; + } + placed = 1; + placedWords++; + } + } + else //Wort vertikal plazieren + { + if (y + wordLength > searchFieldLen) continue; + + //Prüfen ob kein anderes Wort im Weg + int fits = 1; + for (int i = 0; i < wordLength; i++) + { + if (salad[y + i][x] != EMPTY_CHAR) + { + fits = 0; + break; + } + } + + //Wort plazieren + if (fits) + { + for (int i = 0; i < wordLength; i++) + { + salad[y + i][x] = words[wordNumber][i]; + } + placed = 1; + placedWords++; + } + } + } + } + fillRestWithRandom(salad, searchFieldLen); + return placedWords; } // Prints the word salad to console void showWordSalad(const char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen) { - + for( int i = 0; i < searchFieldLen; i++ ) + { + for( int j = 0; j < searchFieldLen; j++ ) + { + printf("%c", salad[i][j]); + } + } } diff --git a/I2_Wortsalat/Start_Linux/input.c b/I2_Wortsalat/Start_Linux/input.c index ed77805..1bfbe7c 100644 --- a/I2_Wortsalat/Start_Linux/input.c +++ b/I2_Wortsalat/Start_Linux/input.c @@ -8,5 +8,35 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + char lines [MAX_LINE_LEN]; + int word_counter = 0; + while (fgets(lines, sizeof(lines) , file) != NULL) + { + + for (int i = 0; lines[i] != '\0'; i++) //Entfernen von \n aus dem String + { + lines[i] = toupper(lines[i]); + if (lines[i] == '\n') + { + lines[i] = '\0'; + break; + } + } + + char *single_word = strtok(lines, " ;,"); + + while (single_word != NULL && word_counter < maxWordCount) + { + strncpy(words[word_counter], single_word, MAX_WORD_LEN - 1); + words [word_counter][MAX_WORD_LEN -1] = '\0'; //Zur Sicherheit, damit \0 auf alle Fälle vorhanden ist + word_counter++; + single_word = strtok(NULL, " ;,"); + } + + + } + + + return word_counter; } \ No newline at end of file diff --git a/I2_Wortsalat/Start_Linux/main.c b/I2_Wortsalat/Start_Linux/main.c index 03da755..7385192 100644 --- a/I2_Wortsalat/Start_Linux/main.c +++ b/I2_Wortsalat/Start_Linux/main.c @@ -36,15 +36,21 @@ int main(int argc, char *argv[]) // Create the word salad by placing words into grid placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); - // TODO: // Check if all words were successfully placed - // Start the game if successful - // error message if some words couldn't be placed - + if(placedWords == wordCount) + { + printf("Eingabe war erfolgreich!\nDas Spiel beginnt:"); + startGame(wordSalad, SALAD_SIZE, words, wordCount, SALAD_SIZE); + } + else + { + printf("Fehler! Es konnten nicht alle Woerter hinzugefuegt werden!\n Bitte Spiel neustarten!"); + } } else { // Print error message if file couldn't be opened + fprintf(stderr, "Could not open file %s for reading ...\n", argv[1]); exitCode = EXIT_FAILURE; } diff --git a/I2_Wortsalat/Start_Mac/input.c b/I2_Wortsalat/Start_Mac/input.c index 43d8db7..1bfbe7c 100644 --- a/I2_Wortsalat/Start_Mac/input.c +++ b/I2_Wortsalat/Start_Mac/input.c @@ -8,7 +8,7 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { - char lines [(MAX_WORD_LEN + 2) * maxWordCount]; //Es wird davon ausgegangen, dass die Wörter nur durch eine Trennzeichen und ein Leerzeichen von einander gertrennt gespeichert werden + char lines [MAX_LINE_LEN]; int word_counter = 0; while (fgets(lines, sizeof(lines) , file) != NULL) diff --git a/I2_Wortsalat/Start_Mac/main.c b/I2_Wortsalat/Start_Mac/main.c index ae0df05..7385192 100644 --- a/I2_Wortsalat/Start_Mac/main.c +++ b/I2_Wortsalat/Start_Mac/main.c @@ -36,12 +36,11 @@ int main(int argc, char *argv[]) // Create the word salad by placing words into grid placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); - // TODO: // Check if all words were successfully placed if(placedWords == wordCount) { printf("Eingabe war erfolgreich!\nDas Spiel beginnt:"); - void startGame(wordSalad, searchFieldSize, words, numberOfWords, windowSize); + startGame(wordSalad, SALAD_SIZE, words, wordCount, SALAD_SIZE); } else { diff --git a/I2_Wortsalat/Start_Windows/input.c b/I2_Wortsalat/Start_Windows/input.c index 43d8db7..1bfbe7c 100644 --- a/I2_Wortsalat/Start_Windows/input.c +++ b/I2_Wortsalat/Start_Windows/input.c @@ -8,7 +8,7 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { - char lines [(MAX_WORD_LEN + 2) * maxWordCount]; //Es wird davon ausgegangen, dass die Wörter nur durch eine Trennzeichen und ein Leerzeichen von einander gertrennt gespeichert werden + char lines [MAX_LINE_LEN]; int word_counter = 0; while (fgets(lines, sizeof(lines) , file) != NULL) diff --git a/I2_Wortsalat/Start_Windows/main.c b/I2_Wortsalat/Start_Windows/main.c index ae0df05..7385192 100644 --- a/I2_Wortsalat/Start_Windows/main.c +++ b/I2_Wortsalat/Start_Windows/main.c @@ -36,12 +36,11 @@ int main(int argc, char *argv[]) // Create the word salad by placing words into grid placedWords = createWordSalad(wordSalad, SALAD_SIZE, words, wordCount); - // TODO: // Check if all words were successfully placed if(placedWords == wordCount) { printf("Eingabe war erfolgreich!\nDas Spiel beginnt:"); - void startGame(wordSalad, searchFieldSize, words, numberOfWords, windowSize); + startGame(wordSalad, SALAD_SIZE, words, wordCount, SALAD_SIZE); } else {