From 2bb7b57e02126d9fce8af3d851ffcbc7ffd5fdac Mon Sep 17 00:00:00 2001 From: Alexei Date: Mon, 10 Nov 2025 15:21:46 +0100 Subject: [PATCH] Final changes --- Start_Windows/game.c | 110 +++++++++++++++++++++++++++++++++++++++-- Start_Windows/input.c | 23 +++++++++ Start_Windows/main.c | 10 ++++ Start_Windows/makefile | 7 ++- 4 files changed, 145 insertions(+), 5 deletions(-) diff --git a/Start_Windows/game.c b/Start_Windows/game.c index 3d0864d..4c053de 100644 --- a/Start_Windows/game.c +++ b/Start_Windows/game.c @@ -6,19 +6,121 @@ #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 */ // 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 createWordSalad(char salad[MAX_SEARCH_FIELD_LEN][MAX_SEARCH_FIELD_LEN], unsigned int searchFieldLen, const char words[][MAX_WORD_LEN], unsigned int wordCount) { + int addedWords = 0; + int attempts = 0; + int added = 0; // = 1 wäre Wort hinzugefügt + int space = 0; // = 1 wäre Wort zu lang für diese Stelle + unsigned long long wordLength = 0; + + srand(time(NULL)); + + for (int i = 0; i < searchFieldLen; i++) + { + for (int j = 0; j < searchFieldLen; j++) + { + salad[i][j] = '.'; //Füllen des gesamten Arrays mit '.' + } + } + for (int i=0; i < wordCount; i++) + { + + wordLength = strlen(words[i]); + + while (attempts <= MAX_RAND_TRIES_PER_WORD && added != 1) + { + int direction = rand() % 2;// 0 = Horizontal, 1 = Vertikal + if (direction == 0) //Horizontal + { + int spalte = rand() % searchFieldLen; + int reihe = rand() % searchFieldLen; + for (int j = 0; j < wordLength; j++) + { + if (salad[reihe][spalte+j] != '.') //Prüfen das Wort mit einem anderem Wort überlappen würde + { + space = 1; + break; + } + + } + if ((spalte + wordLength >= searchFieldLen-1) || (space == 1)) // überprüfung, ob das Wort an der Stelle ins Array passt + { + attempts++; + } + + + else + { + for (int j = 0; j < wordLength; j++) + { + salad[reihe][spalte+j] = words[i][j]; //Einfügen er Chars des Wortes + } + added=1; + addedWords++; + } + } + else if (direction == 1) // Vertikal + { + int spalte = rand() % searchFieldLen; + int reihe = rand() % searchFieldLen; + for (int j = 0; j < wordLength; j++) + { + if (salad[reihe+j][spalte] != '.') + { + space = 1; + break; + } + } + if ((reihe + wordLength >= searchFieldLen-1) || (space == 1)) + { + attempts++; + } + + + else + { + for (int j = 0; j < wordLength; j++) + { + salad[reihe+j][spalte] = words[i][j]; + } + added=1; + addedWords++; + } + } + space = 0; + } + attempts = 0; + added = 0; + } + for (int i = 0; i < searchFieldLen; i++) + { + for (int j = 0; j < searchFieldLen; j++) + { + if (salad[i][j] == '.') + { + salad[i][j] = rand()%('Z'-'A'+1)+'A';//Zufällige Befüllung der noch nicht befüllten Felder + } + } + } + return addedWords; } // 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]);//Ausgabe des Arrays auf die Konsole + } + } } diff --git a/Start_Windows/input.c b/Start_Windows/input.c index ed77805..d690443 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -8,5 +8,28 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + char puffer[MAX_LINE_LEN]; //Array für eingelesene Zeile + int counter = 0; //Zähler für Anzahl eingelesener Wörter + int i; + while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount) + { + char *word_parts = strtok(puffer, ",;\n\t/. "); //Wörter aufteilen + + while(word_parts != NULL && counter < maxWordCount) + { + // Kopiere das Wort in words-Array + strncpy(words[counter], word_parts, MAX_WORD_LEN - 1); + words[counter][MAX_WORD_LEN - 1] = '\0'; + + // Alles in Großbuchstaben umwandeln + for(i = 0; words[counter][i] != '\0'; i++) + { + words[counter][i] = toupper(words[counter][i]); + } + counter++; // Wort eingelesen, Wortzähler erhöhen + word_parts = strtok(NULL, ",;\n\t/. "); //NULL für Zeiger angeben + } + } + return counter; } \ No newline at end of file diff --git a/Start_Windows/main.c b/Start_Windows/main.c index 03da755..2ea62b3 100644 --- a/Start_Windows/main.c +++ b/Start_Windows/main.c @@ -38,6 +38,16 @@ int main(int argc, char *argv[]) // TODO: // Check if all words were successfully placed + if(wordCount == placedWords) + { + exitCode = EXIT_SUCCESS; + startGame(wordSalad,SALAD_SIZE, words,wordCount,750); + } + else + { + exitCode = EXIT_FAILURE; + printf("Error. The words couldn't be placed.\n"); + } // Start the game if successful // error message if some words couldn't be placed diff --git a/Start_Windows/makefile b/Start_Windows/makefile index 72cccb7..32ac0da 100644 --- a/Start_Windows/makefile +++ b/Start_Windows/makefile @@ -33,7 +33,7 @@ graphicalGame.o: graphicalGame.c # -------------------------- # Unit Tests # -------------------------- -TEST_BIN = runTests +TEST_BIN = runTests2 test: input.o game.o unit_tests.c $(CC) $(CFLAGS) -I$(unityfolder) -o $(TEST_BIN) input.o game.o unit_tests.c $(BINARIES)/libunity.a @@ -43,3 +43,8 @@ test: input.o game.o unit_tests.c # -------------------------- clean: del /f *.o *.exe +#hier eigene wordsalad kopieren, library rauslassen + +wordsalad_myversion: main.o input.o game.o graphicalGame.o + $(CC) $(CFLAGS) -o wordsalad main.o input.o game.o graphicalGame.o $(BINARIES)/libraylib.a $(LDFLAGS) +